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

Expose loaded config files, make load idempotent #522

Merged
merged 3 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@ def _load_config_files(cls, basefilename, path=None, log=None, raise_config_file
loaded.append(config)
filenames.append(loader.full_filename)

@property
def loaded_config_files(self):
"""Currently loaded configuration files"""
return self._loaded_config_files[:]

@catch_config_error
def load_config_file(self, filename, path=None):
"""Load config files by filename and path."""
Expand All @@ -763,7 +768,8 @@ def load_config_file(self, filename, path=None):
raise_config_file_errors=self.raise_config_file_errors,
):
new_config.merge(config)
self._loaded_config_files.append(filename)
if filename not in self._loaded_config_files: # only add if not present (support reloads)
kevin-bates marked this conversation as resolved.
Show resolved Hide resolved
self._loaded_config_files.append(filename)
# add self.cli_config to preserve CLI config priority
new_config.merge(self.cli_config)
self.update_config(new_config)
Expand Down
42 changes: 42 additions & 0 deletions traitlets/config/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,48 @@ def test_subcommands_instanciation(self):
self.assertIs(app.subapp.parent, app)
self.assertIs(app.subapp.subapp.parent, app.subapp) # Set by factory.

def test_loaded_config_files(self):
app = MyApp()
app.log = logging.getLogger()
name = 'config.py'
with TemporaryDirectory('_1') as td1:
config_file = pjoin(td1, name)
with open(config_file, 'w') as f:
f.writelines([
"c.MyApp.running = True\n"
])

app.load_config_file(name, path=[td1])
self.assertEqual(len(app.loaded_config_files), 1)
self.assertEquals(app.loaded_config_files[0], config_file)

app.start()
self.assertEqual(app.running, True)

# emulate an app that allows dynamic updates and update config file
with open(config_file, 'w') as f:
f.writelines([
"c.MyApp.running = False\n"
])

# reload and verify update, and that loaded_configs was not increased
app.load_config_file(name, path=[td1])
self.assertEqual(len(app.loaded_config_files), 1)
self.assertEqual(app.running, False)

# Attempt to update, ensure error...
with self.assertRaises(AttributeError):
app.loaded_config_files = "/foo"

# ensure it can't be udpated via append
app.loaded_config_files.append("/bar")
self.assertEqual(len(app.loaded_config_files), 1)

# repeat to ensure no unexpected changes occurred
app.load_config_file(name, path=[td1])
self.assertEqual(len(app.loaded_config_files), 1)
self.assertEqual(app.running, False)


class Root(Application):
subcommands = {
Expand Down