Skip to content

Commit

Permalink
Merge pull request #522 from kevin-bates/expose-loaded-config-files
Browse files Browse the repository at this point in the history
Expose loaded config files, make load idempotent
  • Loading branch information
rmorshea authored Jun 13, 2019
2 parents c5c9166 + 11b5610 commit c95f848
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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 to list of loaded files if not previously loaded
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

0 comments on commit c95f848

Please sign in to comment.