diff --git a/traitlets/config/application.py b/traitlets/config/application.py index b439822c..e7c28e98 100644 --- a/traitlets/config/application.py +++ b/traitlets/config/application.py @@ -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.""" @@ -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) diff --git a/traitlets/config/tests/test_application.py b/traitlets/config/tests/test_application.py index bf66eb71..30b89178 100644 --- a/traitlets/config/tests/test_application.py +++ b/traitlets/config/tests/test_application.py @@ -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 = {