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