From 05462871642f741b602150a2625b9fe2c89afd85 2020-08-27 19:13:59 From: Matthias Bussonnier Date: 2020-08-27 19:13:59 Subject: [PATCH] Use Pathlib here and there. This make code a little bit more compact and readable. --- diff --git a/IPython/core/application.py b/IPython/core/application.py index 93639d8..a823dba 100644 --- a/IPython/core/application.py +++ b/IPython/core/application.py @@ -20,6 +20,8 @@ import os import shutil import sys +from pathlib import Path + from traitlets.config.application import Application, catch_config_error from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader from IPython.core import release, crashhandler @@ -31,10 +33,10 @@ from traitlets import ( default, observe, ) -if os.name == 'nt': - programdata = os.environ.get('PROGRAMDATA', None) +if os.name == "nt": + programdata = Path(os.environ.get("PROGRAMDATA", None)) if programdata: - SYSTEM_CONFIG_DIRS = [os.path.join(programdata, 'ipython')] + SYSTEM_CONFIG_DIRS = [programdata / "ipython"] else: # PROGRAMDATA is not defined by default on XP. SYSTEM_CONFIG_DIRS = [] else: @@ -409,14 +411,15 @@ class BaseIPythonApplication(Application): self.config_file_paths.extend(ENV_CONFIG_DIRS) self.config_file_paths.extend(SYSTEM_CONFIG_DIRS) # copy config files - path = self.builtin_profile_dir + path = Path(self.builtin_profile_dir) if self.copy_config_files: src = self.profile cfg = self.config_file_name - if path and os.path.exists(os.path.join(path, cfg)): - self.log.warning("Staging %r from %s into %r [overwrite=%s]"%( - cfg, src, self.profile_dir.location, self.overwrite) + if path and (path / cfg).exists(): + self.log.warning( + "Staging %r from %s into %r [overwrite=%s]" + % (cfg, src, self.profile_dir.location, self.overwrite) ) self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite) else: @@ -425,9 +428,9 @@ class BaseIPythonApplication(Application): # Still stage *bundled* config files, but not generated ones # This is necessary for `ipython profile=sympy` to load the profile # on the first go - files = glob.glob(os.path.join(path, '*.py')) + files = path.glob("*.py") for fullpath in files: - cfg = os.path.basename(fullpath) + cfg = fullpath.name if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False): # file was copied self.log.warning("Staging bundled %s from %s into %r"%( @@ -438,11 +441,10 @@ class BaseIPythonApplication(Application): def stage_default_config_file(self): """auto generate default config file, and stage it into the profile.""" s = self.generate_config_file() - fname = os.path.join(self.profile_dir.location, self.config_file_name) - if self.overwrite or not os.path.exists(fname): - self.log.warning("Generating default config file: %r"%(fname)) - with open(fname, 'w') as f: - f.write(s) + config_file = Path(self.profile_dir.location) / self.config_file_name + if self.overwrite or not config_file.exists(): + self.log.warning("Generating default config file: %r" % (config_file)) + config_file.write_text(s) @catch_config_error def initialize(self, argv=None): diff --git a/IPython/core/profiledir.py b/IPython/core/profiledir.py index 3199dfd..756595a 100644 --- a/IPython/core/profiledir.py +++ b/IPython/core/profiledir.py @@ -7,6 +7,7 @@ import os import shutil import errno +from pathlib import Path from traitlets.config.configurable import LoggingConfigurable from ..paths import get_ipython_package_dir @@ -133,19 +134,20 @@ class ProfileDir(LoggingConfigurable): self.check_pid_dir() self.check_startup_dir() - def copy_config_file(self, config_file, path=None, overwrite=False): + def copy_config_file(self, config_file: str, path: Path, overwrite=False) -> bool: """Copy a default config file into the active profile directory. Default configuration files are kept in :mod:`IPython.core.profile`. This function moves these from that location to the working profile directory. """ - dst = os.path.join(self.location, config_file) - if os.path.isfile(dst) and not overwrite: + dst = Path(os.path.join(self.location, config_file)) + if dst.exists() and not overwrite: return False if path is None: path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default') - src = os.path.join(path, config_file) + assert isinstance(path, Path) + src = path / config_file shutil.copy(src, dst) return True