Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add conf.d config-loading #242

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,18 +581,31 @@ def _load_config_files(cls, basefilename, path=None, log=None, raise_config_file
yield each config object in turn.
"""

def new_loader(name, path):
if name.endswith('.py'):
return cls.python_config_loader_class(name, path=path, log=log)
elif name.endswith('.json'):
return cls.json_config_loader_class(name, path=path, log=log)

if not isinstance(path, list):
path = [path]
for path in path[::-1]:
# path list is in descending priority order, so load files backwards:
pyloader = cls.python_config_loader_class(basefilename+'.py', path=path, log=log)
if log:
log.debug("Looking for %s in %s", basefilename, path or os.getcwd())
jsonloader = cls.json_config_loader_class(basefilename+'.json', path=path, log=log)
pyloader = new_loader(basefilename + '.py', path=path)
jsonloader = new_loader(basefilename + '.json', path=path)
loaders = [pyloader, jsonloader]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question not strictly related to the core functionality of this PR
Why did you changed the separate cls.XXX_config_loader_class() calls with new_loader()?

I also need to change the abov 3 lines,
but for adding a new loader (i.e. YAML) without overriding and reimplementing _load_config_files(),
like that:

-            pyloader = cls.python_config_loader_class(basefilename+'.py', path=path, log=log)
             if log:
                 log.debug("Looking for %s in %s", basefilename, path or os.getcwd())
-            jsonloader = cls.json_config_loader_class(basefilename+'.json', path=path, log=log)
+           loaders = cls._make_loaders(basefilename, path=path, log=log)
+           log.debug("Looking for %s in %s", basefilename, path or os.getcwd())

You may implement _make_loaders() either with new_loader() or cls.XXX_config_loader_class() calls.
Then adding a new loader requires only to overrd this trivial method.

# load conf.d/config files in lorder
conf_d = os.path.join(path, basefilename + '.d')
if os.path.isdir(conf_d):
for filename in sorted(os.listdir(conf_d)):
if filename.endswith(('.py', '.json')):
loaders.append(new_loader(filename, path=conf_d))
config = None
loaded = []
filenames = []
for loader in [pyloader, jsonloader]:
for loader in loaders:
try:
config = loader.load_config()
except ConfigFileNotFound:
Expand Down