##// END OF EJS Templates
Put frontend config files in profile_foo/nbconfig/ subdir
Thomas Kluyver -
Show More
@@ -5,6 +5,7 b''
5 import json
5 import json
6 import os
6 import os
7 import io
7 import io
8 import errno
8 from tornado import web
9 from tornado import web
9
10
10 from IPython.utils.py3compat import PY3
11 from IPython.utils.py3compat import PY3
@@ -33,8 +34,19 b' def recursive_update(target, new):'
33 class ConfigHandler(IPythonHandler):
34 class ConfigHandler(IPythonHandler):
34 SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH')
35 SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH')
35
36
37 @property
38 def config_dir(self):
39 return os.path.join(self.profile_dir, 'nbconfig')
40
41 def ensure_config_dir_exists(self):
42 try:
43 os.mkdir(self.config_dir, 0o755)
44 except OSError as e:
45 if e.errno != errno.EEXIST:
46 raise
47
36 def file_name(self, section_name):
48 def file_name(self, section_name):
37 return os.path.join(self.profile_dir, 'nb_%s_config.json' % section_name)
49 return os.path.join(self.config_dir, section_name+'.json')
38
50
39 @web.authenticated
51 @web.authenticated
40 @json_errors
52 @json_errors
@@ -52,6 +64,7 b' class ConfigHandler(IPythonHandler):'
52 def put(self, section_name):
64 def put(self, section_name):
53 self.get_json_body() # Will raise 400 if content is not valid JSON
65 self.get_json_body() # Will raise 400 if content is not valid JSON
54 filename = self.file_name(section_name)
66 filename = self.file_name(section_name)
67 self.ensure_config_dir_exists()
55 with open(filename, 'wb') as f:
68 with open(filename, 'wb') as f:
56 f.write(self.request.body)
69 f.write(self.request.body)
57 self.set_status(204)
70 self.set_status(204)
@@ -69,6 +82,7 b' class ConfigHandler(IPythonHandler):'
69 update = self.get_json_body()
82 update = self.get_json_body()
70 recursive_update(section, update)
83 recursive_update(section, update)
71
84
85 self.ensure_config_dir_exists()
72 if PY3:
86 if PY3:
73 f = io.open(filename, 'w', encoding='utf-8')
87 f = io.open(filename, 'w', encoding='utf-8')
74 else:
88 else:
General Comments 0
You need to be logged in to leave comments. Login now