-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Add TOML support #649
base: main
Are you sure you want to change the base?
Add TOML support #649
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,11 @@ | |
import re | ||
import sys | ||
import json | ||
try: | ||
import toml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should likely now prefer py3.11+ stdlib's |
||
HAS_TOML = True | ||
except ImportError: | ||
HAS_TOML = None | ||
import warnings | ||
|
||
from ..utils import cast_unicode | ||
|
@@ -543,6 +548,7 @@ def _find_file(self): | |
"""Try to find the file by searching the paths.""" | ||
self.full_filename = filefind(self.filename, self.path) | ||
|
||
|
||
class JSONFileConfigLoader(FileConfigLoader): | ||
"""A JSON file loader for config | ||
|
||
|
@@ -598,6 +604,69 @@ def __exit__(self, exc_type, exc_value, traceback): | |
f.write(json_config) | ||
|
||
|
||
class TOMLFileConfigLoader(FileConfigLoader): | ||
"""A TOML file loader for config | ||
|
||
Can also act as a context manager that rewrite the configuration file to disk on exit. | ||
|
||
Example:: | ||
|
||
with TOMLFileConfigLoader('myapp.toml','/home/jupyter/configurations/') as c: | ||
c.MyNewConfigurable.new_value = 'Updated' | ||
|
||
""" | ||
|
||
def __init__(self, filename, **kw): | ||
"""Wrapper for checking (optional) toml module import""" | ||
if not HAS_TOML: | ||
raise ConfigLoaderError('toml module is not found. In order to use toml configuration' | ||
'files, please either install traitlets with corresponding option' | ||
' (pip install "traitlets[with-toml]") or simply add it with' | ||
'"pip install toml"') | ||
super(TOMLFileConfigLoader, self).__init__(filename, **kw) | ||
|
||
def load_config(self): | ||
"""Load the config from a file and return it as a Config object.""" | ||
self.clear() | ||
try: | ||
self._find_file() | ||
except IOError as e: | ||
raise ConfigFileNotFound(str(e)) | ||
dct = self._read_file_as_dict() | ||
self.config = self._convert_to_config(dct) | ||
return self.config | ||
|
||
def _read_file_as_dict(self): | ||
with open(self.full_filename) as f: | ||
return toml.load(f) | ||
|
||
def _convert_to_config(self, dictionary): | ||
if 'version' in dictionary: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't recall having seen |
||
version = dictionary.pop('version') | ||
else: | ||
version = 1 | ||
|
||
if version == 1: | ||
return Config(dictionary) | ||
else: | ||
raise ValueError('Unknown version of TOML config file: {version}'.format(version=version)) | ||
|
||
def __enter__(self): | ||
self.load_config() | ||
return self.config | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
""" | ||
Exit the context manager but do not handle any errors. | ||
|
||
In case of any error, we do not want to write the potentially broken | ||
configuration to disk. | ||
""" | ||
self.config.version = 1 | ||
toml_config = toml.dumps(self.config) | ||
with open(self.full_filename, 'w') as f: | ||
f.write(toml_config) | ||
|
||
|
||
class PyFileConfigLoader(FileConfigLoader): | ||
"""A config loader for pure python files. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would probably just add
tomli ; python_version < 3.11
toinstall_requires
instead