Skip to content

Commit

Permalink
inherit base aliases/flags from traitlets Application
Browse files Browse the repository at this point in the history
gets `--show-config` and `--show-config-json` options for all Applications
  • Loading branch information
minrk committed Aug 3, 2021
1 parent e21f003 commit 9a97bdc
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions jupyter_core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@

# aliases and flags

base_aliases = {
base_aliases = {}
if isinstance(Application.aliases, dict):
# traitlets 5
base_aliases.update(Application.aliases)
_jupyter_aliases = {
'log-level' : 'Application.log_level',
'config' : 'JupyterApp.config_file',
}
base_aliases.update(_jupyter_aliases)

base_flags = {
base_flags = {}
if isinstance(Application.flags, dict):
# traitlets 5
base_flags.update(Application.flags)
_jupyter_flags = {
'debug': ({'Application' : {'log_level' : logging.DEBUG}},
"set log level to logging.DEBUG (maximize logging output)"),
'generate-config': ({'JupyterApp': {'generate_config': True}},
"generate default config file"),
'y': ({'JupyterApp': {'answer_yes': True}},
"Answer yes to any questions instead of prompting."),
}
base_flags.update(_jupyter_flags)

class NoStart(Exception):
"""Exception to raise when an application shouldn't start"""
Expand All @@ -48,38 +58,38 @@ class JupyterApp(Application):
"""Base class for Jupyter applications"""
name = 'jupyter' # override in subclasses
description = "A Jupyter Application"

aliases = base_aliases
flags = base_flags

def _log_level_default(self):
return logging.INFO

jupyter_path = List(Unicode())
def _jupyter_path_default(self):
return jupyter_path()

config_dir = Unicode()

def _config_dir_default(self):
return jupyter_config_dir()

@property
def config_file_paths(self):
path = jupyter_config_path()
if self.config_dir not in path:
path.insert(0, self.config_dir)
path.insert(0, os.getcwd())
return path

data_dir = Unicode()

def _data_dir_default(self):
d = jupyter_data_dir()
ensure_dir_exists(d, mode=0o700)
return d
runtime_dir = Unicode()

def _runtime_dir_default(self):
rd = jupyter_runtime_dir()
ensure_dir_exists(rd, mode=0o700)
Expand All @@ -92,30 +102,30 @@ def _runtime_dir_changed(self, change):
generate_config = Bool(False, config=True,
help="""Generate default config file."""
)

config_file_name = Unicode(config=True,
help="Specify a config file to load."
)
def _config_file_name_default(self):
if not self.name:
return ''
return self.name.replace('-','_') + '_config'

config_file = Unicode(config=True,
help="""Full path of a config file.""",
)

answer_yes = Bool(False, config=True,
help="""Answer yes to any prompts."""
)

def write_default_config(self):
"""Write our default config to a .py config file"""
if self.config_file:
config_file = self.config_file
else:
config_file = os.path.join(self.config_dir, self.config_file_name + '.py')

if os.path.exists(config_file) and not self.answer_yes:
answer = ''
def ask():
Expand All @@ -131,15 +141,15 @@ def ask():
answer = ask()
if answer.startswith('n'):
return

config_text = self.generate_config_file()
if isinstance(config_text, bytes):
config_text = config_text.decode('utf8')
print("Writing default config to: %s" % config_file)
ensure_dir_exists(os.path.abspath(os.path.dirname(config_file)), 0o700)
with open(config_file, mode='w') as f:
f.write(config_text)

def migrate_config(self):
"""Migrate config/data from IPython 3"""
if os.path.exists(os.path.join(self.config_dir, 'migrated')):
Expand Down Expand Up @@ -200,17 +210,17 @@ def load_config_file(self, suppress_errors=True):
def _find_subcommand(self, name):
name = '{}-{}'.format(self.name, name)
return which(name)

@property
def _dispatching(self):
"""Return whether we are dispatching to another command
or running ourselves.
"""
return bool(self.generate_config or self.subapp or self.subcommand)

subcommand = Unicode()

@catch_config_error
def initialize(self, argv=None):
# don't hook up crash handler before parsing command-line
Expand Down Expand Up @@ -238,15 +248,15 @@ def start(self):
if self.subcommand:
os.execv(self.subcommand, [self.subcommand] + self.argv[1:])
raise NoStart()

if self.subapp:
self.subapp.start()
raise NoStart()

if self.generate_config:
self.write_default_config()
raise NoStart()

@classmethod
def launch_instance(cls, argv=None, **kwargs):
"""Launch an instance of a Jupyter Application"""
Expand Down

0 comments on commit 9a97bdc

Please sign in to comment.