-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
frontend/extension-config: allow default json files in a .d directory #3116
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89b32e4
allow default json files in a .d directory, original PR: ipython/trai…
maartenbreddels 5a48049
make unittest non-pytest compatible
maartenbreddels 772544e
renamed manager.py to config_manager.py and added more docstrings
maartenbreddels 46e2534
py27 compat importing
maartenbreddels 49a66b8
log.debug the paths that are used
maartenbreddels ab24db7
update copyright header + encoding
maartenbreddels c73b711
added docstrings
maartenbreddels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"""Manager to read and modify config data in JSON files. | ||
""" | ||
# Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
import errno | ||
import glob | ||
import io | ||
import json | ||
import os | ||
|
||
from six import PY3 | ||
from traitlets.config import LoggingConfigurable | ||
from traitlets.traitlets import Unicode, Bool | ||
|
||
|
||
def recursive_update(target, new): | ||
"""Recursively update one dictionary using another. | ||
|
||
None values will delete their keys. | ||
""" | ||
for k, v in new.items(): | ||
if isinstance(v, dict): | ||
if k not in target: | ||
target[k] = {} | ||
recursive_update(target[k], v) | ||
if not target[k]: | ||
# Prune empty subdicts | ||
del target[k] | ||
|
||
elif v is None: | ||
target.pop(k, None) | ||
|
||
else: | ||
target[k] = v | ||
|
||
|
||
class BaseJSONConfigManager(LoggingConfigurable): | ||
"""General JSON config manager | ||
|
||
Deals with persisting/storing config in a json file with optionally | ||
default values in a {section_name}.d directory. | ||
""" | ||
|
||
config_dir = Unicode('.') | ||
read_directory = Bool(True) | ||
|
||
def ensure_config_dir_exists(self): | ||
try: | ||
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. Mind adding short docstrings for these methods? |
||
os.makedirs(self.config_dir, 0o755) | ||
except OSError as e: | ||
if e.errno != errno.EEXIST: | ||
raise | ||
|
||
def file_name(self, section_name): | ||
return os.path.join(self.config_dir, section_name+'.json') | ||
|
||
def directory(self, section_name): | ||
return os.path.join(self.config_dir, section_name+'.d') | ||
|
||
def get(self, section_name): | ||
"""Retrieve the config data for the specified section. | ||
|
||
Returns the data as a dictionary, or an empty dictionary if the file | ||
doesn't exist. | ||
""" | ||
paths = [self.file_name(section_name)] | ||
if self.read_directory: | ||
pattern = os.path.join(self.directory(section_name), '*.json') | ||
# These json files should be processed first so that the | ||
# {section_name}.json take precedence. | ||
# The idea behind this is that installing a Python package may | ||
# put a json file somewhere in the a .d directory, while the | ||
# .json file is probably a user configuration. | ||
paths = sorted(glob.glob(pattern)) + paths | ||
data = {} | ||
for path in paths: | ||
if os.path.isfile(path): | ||
with io.open(path, encoding='utf-8') as f: | ||
recursive_update(data, json.load(f)) | ||
return data | ||
|
||
def set(self, section_name, data): | ||
"""Store the given config data. | ||
""" | ||
filename = self.file_name(section_name) | ||
self.ensure_config_dir_exists() | ||
|
||
if PY3: | ||
f = io.open(filename, 'w', encoding='utf-8') | ||
else: | ||
f = open(filename, 'wb') | ||
with f: | ||
json.dump(data, f, indent=2) | ||
|
||
def update(self, section_name, new_data): | ||
"""Modify the config section by recursively updating it with new_data. | ||
|
||
Returns the modified config data as a dictionary. | ||
""" | ||
data = self.get(section_name) | ||
recursive_update(data, new_data) | ||
self.set(section_name, data) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from notebook.manager import BaseJSONConfigManager | ||
|
||
|
||
def test_json(): | ||
tmpdir = tempfile.mkdtemp() | ||
try: | ||
with open(os.path.join(tmpdir, 'foo.json'), 'w') as f: | ||
json.dump(dict(a=1), f) | ||
# also make a foo.d/ directory with multiple json files | ||
os.makedirs(os.path.join(tmpdir, 'foo.d')) | ||
with open(os.path.join(tmpdir, 'foo.d', 'a.json'), 'w') as f: | ||
json.dump(dict(a=2, b=1), f) | ||
with open(os.path.join(tmpdir, 'foo.d', 'b.json'), 'w') as f: | ||
json.dump(dict(a=3, b=2, c=3), f) | ||
manager = BaseJSONConfigManager(config_dir=tmpdir, read_directory=False) | ||
data = manager.get('foo') | ||
assert 'a' in data | ||
assert 'b' not in data | ||
assert 'c' not in data | ||
assert data['a'] == 1 | ||
|
||
manager = BaseJSONConfigManager(config_dir=tmpdir, read_directory=True) | ||
data = manager.get('foo') | ||
assert 'a' in data | ||
assert 'b' in data | ||
assert 'c' in data | ||
# files should be read in order foo.d/a.json foo.d/b.json foo.json | ||
assert data['a'] == 1 | ||
assert data['b'] == 2 | ||
assert data['c'] == 3 | ||
finally: | ||
shutil.rmtree(tmpdir) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe call this
config_manager.py
?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.
yeah, wasn't sure where to put it also, but wanted to start with a clean dup of the original PR. Shall I keep it in the root dir?
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.
I think so