Skip to content

Commit

Permalink
Merge minrk/conf.d-->ankostis/cfg_loader_fact to render ipython#242 e…
Browse files Browse the repository at this point in the history
…xtensible

- Deprecate Application class-attribute:
  - python_config_loader_class
  - json_config_loader_class
  replaced by `supported_cfg_loaders` ordered mapping:
    {.ext: loader-class}
- To extend with a new config-file, simply add its loader class in the
`supported_cfg_loaders` mapping.
- Merge-conflicts resolved in Application.
  • Loading branch information
ankostis committed Feb 4, 2018
2 parents 98ca7c1 + 1786510 commit a7e43e2
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,16 @@ class Application(SingletonConfigurable):
keyvalue_description = Unicode(keyvalue_description)
subcommand_description = Unicode(subcommand_description)

#: .. deprecated:: 5.0.0
#: Replaced by `supported_cfg_loaders` ordered-map.
python_config_loader_class = PyFileConfigLoader
json_config_loader_class = JSONFileConfigLoader

supported_cfg_loaders = OrderedDict([
('.py', PyFileConfigLoader),
('.json', JSONFileConfigLoader)])


# The usage and example string that goes at the end of the help string.
examples = Unicode()

Expand Down Expand Up @@ -705,12 +712,12 @@ def parse_command_line(self, argv=None):
self.extra_args = loader.extra_args

@classmethod
def _make_loaders(cls, basefilename, path=None, **kw):
def _make_loader_for_ext(cls, config_fname, path, **kw):
"""
Builds the list of config-loaders to try.
:param str basefilename:
The config filename without extension (`.py` or `.json`).
:param str config_fname:
The config filename with extension (e.g. '.py', '.json').
:param path:
The dirpath to search for the config file on, or a sequence of
paths to try in order.
Expand All @@ -720,12 +727,10 @@ def _make_loaders(cls, basefilename, path=None, **kw):
:return:
a list, currently `python-load, json-loader].
"""
pyloader = cls.python_config_loader_class(basefilename+'.py',
path=path, **kw)
jsonloader = cls.json_config_loader_class(basefilename+'.json',
path=path, **kw)

return [pyloader, jsonloader]
for ext, loader in cls.supported_cfg_loaders.items():
if config_fname.endswith(ext):
return loader(config_fname, path, **kw)
raise AssertionError("Unknown config-file extension: %s" % config_fname)

@classmethod
def _load_config_files(cls, basefilename, path=None, log=None, raise_config_file_errors=False):
Expand All @@ -740,11 +745,21 @@ def _load_config_files(cls, basefilename, path=None, log=None, raise_config_file
# path list is in descending priority order, so load files backwards:
if log:
log.debug("Looking for %s in %s", basefilename, path or os.getcwd())
loaders = cls._make_loaders(basefilename, path=path, log=log)
loaders = [cls._make_loader_for_ext(basefilename + ext,
path=path, log=log)
for ext in cls.supported_cfg_loaders]
# load conf.d/config files in lorder
conf_d = os.path.join(path, basefilename + '.d')
if os.path.isdir(conf_d):
supported_cfg_extensions = tuple(cls.supported_cfg_loaders)
for filename in sorted(os.listdir(conf_d)):
if filename.endswith(supported_cfg_extensions):
loaders.append(cls._make_loader_for_ext(filename,
path=conf_d, log=log))
config = None
loaded = []
filenames = []
for loader in loaders:
config = None
try:
config = loader.load_config()
except ConfigFileNotFound:
Expand Down

0 comments on commit a7e43e2

Please sign in to comment.