Show More
@@ -1,99 +1,99 b'' | |||
|
1 | 1 | """Manager to read and modify config data in JSON files. |
|
2 | 2 | """ |
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | import errno |
|
6 | 6 | import io |
|
7 | 7 | import json |
|
8 | 8 | import os |
|
9 | 9 | |
|
10 | 10 | from IPython.config import LoggingConfigurable |
|
11 | 11 | from IPython.utils.path import locate_profile |
|
12 | 12 | from IPython.utils.py3compat import PY3 |
|
13 | 13 | from IPython.utils.traitlets import Unicode |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | def recursive_update(target, new): |
|
17 | 17 | """Recursively update one dictionary using another. |
|
18 | 18 | |
|
19 | 19 | None values will delete their keys. |
|
20 | 20 | """ |
|
21 | 21 | for k, v in new.items(): |
|
22 | 22 | if isinstance(v, dict): |
|
23 | 23 | if k not in target: |
|
24 | 24 | target[k] = {} |
|
25 | 25 | recursive_update(target[k], v) |
|
26 | 26 | if not target[k]: |
|
27 | 27 | # Prune empty subdicts |
|
28 | 28 | del target[k] |
|
29 | 29 | |
|
30 | 30 | elif v is None: |
|
31 | 31 | target.pop(k, None) |
|
32 | 32 | |
|
33 | 33 | else: |
|
34 | 34 | target[k] = v |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | class BaseJSONConfigManager(LoggingConfigurable): |
|
38 | 38 | """General config manager |
|
39 | 39 | |
|
40 | 40 | Deals with persisting/storing config in a json file |
|
41 | 41 | in IPython profile |
|
42 | 42 | """ |
|
43 | 43 | |
|
44 | 44 | profile_dir = Unicode() |
|
45 | 45 | def _profile_dir_default(self): |
|
46 | 46 | return locate_profile() |
|
47 | 47 | |
|
48 | 48 | @property |
|
49 | 49 | def config_dir(self): |
|
50 | 50 | return self._config_dir() |
|
51 | 51 | |
|
52 | 52 | def _config_dir(self): |
|
53 | 53 | return self.profile_dir |
|
54 | 54 | |
|
55 | 55 | def ensure_config_dir_exists(self): |
|
56 | 56 | try: |
|
57 | 57 | os.mkdir(self.config_dir, 0o755) |
|
58 | 58 | except OSError as e: |
|
59 | 59 | if e.errno != errno.EEXIST: |
|
60 | 60 | raise |
|
61 | 61 | |
|
62 | 62 | def file_name(self, section_name): |
|
63 | 63 | return os.path.join(self.config_dir, section_name+'.json') |
|
64 | 64 | |
|
65 | 65 | def get(self, section_name): |
|
66 | 66 | """Retrieve the config data for the specified section. |
|
67 | 67 | |
|
68 | 68 | Returns the data as a dictionary, or an empty dictionary if the file |
|
69 | 69 | doesn't exist. |
|
70 | 70 | """ |
|
71 | 71 | filename = self.file_name(section_name) |
|
72 | 72 | if os.path.isfile(filename): |
|
73 | 73 | with io.open(filename, encoding='utf-8') as f: |
|
74 | 74 | return json.load(f) |
|
75 | 75 | else: |
|
76 | 76 | return {} |
|
77 | 77 | |
|
78 | 78 | def set(self, section_name, data): |
|
79 | 79 | """Store the given config data. |
|
80 | 80 | """ |
|
81 | 81 | filename = self.file_name(section_name) |
|
82 | 82 | self.ensure_config_dir_exists() |
|
83 | 83 | |
|
84 | 84 | if PY3: |
|
85 | 85 | f = io.open(filename, 'w', encoding='utf-8') |
|
86 | 86 | else: |
|
87 | 87 | f = open(filename, 'wb') |
|
88 | 88 | with f: |
|
89 | json.dump(data, f) | |
|
89 | json.dump(data, f, indent=2) | |
|
90 | 90 | |
|
91 | 91 | def update(self, section_name, new_data): |
|
92 | 92 | """Modify the config section by recursively updating it with new_data. |
|
93 | 93 | |
|
94 | 94 | Returns the modified config data as a dictionary. |
|
95 | 95 | """ |
|
96 | 96 | data = self.get(section_name) |
|
97 | 97 | recursive_update(data, new_data) |
|
98 | 98 | self.set(section_name, data) |
|
99 | 99 | return data |
@@ -1,14 +1,14 b'' | |||
|
1 | 1 | """Manager to read and modify frontend config data in JSON files. |
|
2 | 2 | """ |
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | import os |
|
7 | 7 | |
|
8 | 8 | from IPython.config.manager import BaseJSONConfigManager |
|
9 | 9 | |
|
10 | 10 | class ConfigManager(BaseJSONConfigManager): |
|
11 |
"""Config Manager use for storin |
|
|
11 | """Config Manager used for storing notebook frontend config""" | |
|
12 | 12 | |
|
13 | 13 | def _config_dir(self): |
|
14 | 14 | return os.path.join(self.profile_dir, 'nbconfig') |
General Comments 0
You need to be logged in to leave comments.
Login now