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