##// END OF EJS Templates
Merge pull request #13741 from Carreau/ruff...
Merge pull request #13741 from Carreau/ruff try ruff to find unused imports

File last commit:

r27764:aefe51c6
r27785:ae0dfcd9 merge
Show More
application.py
489 lines | 18.5 KiB | text/x-python | PythonLexer
Brian Granger
Working version of the new config loaders for .py files and argparse.
r2185 # encoding: utf-8
"""
Brian Granger
ipcontroller/ipengine use the new clusterdir.py module.
r2301 An application for IPython.
All top-level applications should use the classes in this module for
MinRK
create [nb]extensions dirs in skeleton IPYTHONDIR
r12814 handling configuration and creating configurables.
Brian Granger
ipcontroller/ipengine use the new clusterdir.py module.
r2301
Bernardo B. Marques
remove all trailling spaces
r4872 The job of an :class:`Application` is to create the master configuration
Brian Granger
First draft of refactored Component->Configurable.
r2731 object and then create the configurable objects, passing the config to them.
Brian Granger
Working version of the new config loaders for .py files and argparse.
r2185 """
Min RK
add system-wide IPython config...
r18527 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Working version of the new config loaders for .py files and argparse.
r2185
MinRK
prevent atexit handlers from generating crash report...
r4994 import atexit
Min RK
Don't rely upon traitlets copying self.config...
r22585 from copy import deepcopy
Brian Granger
Work on startup related things....
r2252 import logging
Brian Granger
Semi-final Application and minor work on traitlets.
r2200 import os
MinRK
rename core.newapplication -> core.application
r4023 import shutil
Brian Granger
Working version of the new config loaders for .py files and argparse.
r2185 import sys
Brian Granger
Massive refactoring of of the core....
r2245
Matthias Bussonnier
Use Pathlib here and there....
r25972 from pathlib import Path
Min RK
update dependency imports...
r21253 from traitlets.config.application import Application, catch_config_error
from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader
Fernando Perez
Move crash handling to the application level and simplify class structure....
r2403 from IPython.core import release, crashhandler
MinRK
move ipcluster create|list to `ipython profile create|list`...
r4024 from IPython.core.profiledir import ProfileDir, ProfileDirError
Min RK
update dependency imports...
r21253 from IPython.paths import get_ipython_dir, get_ipython_package_dir
from IPython.utils.path import ensure_dir_exists
Min RK
adopt traitlets 4.2 API...
r22340 from traitlets import (
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 List, Unicode, Type, Bool, Set, Instance, Undefined,
Min RK
adopt traitlets 4.2 API...
r22340 default, observe,
)
Brian Granger
Working version of the new config loaders for .py files and argparse.
r2185
Matthias Bussonnier
Use Pathlib here and there....
r25972 if os.name == "nt":
Matthias Bussonnier
Fix case where programdata is None on windows....
r27343 programdata = os.environ.get("PROGRAMDATA", None)
if programdata is not None:
SYSTEM_CONFIG_DIRS = [str(Path(programdata) / "ipython")]
Min RK
add system-wide IPython config...
r18527 else: # PROGRAMDATA is not defined by default on XP.
SYSTEM_CONFIG_DIRS = []
else:
SYSTEM_CONFIG_DIRS = [
"/usr/local/etc/ipython",
"/etc/ipython",
]
Brian Granger
Working version of the new config loaders for .py files and argparse.
r2185
Min RK
include {sys.prefix}/etc/ipython in config path
r23759
ENV_CONFIG_DIRS = []
_env_config_dir = os.path.join(sys.prefix, 'etc', 'ipython')
if _env_config_dir not in SYSTEM_CONFIG_DIRS:
# only add ENV_CONFIG if sys.prefix is not already included
ENV_CONFIG_DIRS.append(_env_config_dir)
Matthias Bussonnier
Rename env variable.
r22366 _envvar = os.environ.get('IPYTHON_SUPPRESS_CONFIG_ERRORS')
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 if _envvar in {None, ''}:
Matthias Bussonnier
Rename env variable.
r22366 IPYTHON_SUPPRESS_CONFIG_ERRORS = None
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 else:
if _envvar.lower() in {'1','true'}:
Matthias Bussonnier
Rename env variable.
r22366 IPYTHON_SUPPRESS_CONFIG_ERRORS = True
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 elif _envvar.lower() in {'0','false'} :
Matthias Bussonnier
Rename env variable.
r22366 IPYTHON_SUPPRESS_CONFIG_ERRORS = False
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 else:
Matthias Bussonnier
Rename env variable.
r22366 sys.exit("Unsupported value for environment variable: 'IPYTHON_SUPPRESS_CONFIG_ERRORS' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}."% _envvar )
MinRK
rename core.newapplication -> core.application
r4023
# aliases and flags
Min RK
inherit base aliases / flags from traitlets.config.Application...
r26681 base_aliases = {}
if isinstance(Application.aliases, dict):
# traitlets 5
base_aliases.update(Application.aliases)
base_aliases.update(
{
"profile-dir": "ProfileDir.location",
"profile": "BaseIPythonApplication.profile",
"ipython-dir": "BaseIPythonApplication.ipython_dir",
"log-level": "Application.log_level",
"config": "BaseIPythonApplication.extra_config_file",
}
)
base_flags = dict()
if isinstance(Application.flags, dict):
# traitlets 5
base_flags.update(Application.flags)
base_flags.update(
dict(
debug=(
{"Application": {"log_level": logging.DEBUG}},
"set log level to logging.DEBUG (maximize logging output)",
),
quiet=(
{"Application": {"log_level": logging.CRITICAL}},
"set log level to logging.CRITICAL (minimize logging output)",
),
init=(
{
"BaseIPythonApplication": {
"copy_config_files": True,
"auto_create": True,
}
},
"""Initialize profile with default config files. This is equivalent
MinRK
command-line pass...
r4247 to running `ipython profile create <profile>` prior to startup.
Min RK
inherit base aliases / flags from traitlets.config.Application...
r26681 """,
),
)
MinRK
rename core.newapplication -> core.application
r4023 )
Min RK
inherit base aliases / flags from traitlets.config.Application...
r26681
Min RK
remove profile awareness from load_subconfig...
r20868 class ProfileAwareConfigLoader(PyFileConfigLoader):
"""A Python file config loader that is aware of IPython profiles."""
def load_subconfig(self, fname, path=None, profile=None):
if profile is not None:
try:
profile_dir = ProfileDir.find_profile_dir_by_name(
get_ipython_dir(),
profile,
)
except ProfileDirError:
return
path = profile_dir.location
return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path)
MinRK
rename core.newapplication -> core.application
r4023
class BaseIPythonApplication(Application):
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 name = u'ipython'
MinRK
rename core.newapplication -> core.application
r4023 description = Unicode(u'IPython: an enhanced interactive Python shell.')
version = Unicode(release.version)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Clean up some attributes which don't need to be traitlets
r23932 aliases = base_aliases
flags = base_flags
MinRK
expose IPClusterEngines.daemonize as `--daemonize` flag....
r4114 classes = List([ProfileDir])
Min RK
remove profile awareness from load_subconfig...
r20868
# enable `load_subconfig('cfg.py', profile='name')`
python_config_loader_class = ProfileAwareConfigLoader
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
rename core.newapplication -> core.application
r4023 # Track whether the config_file has changed,
# because some logic happens only if we aren't using the default.
MinRK
add extra_config_file...
r11277 config_file_specified = Set()
Bernardo B. Marques
remove all trailling spaces
r4872
Jonathan Frederic
Fix, config_file_name was ignored
r11364 config_file_name = Unicode()
Min RK
adopt traitlets 4.2 API...
r22340 @default('config_file_name')
MinRK
default config files are automatically generated...
r4025 def _config_file_name_default(self):
return self.name.replace('-','_') + u'_config.py'
Min RK
adopt traitlets 4.2 API...
r22340 @observe('config_file_name')
def _config_file_name_changed(self, change):
if change['new'] != change['old']:
self.config_file_specified.add(change['new'])
MinRK
rename core.newapplication -> core.application
r4023
# The directory that contains IPython's builtin profiles.
builtin_profile_dir = Unicode(
os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default')
)
Min RK
add system-wide IPython config...
r18527
Jason Grout
Use instances of traits instead of trait classes
r21534 config_file_paths = List(Unicode())
Min RK
adopt traitlets 4.2 API...
r22340 @default('config_file_paths')
MinRK
rename core.newapplication -> core.application
r4023 def _config_file_paths_default(self):
Matthias Bussonnier
FIX CVE-2022-21699...
r27464 return []
MinRK
rename core.newapplication -> core.application
r4023
Min RK
adopt traitlets 4.2 API...
r22340 extra_config_file = Unicode(
MinRK
add extra_config_file...
r11277 help="""Path to an extra config file to load.
If specified, load this config file in addition to any other IPython config.
Min RK
adopt traitlets 4.2 API...
r22340 """).tag(config=True)
@observe('extra_config_file')
def _extra_config_file_changed(self, change):
old = change['old']
new = change['new']
MinRK
add extra_config_file...
r11277 try:
self.config_files.remove(old)
except ValueError:
pass
self.config_file_specified.add(new)
self.config_files.append(new)
Min RK
adopt traitlets 4.2 API...
r22340 profile = Unicode(u'default',
MinRK
rename core.newapplication -> core.application
r4023 help="""The IPython profile to use."""
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
@observe('profile')
def _profile_changed(self, change):
MinRK
rename core.newapplication -> core.application
r4023 self.builtin_profile_dir = os.path.join(
Min RK
adopt traitlets 4.2 API...
r22340 get_ipython_package_dir(), u'config', u'profile', change['new']
MinRK
rename core.newapplication -> core.application
r4023 )
Bernardo B. Marques
remove all trailling spaces
r4872
Matthias Bussonnier
reformat
r27198 add_ipython_dir_to_sys_path = Bool(
False,
Matthias Bussonnier
Don't add ipython unconditionally to sys.path...
r27190 """Should the IPython profile directory be added to sys path ?
This option was non-existing before IPython 8.0, and ipython_dir was added to
sys path to allow import of extensions present there. This was historical
baggage from when pip did not exist. This now default to false,
Matthias Bussonnier
Apply suggestions from code review...
r27197 but can be set to true for legacy reasons.
Matthias Bussonnier
reformat
r27198 """,
).tag(config=True)
Matthias Bussonnier
Don't add ipython unconditionally to sys.path...
r27190
Min RK
adopt traitlets 4.2 API...
r22340 ipython_dir = Unicode(
MinRK
rename core.newapplication -> core.application
r4023 help="""
The name of the IPython directory. This directory is used for logging
configuration (through profiles), history storage, etc. The default
Thomas Kluyver
Trim which options are exposed on kernelspec CLI
r16597 is usually $HOME/.ipython. This option can also be specified through
Bradley M. Froehle
IPYTHON_DIR -> IPYTHONDIR in comments and documentation
r6696 the environment variable IPYTHONDIR.
Brian Granger
General work on the kernel config.
r2294 """
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
@default('ipython_dir')
MinRK
create [nb]extensions dirs in skeleton IPYTHONDIR
r12814 def _ipython_dir_default(self):
d = get_ipython_dir()
Min RK
adopt traitlets 4.2 API...
r22340 self._ipython_dir_changed({
'name': 'ipython_dir',
'old': d,
'new': d,
})
MinRK
create [nb]extensions dirs in skeleton IPYTHONDIR
r12814 return d
MinRK
prevent profile_dir from being undefined...
r11836 _in_init_profile_dir = False
Sylvain Corlay
profile_dir
r20946 profile_dir = Instance(ProfileDir, allow_none=True)
Min RK
adopt traitlets 4.2 API...
r22340 @default('profile_dir')
MinRK
prevent profile_dir from being undefined...
r11836 def _profile_dir_default(self):
# avoid recursion
if self._in_init_profile_dir:
return
# profile_dir requested early, force initialization
self.init_profile_dir()
return self.profile_dir
Bernardo B. Marques
remove all trailling spaces
r4872
Min RK
adopt traitlets 4.2 API...
r22340 overwrite = Bool(False,
help="""Whether to overwrite existing config files when copying"""
).tag(config=True)
auto_create = Bool(False,
help="""Whether to create profile dir if it doesn't exist"""
).tag(config=True)
Bernardo B. Marques
remove all trailling spaces
r4872
Jason Grout
Use instances of traits instead of trait classes
r21534 config_files = List(Unicode())
Min RK
adopt traitlets 4.2 API...
r22340 @default('config_files')
MinRK
rename core.newapplication -> core.application
r4023 def _config_files_default(self):
Jonathan Frederic
Fix, config_file_name was ignored
r11364 return [self.config_file_name]
Bernardo B. Marques
remove all trailling spaces
r4872
Min RK
adopt traitlets 4.2 API...
r22340 copy_config_files = Bool(False,
MinRK
default config files are automatically generated...
r4025 help="""Whether to install the default config files into the profile dir.
If a new profile is being created, and IPython contains config files for that
profile, then they will be staged into the new directory. Otherwise,
default config files will be automatically generated.
Min RK
adopt traitlets 4.2 API...
r22340 """).tag(config=True)
MinRK
Don't use crash_handler by default...
r5317
Min RK
adopt traitlets 4.2 API...
r22340 verbose_crash = Bool(False,
Jason Grout
Fix typo enconters->encounters
r6872 help="""Create a massive crash report when IPython encounters what may be an
MinRK
Don't use crash_handler by default...
r5317 internal error. The default is to append a short message to the
Min RK
adopt traitlets 4.2 API...
r22340 usual traceback""").tag(config=True)
MinRK
rename core.newapplication -> core.application
r4023
# The class to use as the crash handler.
crash_handler_class = Type(crashhandler.CrashHandler)
Thomas Spura
Use @catch_config_error to catch exception from getcwdu in nonexisting directory
r10381 @catch_config_error
MinRK
rename core.newapplication -> core.application
r4023 def __init__(self, **kwargs):
super(BaseIPythonApplication, self).__init__(**kwargs)
Thomas Spura
Search for first existing directory, if cwd doesn't exist....
r10154 # ensure current working directory exists
Thomas Spura
Report if cwd does not exist and raise exception in BaseIPythonApplication...
r10169 try:
Srinivas Reddy Thatiparthy
rename py3compat.getcwd() -> os.getcwd()
r23045 os.getcwd()
Thomas Spura
Report if cwd does not exist and raise exception in BaseIPythonApplication...
r10169 except:
Min RK
exit if cwd doesn't exist...
r20485 # exit if cwd doesn't exist
Thomas Spura
Report if cwd does not exist and raise exception in BaseIPythonApplication...
r10169 self.log.error("Current working directory doesn't exist.")
Min RK
exit if cwd doesn't exist...
r20485 self.exit(1)
Thomas Spura
Search for first existing directory, if cwd doesn't exist....
r10154
MinRK
rename core.newapplication -> core.application
r4023 #-------------------------------------------------------------------------
# Various stages of Application creation
#-------------------------------------------------------------------------
Min RK
ensure deprecated_subcommands is defined...
r21893
MinRK
rename core.newapplication -> core.application
r4023 def init_crash_handler(self):
"""Create a crash handler, typically setting sys.excepthook to it."""
self.crash_handler = self.crash_handler_class(self)
MinRK
Don't use crash_handler by default...
r5317 sys.excepthook = self.excepthook
MinRK
prevent atexit handlers from generating crash report...
r4994 def unset_crashhandler():
sys.excepthook = sys.__excepthook__
atexit.register(unset_crashhandler)
MinRK
Don't use crash_handler by default...
r5317
def excepthook(self, etype, evalue, tb):
"""this is sys.excepthook after init_crashhandler
Matthias Bussonnier
DOC: Some more autoformatting of docstrings....
r26491
MinRK
Don't use crash_handler by default...
r5317 set self.verbose_crash=True to use our full crashhandler, instead of
a regular traceback with a short message (crash_handler_lite)
"""
if self.verbose_crash:
return self.crash_handler(etype, evalue, tb)
else:
return crashhandler.crash_handler_lite(etype, evalue, tb)
Min RK
adopt traitlets 4.2 API...
r22340
@observe('ipython_dir')
def _ipython_dir_changed(self, change):
old = change['old']
new = change['new']
Sylvain Corlay
minor fixes on custom serialization, and traitlet hold_traits
r21172 if old is not Undefined:
Srinivas Reddy Thatiparthy
rm cast_bytes_py2
r23666 str_old = os.path.abspath(old)
Thomas Kluyver
Don't calculate default value when setting traitlet for the first time
r19121 if str_old in sys.path:
sys.path.remove(str_old)
Matthias Bussonnier
Don't add ipython unconditionally to sys.path...
r27190 if self.add_ipython_dir_to_sys_path:
str_path = os.path.abspath(new)
Matthias Bussonnier
Apply suggestions from code review...
r27197 sys.path.append(str_path)
Matthias Bussonnier
Don't add ipython unconditionally to sys.path...
r27190 ensure_dir_exists(new)
Matthias Bussonnier
reformat
r27198 readme = os.path.join(new, "README")
readme_src = os.path.join(
get_ipython_package_dir(), "config", "profile", "README"
)
Matthias Bussonnier
Don't add ipython unconditionally to sys.path...
r27190 if not os.path.exists(readme) and os.path.exists(readme_src):
shutil.copy(readme_src, readme)
Matthias Bussonnier
reformat
r27198 for d in ("extensions", "nbextensions"):
Matthias Bussonnier
Don't add ipython unconditionally to sys.path...
r27190 path = os.path.join(new, d)
try:
ensure_dir_exists(path)
except OSError as e:
# this will not be EEXIST
self.log.error("couldn't create path %s: %s", path, e)
self.log.debug("IPYTHONDIR set to: %s" % new)
MinRK
rename core.newapplication -> core.application
r4023
Matthias Bussonnier
Rename env variable.
r22366 def load_config_file(self, suppress_errors=IPYTHON_SUPPRESS_CONFIG_ERRORS):
Brian Granger
Semi-final Application and minor work on traitlets.
r2200 """Load the config file.
MinRK
rename core.newapplication -> core.application
r4023
Thomas Kluyver
Tweak code with suggestions from yesterday.
r3458 By default, errors in loading config are handled, and a warning
printed on screen. For testing, the suppress_errors option is set
to False, so errors will make tests fail.
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360
Min ho Kim
Fix typos
r25146 `suppress_errors` default value is to be `None` in which case the
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 behavior default to the one of `traitlets.Application`.
Matthias Bussonnier
/change/set/
r22364 The default value can be set :
Matthias Bussonnier
Rename env variable.
r22366 - to `False` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '0', or 'false' (case insensitive).
- to `True` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '1' or 'true' (case insensitive).
- to `None` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '' (empty string) or leaving it unset.
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360
Any other value are invalid, and will make IPython exit with a non-zero return code.
Brian Granger
Semi-final Application and minor work on traitlets.
r2200 """
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360
MinRK
make config-loading debug messages more explicit...
r4564 self.log.debug("Searching path %s for config files", self.config_file_paths)
MinRK
default config files are automatically generated...
r4025 base_config = 'ipython_config.py'
self.log.debug("Attempting to load config file: %s" %
base_config)
try:
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 if suppress_errors is not None:
old_value = Application.raise_config_file_errors
Application.raise_config_file_errors = not suppress_errors;
MinRK
default config files are automatically generated...
r4025 Application.load_config_file(
self,
Bernardo B. Marques
remove all trailling spaces
r4872 base_config,
MinRK
default config files are automatically generated...
r4025 path=self.config_file_paths
)
MinRK
catch ConfigFileNotFound where appropriate...
r4909 except ConfigFileNotFound:
MinRK
default config files are automatically generated...
r4025 # ignore errors loading parent
MinRK
make config-loading debug messages more explicit...
r4564 self.log.debug("Config file %s not found", base_config)
MinRK
default config files are automatically generated...
r4025 pass
Matthias Bussonnier
Allow a raise on bad config using env variable....
r22360 if suppress_errors is not None:
Application.raise_config_file_errors = old_value
MinRK
add extra_config_file...
r11277
for config_file_name in self.config_files:
if not config_file_name or config_file_name == base_config:
continue
self.log.debug("Attempting to load config file: %s" %
self.config_file_name)
try:
Application.load_config_file(
self,
config_file_name,
path=self.config_file_paths
)
except ConfigFileNotFound:
# Only warn if the default config file was NOT being used.
if config_file_name in self.config_file_specified:
Pierre Gerold
log.warn -> log.warning in application.py
r21887 msg = self.log.warning
MinRK
add extra_config_file...
r11277 else:
msg = self.log.debug
msg("Config file not found, skipping: %s", config_file_name)
Matthias Bussonnier
dont' swallow sys.exit
r21262 except Exception:
MinRK
add extra_config_file...
r11277 # For testing purposes.
if not suppress_errors:
raise
Pierre Gerold
log.warn -> log.warning in application.py
r21887 self.log.warning("Error loading config file: %s" %
MinRK
add extra_config_file...
r11277 self.config_file_name, exc_info=True)
Brian Granger
General work on the kernel config.
r2294
MinRK
rename core.newapplication -> core.application
r4023 def init_profile_dir(self):
"""initialize the profile dir"""
MinRK
prevent profile_dir from being undefined...
r11836 self._in_init_profile_dir = True
MinRK
don't run init_profile_dir twice
r11870 if self.profile_dir is not None:
# already ran
return
MinRK
AttributeError check on config no longer works...
r12796 if 'ProfileDir.location' not in self.config:
MinRK
rename core.newapplication -> core.application
r4023 # location not specified, find by profile name
Brian Granger
Fixing minor bug with the logging level in ipapp.py.
r2270 try:
MinRK
rename core.newapplication -> core.application
r4023 p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
except ProfileDirError:
# not found, maybe create it (always create default profile)
Matthias BUSSONNIER
fix error in doc (arg->kwarg) and pep-8
r8885 if self.auto_create or self.profile == 'default':
MinRK
rename core.newapplication -> core.application
r4023 try:
p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
except ProfileDirError:
self.log.fatal("Could not create profile: %r"%self.profile)
self.exit(1)
else:
self.log.info("Created profile dir: %r"%p.location)
else:
self.log.fatal("Profile %r not found."%self.profile)
self.exit(1)
else:
Min RK
match log message for profile by name and path...
r26859 self.log.debug(f"Using existing profile dir: {p.location!r}")
Brian Granger
Lots more work on the kernel scripts.
r2303 else:
MinRK
AttributeError check on config no longer works...
r12796 location = self.config.ProfileDir.location
MinRK
rename core.newapplication -> core.application
r4023 # location is fully specified
try:
p = ProfileDir.find_profile_dir(location, self.config)
except ProfileDirError:
# not found, maybe create it
if self.auto_create:
try:
p = ProfileDir.create_profile_dir(location, self.config)
except ProfileDirError:
self.log.fatal("Could not create profile directory: %r"%location)
self.exit(1)
else:
Matthias Bussonnier
Move “Using existing profile dir” from info to debug:...
r20725 self.log.debug("Creating new profile dir: %r"%location)
MinRK
rename core.newapplication -> core.application
r4023 else:
self.log.fatal("Profile directory %r not found."%location)
self.exit(1)
else:
Min RK
match log message for profile by name and path...
r26859 self.log.debug(f"Using existing profile dir: {p.location!r}")
MinRK
set profile name from profile_dir...
r14790 # if profile_dir is specified explicitly, set profile name
dir_name = os.path.basename(p.location)
if dir_name.startswith('profile_'):
self.profile = dir_name[8:]
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
rename core.newapplication -> core.application
r4023 self.profile_dir = p
self.config_file_paths.append(p.location)
MinRK
prevent profile_dir from being undefined...
r11836 self._in_init_profile_dir = False
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
rename core.newapplication -> core.application
r4023 def init_config_files(self):
"""[optionally] copy default config files into profile dir."""
Min RK
include {sys.prefix}/etc/ipython in config path
r23759 self.config_file_paths.extend(ENV_CONFIG_DIRS)
Min RK
add system-wide IPython config...
r18527 self.config_file_paths.extend(SYSTEM_CONFIG_DIRS)
MinRK
rename core.newapplication -> core.application
r4023 # copy config files
Matthias Bussonnier
Use Pathlib here and there....
r25972 path = Path(self.builtin_profile_dir)
MinRK
rename core.newapplication -> core.application
r4023 if self.copy_config_files:
src = self.profile
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
default config files are automatically generated...
r4025 cfg = self.config_file_name
Matthias Bussonnier
Use Pathlib here and there....
r25972 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)
MinRK
default config files are automatically generated...
r4025 )
MinRK
rename core.newapplication -> core.application
r4023 self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
MinRK
default config files are automatically generated...
r4025 else:
self.stage_default_config_file()
MinRK
load bundled profiles without having to use 'profile create' or '--init'...
r4122 else:
# Still stage *bundled* config files, but not generated ones
# This is necessary for `ipython profile=sympy` to load the profile
# on the first go
Matthias Bussonnier
Use Pathlib here and there....
r25972 files = path.glob("*.py")
MinRK
load bundled profiles without having to use 'profile create' or '--init'...
r4122 for fullpath in files:
Matthias Bussonnier
Use Pathlib here and there....
r25972 cfg = fullpath.name
MinRK
load bundled profiles without having to use 'profile create' or '--init'...
r4122 if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
# file was copied
Pierre Gerold
log.warn -> log.warning in application.py
r21887 self.log.warning("Staging bundled %s from %s into %r"%(
MinRK
load bundled profiles without having to use 'profile create' or '--init'...
r4122 cfg, self.profile, self.profile_dir.location)
)
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
default config files are automatically generated...
r4025 def stage_default_config_file(self):
"""auto generate default config file, and stage it into the profile."""
s = self.generate_config_file()
Matthias Bussonnier
Use Pathlib here and there....
r25972 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))
gousaiyang
Format code
r27495 config_file.write_text(s, encoding="utf-8")
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
catch_config -> catch_config_error
r5214 @catch_config_error
MinRK
rename core.newapplication -> core.application
r4023 def initialize(self, argv=None):
MinRK
don't crash apps on TraitErrors when parsing command-line
r4106 # don't hook up crash handler before parsing command-line
MinRK
rename core.newapplication -> core.application
r4023 self.parse_command_line(argv)
MinRK
don't crash apps on TraitErrors when parsing command-line
r4106 self.init_crash_handler()
MinRK
default config files are automatically generated...
r4025 if self.subapp is not None:
# stop here if subapp is taking over
return
Min RK
Don't rely upon traitlets copying self.config...
r22585 # save a copy of CLI config to re-load after config files
# so that it has highest priority
cl_config = deepcopy(self.config)
MinRK
rename core.newapplication -> core.application
r4023 self.init_profile_dir()
self.init_config_files()
self.load_config_file()
# enforce cl-opts override configfile opts:
self.update_config(cl_config)