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