manager.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Manager to read and modify frontend config data in JSON files.
  2. """
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import os.path
  6. from notebook.config_manager import BaseJSONConfigManager, recursive_update
  7. from jupyter_core.paths import jupyter_config_dir, jupyter_config_path
  8. from traitlets import Unicode, Instance, List, observe, default
  9. from traitlets.config import LoggingConfigurable
  10. class ConfigManager(LoggingConfigurable):
  11. """Config Manager used for storing notebook frontend config"""
  12. # Public API
  13. def get(self, section_name):
  14. """Get the config from all config sections."""
  15. config = {}
  16. # step through back to front, to ensure front of the list is top priority
  17. for p in self.read_config_path[::-1]:
  18. cm = BaseJSONConfigManager(config_dir=p)
  19. recursive_update(config, cm.get(section_name))
  20. return config
  21. def set(self, section_name, data):
  22. """Set the config only to the user's config."""
  23. return self.write_config_manager.set(section_name, data)
  24. def update(self, section_name, new_data):
  25. """Update the config only to the user's config."""
  26. return self.write_config_manager.update(section_name, new_data)
  27. # Private API
  28. read_config_path = List(Unicode())
  29. @default('read_config_path')
  30. def _default_read_config_path(self):
  31. return [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]
  32. write_config_dir = Unicode()
  33. @default('write_config_dir')
  34. def _default_write_config_dir(self):
  35. return os.path.join(jupyter_config_dir(), 'nbconfig')
  36. write_config_manager = Instance(BaseJSONConfigManager)
  37. @default('write_config_manager')
  38. def _default_write_config_manager(self):
  39. return BaseJSONConfigManager(config_dir=self.write_config_dir)
  40. @observe('write_config_dir')
  41. def _update_write_config_dir(self, change):
  42. self.write_config_manager = BaseJSONConfigManager(config_dir=self.write_config_dir)