Show More
@@ -92,18 +92,11 b' class ContentsHandler(IPythonHandler):' | |||||
92 | model = self.contents_manager.new(model, path) |
|
92 | model = self.contents_manager.new(model, path) | |
93 | self.set_status(201) |
|
93 | self.set_status(201) | |
94 | self._finish_model(model) |
|
94 | self._finish_model(model) | |
95 |
|
||||
96 | def _new(self, path, type='notebook', ext=''): |
|
|||
97 | """Create an empty file or directory in directory specified by path |
|
|||
98 |
|
95 | |||
99 | ContentsManager picks the filename. |
|
96 | def _new_untitled(self, path, type='', ext=''): | |
100 | """ |
|
97 | """Create a new, empty untitled entity""" | |
101 | self.log.info(u"Creating new %s in %s", type or 'file', path) |
|
98 | self.log.info(u"Creating new %s in %s", type or 'file', path) | |
102 | if type: |
|
99 | model = self.contents_manager.new_untitled(path=path, type=type, ext=ext) | |
103 | model = {'type': type} |
|
|||
104 | else: |
|
|||
105 | model = None |
|
|||
106 | model = self.contents_manager.new(model, path=path, ext=ext) |
|
|||
107 | self.set_status(201) |
|
100 | self.set_status(201) | |
108 | self._finish_model(model) |
|
101 | self._finish_model(model) | |
109 |
|
102 | |||
@@ -140,13 +133,13 b' class ContentsHandler(IPythonHandler):' | |||||
140 | if model is not None: |
|
133 | if model is not None: | |
141 | copy_from = model.get('copy_from') |
|
134 | copy_from = model.get('copy_from') | |
142 | ext = model.get('ext', '') |
|
135 | ext = model.get('ext', '') | |
143 | type = model.get('type') |
|
136 | type = model.get('type', '') | |
144 | if copy_from: |
|
137 | if copy_from: | |
145 | self._copy(copy_from, path) |
|
138 | self._copy(copy_from, path) | |
146 | else: |
|
139 | else: | |
147 | self._new(path, type=type, ext=ext) |
|
140 | self._new_untitled(path, type=type, ext=ext) | |
148 | else: |
|
141 | else: | |
149 | self._new(path) |
|
142 | self._new_untitled(path) | |
150 |
|
143 | |||
151 | @web.authenticated |
|
144 | @web.authenticated | |
152 | @json_errors |
|
145 | @json_errors | |
@@ -170,7 +163,7 b' class ContentsHandler(IPythonHandler):' | |||||
170 | else: |
|
163 | else: | |
171 | self._upload(model, path) |
|
164 | self._upload(model, path) | |
172 | else: |
|
165 | else: | |
173 | self._new(path) |
|
166 | self._new_untitled(path) | |
174 |
|
167 | |||
175 | @web.authenticated |
|
168 | @web.authenticated | |
176 | @json_errors |
|
169 | @json_errors |
@@ -221,28 +221,60 b' class ContentsManager(LoggingConfigurable):' | |||||
221 | e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'), |
|
221 | e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'), | |
222 | ) |
|
222 | ) | |
223 | return model |
|
223 | return model | |
224 |
|
224 | |||
225 |
def new(self, |
|
225 | def new_untitled(self, path='', type='', ext=''): | |
|
226 | """Create a new untitled file or directory in path | |||
|
227 | ||||
|
228 | path must be a directory | |||
|
229 | ||||
|
230 | File extension can be specified. | |||
|
231 | ||||
|
232 | Use `new` to create files with a fully specified path (including filename). | |||
|
233 | """ | |||
|
234 | path = path.strip('/') | |||
|
235 | if not self.dir_exists(path): | |||
|
236 | raise HTTPError(404, 'No such directory: %s' % path) | |||
|
237 | ||||
|
238 | model = {} | |||
|
239 | if type: | |||
|
240 | model['type'] = type | |||
|
241 | ||||
|
242 | if ext == '.ipynb': | |||
|
243 | model.setdefault('type', 'notebook') | |||
|
244 | else: | |||
|
245 | model.setdefault('type', 'file') | |||
|
246 | ||||
|
247 | if model['type'] == 'directory': | |||
|
248 | untitled = self.untitled_directory | |||
|
249 | elif model['type'] == 'notebook': | |||
|
250 | untitled = self.untitled_notebook | |||
|
251 | ext = '.ipynb' | |||
|
252 | elif model['type'] == 'file': | |||
|
253 | untitled = self.untitled_file | |||
|
254 | else: | |||
|
255 | raise HTTPError(400, "Unexpected model type: %r" % model['type']) | |||
|
256 | ||||
|
257 | name = self.increment_filename(untitled + ext, path) | |||
|
258 | path = u'{0}/{1}'.format(path, name) | |||
|
259 | return self.new(model, path) | |||
|
260 | ||||
|
261 | def new(self, model=None, path=''): | |||
226 | """Create a new file or directory and return its model with no content. |
|
262 | """Create a new file or directory and return its model with no content. | |
227 |
|
263 | |||
228 | If path is a directory, a new untitled file/directory is created in path. |
|
264 | To create a new untitled entity in a directory, use `new_untitled`. | |
229 | Otherwise, a new file/directory is created exactly at path. |
|
|||
230 | """ |
|
265 | """ | |
231 | path = path.strip('/') |
|
266 | path = path.strip('/') | |
232 | if model is None: |
|
267 | if model is None: | |
233 | model = {} |
|
268 | model = {} | |
234 | else: |
|
|||
235 | model.pop('path', None) |
|
|||
236 |
|
269 | |||
237 |
if |
|
270 | if path.endswith('.ipynb'): | |
238 | model.setdefault('type', 'file') |
|
|||
239 | else: |
|
|||
240 | model.setdefault('type', 'notebook') |
|
271 | model.setdefault('type', 'notebook') | |
|
272 | else: | |||
|
273 | model.setdefault('type', 'file') | |||
241 |
|
274 | |||
242 | # no content, not a directory, so fill out new-file model |
|
275 | # no content, not a directory, so fill out new-file model | |
243 | if 'content' not in model and model['type'] != 'directory': |
|
276 | if 'content' not in model and model['type'] != 'directory': | |
244 | if model['type'] == 'notebook': |
|
277 | if model['type'] == 'notebook': | |
245 | ext = '.ipynb' |
|
|||
246 | model['content'] = new_notebook() |
|
278 | model['content'] = new_notebook() | |
247 | model['format'] = 'json' |
|
279 | model['format'] = 'json' | |
248 | else: |
|
280 | else: | |
@@ -250,19 +282,6 b' class ContentsManager(LoggingConfigurable):' | |||||
250 | model['type'] = 'file' |
|
282 | model['type'] = 'file' | |
251 | model['format'] = 'text' |
|
283 | model['format'] = 'text' | |
252 |
|
284 | |||
253 | # if path is a directory, create an untitled file or directory |
|
|||
254 | if self.dir_exists(path): |
|
|||
255 | if model['type'] == 'directory': |
|
|||
256 | untitled = self.untitled_directory |
|
|||
257 | elif model['type'] == 'notebook': |
|
|||
258 | untitled = self.untitled_notebook |
|
|||
259 | elif model['type'] == 'file': |
|
|||
260 | untitled = self.untitled_file |
|
|||
261 | else: |
|
|||
262 | raise HTTPError(400, "Unexpected model type: %r" % model['type']) |
|
|||
263 |
|
||||
264 | name = self.increment_filename(untitled + ext, path) |
|
|||
265 | path = u'{0}/{1}'.format(path, name) |
|
|||
266 | model = self.save(model, path) |
|
285 | model = self.save(model, path) | |
267 | return model |
|
286 | return model | |
268 |
|
287 |
@@ -49,7 +49,7 b' class API(object):' | |||||
49 | def read(self, path): |
|
49 | def read(self, path): | |
50 | return self._req('GET', path) |
|
50 | return self._req('GET', path) | |
51 |
|
51 | |||
52 |
def create_untitled(self, path='/', ext= |
|
52 | def create_untitled(self, path='/', ext='.ipynb'): | |
53 | body = None |
|
53 | body = None | |
54 | if ext: |
|
54 | if ext: | |
55 | body = json.dumps({'ext': ext}) |
|
55 | body = json.dumps({'ext': ext}) |
@@ -101,7 +101,7 b' class TestContentsManager(TestCase):' | |||||
101 |
|
101 | |||
102 | def new_notebook(self): |
|
102 | def new_notebook(self): | |
103 | cm = self.contents_manager |
|
103 | cm = self.contents_manager | |
104 | model = cm.new() |
|
104 | model = cm.new_untitled(type='notebook') | |
105 | name = model['name'] |
|
105 | name = model['name'] | |
106 | path = model['path'] |
|
106 | path = model['path'] | |
107 |
|
107 | |||
@@ -112,30 +112,42 b' class TestContentsManager(TestCase):' | |||||
112 | cm.save(full_model, path) |
|
112 | cm.save(full_model, path) | |
113 | return nb, name, path |
|
113 | return nb, name, path | |
114 |
|
114 | |||
115 | def test_new(self): |
|
115 | def test_new_untitled(self): | |
116 | cm = self.contents_manager |
|
116 | cm = self.contents_manager | |
117 | # Test in root directory |
|
117 | # Test in root directory | |
118 | model = cm.new() |
|
118 | model = cm.new_untitled(type='notebook') | |
119 | assert isinstance(model, dict) |
|
119 | assert isinstance(model, dict) | |
120 | self.assertIn('name', model) |
|
120 | self.assertIn('name', model) | |
121 | self.assertIn('path', model) |
|
121 | self.assertIn('path', model) | |
|
122 | self.assertIn('type', model) | |||
|
123 | self.assertEqual(model['type'], 'notebook') | |||
122 | self.assertEqual(model['name'], 'Untitled0.ipynb') |
|
124 | self.assertEqual(model['name'], 'Untitled0.ipynb') | |
123 | self.assertEqual(model['path'], 'Untitled0.ipynb') |
|
125 | self.assertEqual(model['path'], 'Untitled0.ipynb') | |
124 |
|
126 | |||
125 | # Test in sub-directory |
|
127 | # Test in sub-directory | |
126 | sub_dir = '/foo/' |
|
128 | model = cm.new_untitled(type='directory') | |
127 | self.make_dir(cm.root_dir, 'foo') |
|
|||
128 | model = cm.new(path=sub_dir) |
|
|||
129 | assert isinstance(model, dict) |
|
129 | assert isinstance(model, dict) | |
130 | self.assertIn('name', model) |
|
130 | self.assertIn('name', model) | |
131 | self.assertIn('path', model) |
|
131 | self.assertIn('path', model) | |
132 | self.assertEqual(model['name'], 'Untitled0.ipynb') |
|
132 | self.assertIn('type', model) | |
133 |
self.assertEqual(model[' |
|
133 | self.assertEqual(model['type'], 'directory') | |
|
134 | self.assertEqual(model['name'], 'Untitled Folder0') | |||
|
135 | self.assertEqual(model['path'], 'Untitled Folder0') | |||
|
136 | sub_dir = model['path'] | |||
|
137 | ||||
|
138 | model = cm.new_untitled(path=sub_dir) | |||
|
139 | assert isinstance(model, dict) | |||
|
140 | self.assertIn('name', model) | |||
|
141 | self.assertIn('path', model) | |||
|
142 | self.assertIn('type', model) | |||
|
143 | self.assertEqual(model['type'], 'file') | |||
|
144 | self.assertEqual(model['name'], 'untitled0') | |||
|
145 | self.assertEqual(model['path'], '%s/untitled0' % sub_dir) | |||
134 |
|
146 | |||
135 | def test_get(self): |
|
147 | def test_get(self): | |
136 | cm = self.contents_manager |
|
148 | cm = self.contents_manager | |
137 | # Create a notebook |
|
149 | # Create a notebook | |
138 | model = cm.new() |
|
150 | model = cm.new_untitled(type='notebook') | |
139 | name = model['name'] |
|
151 | name = model['name'] | |
140 | path = model['path'] |
|
152 | path = model['path'] | |
141 |
|
153 | |||
@@ -150,7 +162,7 b' class TestContentsManager(TestCase):' | |||||
150 | # Test in sub-directory |
|
162 | # Test in sub-directory | |
151 | sub_dir = '/foo/' |
|
163 | sub_dir = '/foo/' | |
152 | self.make_dir(cm.root_dir, 'foo') |
|
164 | self.make_dir(cm.root_dir, 'foo') | |
153 | model = cm.new(path=sub_dir, ext='.ipynb') |
|
165 | model = cm.new_untitled(path=sub_dir, ext='.ipynb') | |
154 | model2 = cm.get_model(sub_dir + name) |
|
166 | model2 = cm.get_model(sub_dir + name) | |
155 | assert isinstance(model2, dict) |
|
167 | assert isinstance(model2, dict) | |
156 | self.assertIn('name', model2) |
|
168 | self.assertIn('name', model2) | |
@@ -165,7 +177,7 b' class TestContentsManager(TestCase):' | |||||
165 | path = 'test bad symlink' |
|
177 | path = 'test bad symlink' | |
166 | os_path = self.make_dir(cm.root_dir, path) |
|
178 | os_path = self.make_dir(cm.root_dir, path) | |
167 |
|
179 | |||
168 | file_model = cm.new(path=path, ext='.txt') |
|
180 | file_model = cm.new_untitled(path=path, ext='.txt') | |
169 |
|
181 | |||
170 | # create a broken symlink |
|
182 | # create a broken symlink | |
171 | os.symlink("target", os.path.join(os_path, "bad symlink")) |
|
183 | os.symlink("target", os.path.join(os_path, "bad symlink")) | |
@@ -180,12 +192,11 b' class TestContentsManager(TestCase):' | |||||
180 | path = '{0}/{1}'.format(parent, name) |
|
192 | path = '{0}/{1}'.format(parent, name) | |
181 | os_path = self.make_dir(cm.root_dir, parent) |
|
193 | os_path = self.make_dir(cm.root_dir, parent) | |
182 |
|
194 | |||
183 |
file_model = cm.new(path=parent |
|
195 | file_model = cm.new(path=parent + '/zfoo.txt') | |
184 |
|
196 | |||
185 | # create a good symlink |
|
197 | # create a good symlink | |
186 | os.symlink(file_model['name'], os.path.join(os_path, name)) |
|
198 | os.symlink(file_model['name'], os.path.join(os_path, name)) | |
187 | symlink_model = cm.get_model(path, content=False) |
|
199 | symlink_model = cm.get_model(path, content=False) | |
188 |
|
||||
189 | dir_model = cm.get_model(parent) |
|
200 | dir_model = cm.get_model(parent) | |
190 | self.assertEqual( |
|
201 | self.assertEqual( | |
191 | sorted(dir_model['content'], key=lambda x: x['name']), |
|
202 | sorted(dir_model['content'], key=lambda x: x['name']), | |
@@ -195,7 +206,7 b' class TestContentsManager(TestCase):' | |||||
195 | def test_update(self): |
|
206 | def test_update(self): | |
196 | cm = self.contents_manager |
|
207 | cm = self.contents_manager | |
197 | # Create a notebook |
|
208 | # Create a notebook | |
198 | model = cm.new() |
|
209 | model = cm.new_untitled(type='notebook') | |
199 | name = model['name'] |
|
210 | name = model['name'] | |
200 | path = model['path'] |
|
211 | path = model['path'] | |
201 |
|
212 | |||
@@ -214,7 +225,7 b' class TestContentsManager(TestCase):' | |||||
214 | # Create a directory and notebook in that directory |
|
225 | # Create a directory and notebook in that directory | |
215 | sub_dir = '/foo/' |
|
226 | sub_dir = '/foo/' | |
216 | self.make_dir(cm.root_dir, 'foo') |
|
227 | self.make_dir(cm.root_dir, 'foo') | |
217 |
model = cm.new( |
|
228 | model = cm.new_untitled(path=sub_dir, type='notebook') | |
218 | name = model['name'] |
|
229 | name = model['name'] | |
219 | path = model['path'] |
|
230 | path = model['path'] | |
220 |
|
231 | |||
@@ -234,7 +245,7 b' class TestContentsManager(TestCase):' | |||||
234 | def test_save(self): |
|
245 | def test_save(self): | |
235 | cm = self.contents_manager |
|
246 | cm = self.contents_manager | |
236 | # Create a notebook |
|
247 | # Create a notebook | |
237 | model = cm.new() |
|
248 | model = cm.new_untitled(type='notebook') | |
238 | name = model['name'] |
|
249 | name = model['name'] | |
239 | path = model['path'] |
|
250 | path = model['path'] | |
240 |
|
251 | |||
@@ -253,7 +264,7 b' class TestContentsManager(TestCase):' | |||||
253 | # Create a directory and notebook in that directory |
|
264 | # Create a directory and notebook in that directory | |
254 | sub_dir = '/foo/' |
|
265 | sub_dir = '/foo/' | |
255 | self.make_dir(cm.root_dir, 'foo') |
|
266 | self.make_dir(cm.root_dir, 'foo') | |
256 |
model = cm.new( |
|
267 | model = cm.new_untitled(path=sub_dir, type='notebook') | |
257 | name = model['name'] |
|
268 | name = model['name'] | |
258 | path = model['path'] |
|
269 | path = model['path'] | |
259 | model = cm.get_model(path) |
|
270 | model = cm.get_model(path) |
General Comments 0
You need to be logged in to leave comments.
Login now