##// END OF EJS Templates
Use Pathlib here and there....
Matthias Bussonnier -
Show More
@@ -20,6 +20,8 b' import os'
20 import shutil
20 import shutil
21 import sys
21 import sys
22
22
23 from pathlib import Path
24
23 from traitlets.config.application import Application, catch_config_error
25 from traitlets.config.application import Application, catch_config_error
24 from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader
26 from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader
25 from IPython.core import release, crashhandler
27 from IPython.core import release, crashhandler
@@ -31,10 +33,10 b' from traitlets import ('
31 default, observe,
33 default, observe,
32 )
34 )
33
35
34 if os.name == 'nt':
36 if os.name == "nt":
35 programdata = os.environ.get('PROGRAMDATA', None)
37 programdata = Path(os.environ.get("PROGRAMDATA", None))
36 if programdata:
38 if programdata:
37 SYSTEM_CONFIG_DIRS = [os.path.join(programdata, 'ipython')]
39 SYSTEM_CONFIG_DIRS = [programdata / "ipython"]
38 else: # PROGRAMDATA is not defined by default on XP.
40 else: # PROGRAMDATA is not defined by default on XP.
39 SYSTEM_CONFIG_DIRS = []
41 SYSTEM_CONFIG_DIRS = []
40 else:
42 else:
@@ -409,14 +411,15 b' class BaseIPythonApplication(Application):'
409 self.config_file_paths.extend(ENV_CONFIG_DIRS)
411 self.config_file_paths.extend(ENV_CONFIG_DIRS)
410 self.config_file_paths.extend(SYSTEM_CONFIG_DIRS)
412 self.config_file_paths.extend(SYSTEM_CONFIG_DIRS)
411 # copy config files
413 # copy config files
412 path = self.builtin_profile_dir
414 path = Path(self.builtin_profile_dir)
413 if self.copy_config_files:
415 if self.copy_config_files:
414 src = self.profile
416 src = self.profile
415
417
416 cfg = self.config_file_name
418 cfg = self.config_file_name
417 if path and os.path.exists(os.path.join(path, cfg)):
419 if path and (path / cfg).exists():
418 self.log.warning("Staging %r from %s into %r [overwrite=%s]"%(
420 self.log.warning(
419 cfg, src, self.profile_dir.location, self.overwrite)
421 "Staging %r from %s into %r [overwrite=%s]"
422 % (cfg, src, self.profile_dir.location, self.overwrite)
420 )
423 )
421 self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
424 self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
422 else:
425 else:
@@ -425,9 +428,9 b' class BaseIPythonApplication(Application):'
425 # Still stage *bundled* config files, but not generated ones
428 # Still stage *bundled* config files, but not generated ones
426 # This is necessary for `ipython profile=sympy` to load the profile
429 # This is necessary for `ipython profile=sympy` to load the profile
427 # on the first go
430 # on the first go
428 files = glob.glob(os.path.join(path, '*.py'))
431 files = path.glob("*.py")
429 for fullpath in files:
432 for fullpath in files:
430 cfg = os.path.basename(fullpath)
433 cfg = fullpath.name
431 if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
434 if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
432 # file was copied
435 # file was copied
433 self.log.warning("Staging bundled %s from %s into %r"%(
436 self.log.warning("Staging bundled %s from %s into %r"%(
@@ -438,11 +441,10 b' class BaseIPythonApplication(Application):'
438 def stage_default_config_file(self):
441 def stage_default_config_file(self):
439 """auto generate default config file, and stage it into the profile."""
442 """auto generate default config file, and stage it into the profile."""
440 s = self.generate_config_file()
443 s = self.generate_config_file()
441 fname = os.path.join(self.profile_dir.location, self.config_file_name)
444 config_file = Path(self.profile_dir.location) / self.config_file_name
442 if self.overwrite or not os.path.exists(fname):
445 if self.overwrite or not config_file.exists():
443 self.log.warning("Generating default config file: %r"%(fname))
446 self.log.warning("Generating default config file: %r" % (config_file))
444 with open(fname, 'w') as f:
447 config_file.write_text(s)
445 f.write(s)
446
448
447 @catch_config_error
449 @catch_config_error
448 def initialize(self, argv=None):
450 def initialize(self, argv=None):
@@ -7,6 +7,7 b''
7 import os
7 import os
8 import shutil
8 import shutil
9 import errno
9 import errno
10 from pathlib import Path
10
11
11 from traitlets.config.configurable import LoggingConfigurable
12 from traitlets.config.configurable import LoggingConfigurable
12 from ..paths import get_ipython_package_dir
13 from ..paths import get_ipython_package_dir
@@ -133,19 +134,20 b' class ProfileDir(LoggingConfigurable):'
133 self.check_pid_dir()
134 self.check_pid_dir()
134 self.check_startup_dir()
135 self.check_startup_dir()
135
136
136 def copy_config_file(self, config_file, path=None, overwrite=False):
137 def copy_config_file(self, config_file: str, path: Path, overwrite=False) -> bool:
137 """Copy a default config file into the active profile directory.
138 """Copy a default config file into the active profile directory.
138
139
139 Default configuration files are kept in :mod:`IPython.core.profile`.
140 Default configuration files are kept in :mod:`IPython.core.profile`.
140 This function moves these from that location to the working profile
141 This function moves these from that location to the working profile
141 directory.
142 directory.
142 """
143 """
143 dst = os.path.join(self.location, config_file)
144 dst = Path(os.path.join(self.location, config_file))
144 if os.path.isfile(dst) and not overwrite:
145 if dst.exists() and not overwrite:
145 return False
146 return False
146 if path is None:
147 if path is None:
147 path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default')
148 path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default')
148 src = os.path.join(path, config_file)
149 assert isinstance(path, Path)
150 src = path / config_file
149 shutil.copy(src, dst)
151 shutil.copy(src, dst)
150 return True
152 return True
151
153
General Comments 0
You need to be logged in to leave comments. Login now