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