Show More
@@ -317,7 +317,7 b' class FileContentsManager(ContentsManager):' | |||
|
317 | 317 | self.validate_notebook_model(model) |
|
318 | 318 | return model |
|
319 | 319 | |
|
320 |
def get(self, path, content=True, type |
|
|
320 | def get(self, path, content=True, type=None, format=None): | |
|
321 | 321 | """ Takes a path for an entity and returns its model |
|
322 | 322 | |
|
323 | 323 | Parameters |
@@ -326,7 +326,7 b' class FileContentsManager(ContentsManager):' | |||
|
326 | 326 | the API path that describes the relative path for the target |
|
327 | 327 | content : bool |
|
328 | 328 | Whether to include the contents in the reply |
|
329 |
type |
|
|
329 | type : str, optional | |
|
330 | 330 | The requested type - 'file', 'notebook', or 'directory'. |
|
331 | 331 | Will raise HTTPError 400 if the content doesn't match. |
|
332 | 332 | format : str, optional |
@@ -346,14 +346,14 b' class FileContentsManager(ContentsManager):' | |||
|
346 | 346 | |
|
347 | 347 | os_path = self._get_os_path(path) |
|
348 | 348 | if os.path.isdir(os_path): |
|
349 |
if type |
|
|
349 | if type not in (None, 'directory'): | |
|
350 | 350 | raise web.HTTPError(400, |
|
351 |
u'%s is a directory, not a %s' % (path, type |
|
|
351 | u'%s is a directory, not a %s' % (path, type), reason='bad type') | |
|
352 | 352 | model = self._dir_model(path, content=content) |
|
353 |
elif type |
|
|
353 | elif type == 'notebook' or (type is None and path.endswith('.ipynb')): | |
|
354 | 354 | model = self._notebook_model(path, content=content) |
|
355 | 355 | else: |
|
356 |
if type |
|
|
356 | if type == 'directory': | |
|
357 | 357 | raise web.HTTPError(400, |
|
358 | 358 | u'%s is not a directory', reason='bad type') |
|
359 | 359 | model = self._file_model(path, content=content, format=format) |
@@ -59,15 +59,15 b' class ContentsHandler(IPythonHandler):' | |||
|
59 | 59 | of the files and directories it contains. |
|
60 | 60 | """ |
|
61 | 61 | path = path or '' |
|
62 |
type |
|
|
63 |
if type |
|
|
64 |
raise web.HTTPError(400, u'Type %r is invalid' % type |
|
|
62 | type = self.get_query_argument('type', default=None) | |
|
63 | if type not in {None, 'directory', 'file', 'notebook'}: | |
|
64 | raise web.HTTPError(400, u'Type %r is invalid' % type) | |
|
65 | 65 | |
|
66 | 66 | format = self.get_query_argument('format', default=None) |
|
67 | 67 | if format not in {None, 'text', 'base64'}: |
|
68 | 68 | raise web.HTTPError(400, u'Format %r is invalid' % format) |
|
69 | 69 | |
|
70 |
model = self.contents_manager.get(path=path, type |
|
|
70 | model = self.contents_manager.get(path=path, type=type, format=format) | |
|
71 | 71 | if model['type'] == 'directory': |
|
72 | 72 | # group listing by type, then by name (case-insensitive) |
|
73 | 73 | # FIXME: sorting should be done in the frontends |
@@ -137,7 +137,7 b' class ContentsManager(LoggingConfigurable):' | |||
|
137 | 137 | """ |
|
138 | 138 | return self.file_exists(path) or self.dir_exists(path) |
|
139 | 139 | |
|
140 |
def get(self, path, content=True, type |
|
|
140 | def get(self, path, content=True, type=None, format=None): | |
|
141 | 141 | """Get the model of a file or directory with or without content.""" |
|
142 | 142 | raise NotImplementedError('must be implemented in a subclass') |
|
143 | 143 |
@@ -46,10 +46,10 b' class API(object):' | |||
|
46 | 46 | def list(self, path='/'): |
|
47 | 47 | return self._req('GET', path) |
|
48 | 48 | |
|
49 |
def read(self, path, type |
|
|
49 | def read(self, path, type=None, format=None): | |
|
50 | 50 | params = {} |
|
51 |
if type |
|
|
52 |
params['type'] = type |
|
|
51 | if type is not None: | |
|
52 | params['type'] = type | |
|
53 | 53 | if format is not None: |
|
54 | 54 | params['format'] = format |
|
55 | 55 | return self._req('GET', path, params=params) |
@@ -250,7 +250,7 b' class APITest(NotebookTestBase):' | |||
|
250 | 250 | |
|
251 | 251 | # Specifying format=text should fail on a non-UTF-8 file |
|
252 | 252 | with assert_http_error(400): |
|
253 |
self.api.read('foo/bar/baz.blob', type |
|
|
253 | self.api.read('foo/bar/baz.blob', type='file', format='text') | |
|
254 | 254 | |
|
255 | 255 | def test_get_binary_file_contents(self): |
|
256 | 256 | for d, name in self.dirs_nbs: |
@@ -270,10 +270,10 b' class APITest(NotebookTestBase):' | |||
|
270 | 270 | |
|
271 | 271 | def test_get_bad_type(self): |
|
272 | 272 | with assert_http_error(400): |
|
273 |
self.api.read(u'unicodé', type |
|
|
273 | self.api.read(u'unicodé', type='file') # this is a directory | |
|
274 | 274 | |
|
275 | 275 | with assert_http_error(400): |
|
276 |
self.api.read(u'unicodé/innonascii.ipynb', type |
|
|
276 | self.api.read(u'unicodé/innonascii.ipynb', type='directory') | |
|
277 | 277 | |
|
278 | 278 | def _check_created(self, resp, path, type='notebook'): |
|
279 | 279 | self.assertEqual(resp.status_code, 201) |
@@ -159,13 +159,13 b' class TestContentsManager(TestCase):' | |||
|
159 | 159 | self.assertEqual(model['name'], name) |
|
160 | 160 | self.assertEqual(model['path'], path) |
|
161 | 161 | |
|
162 |
nb_as_file = cm.get(path, content=True, type |
|
|
162 | nb_as_file = cm.get(path, content=True, type='file') | |
|
163 | 163 | self.assertEqual(nb_as_file['path'], path) |
|
164 | 164 | self.assertEqual(nb_as_file['type'], 'file') |
|
165 | 165 | self.assertEqual(nb_as_file['format'], 'text') |
|
166 | 166 | self.assertNotIsInstance(nb_as_file['content'], dict) |
|
167 | 167 | |
|
168 |
nb_as_bin_file = cm.get(path, content=True, type |
|
|
168 | nb_as_bin_file = cm.get(path, content=True, type='file', format='base64') | |
|
169 | 169 | self.assertEqual(nb_as_bin_file['format'], 'base64') |
|
170 | 170 | |
|
171 | 171 | # Test in sub-directory |
@@ -185,7 +185,7 b' class TestContentsManager(TestCase):' | |||
|
185 | 185 | self.assertEqual(dirmodel['type'], 'directory') |
|
186 | 186 | |
|
187 | 187 | with self.assertRaises(HTTPError): |
|
188 |
cm.get('foo', type |
|
|
188 | cm.get('foo', type='file') | |
|
189 | 189 | |
|
190 | 190 | |
|
191 | 191 | @dec.skip_win32 |
General Comments 0
You need to be logged in to leave comments.
Login now