##// END OF EJS Templates
more explicit error catching...
Paul Ivanov -
Show More
@@ -21,14 +21,12 b' import io'
21 import os
21 import os
22 import glob
22 import glob
23 import shutil
23 import shutil
24 import ast
24 import errno
25 import base64
26
25
27 from tornado import web
26 from tornado import web
28
27
29 from IPython.config.configurable import LoggingConfigurable
28 from IPython.config.configurable import LoggingConfigurable
30 from IPython.nbformat import current
29 from IPython.utils.traitlets import Unicode, TraitError
31 from IPython.utils.traitlets import List, Dict, Unicode, TraitError
32 from IPython.utils import tz
30 from IPython.utils import tz
33
31
34 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
@@ -140,8 +138,66 b' class ContentManager(LoggingConfigurable):'
140 "last_modified (UTC)": last_modified.ctime(),
138 "last_modified (UTC)": last_modified.ctime(),
141 "size": size}
139 "size": size}
142 return model
140 return model
143
144 def delete_content(self, content_path):
145 """Delete a file"""
146 os.unlink(os.path.join(self.content_dir, content_path))
147
141
142 def create_folder(self, name, path):
143 """
144 Parameters
145 ----------
146 name : str
147 The name you want give to the folder thats created.
148 If this is None, it will assign an incremented name
149 'new_folder'.
150 path : str
151 The relative location to put the created folder.
152
153 Returns
154 -------
155 The name of the created folder.
156 """
157 if name is None:
158 name = self.increment_filename("new_folder", path)
159 new_path = self.get_os_path(name, path)
160 # Raise an error if the file exists
161 try:
162 os.makedirs(new_path)
163 except OSError as e:
164 if e.errno == errno.EEXIST:
165 raise web.HTTPError(409, u'Directory already exists.')
166 elif e.errno == errno.EACCES:
167 raise web.HTTPError(403, u'Create dir: permission denied.')
168 else:
169 raise web.HTTPError(400, str(e))
170 return name
171
172 def increment_filename(self, basename, content_path='/'):
173 """Return a non-used filename of the form basename<int>.
174
175 This searches through the filenames (basename0, basename1, ...)
176 until is find one that is not already being used. It is used to
177 create Untitled and Copy names that are unique.
178 """
179 i = 0
180 while True:
181 name = u'%s%i' % (basename,i)
182 path = self.get_os_path(name, content_path)
183 if not os.path.isdir(path):
184 break
185 else:
186 i = i+1
187 return name
188
189 def delete_content(self, name=None, content_path='/'):
190 """Delete a file or folder in the named location.
191 Raises an error if the named file/folder doesn't exist
192 """
193 path = self.get_os_path(name, content_path)
194 if path != self.content_dir:
195 try:
196 shutil.rmtree(path)
197 except OSError as e:
198 if e.errno == errno.ENOENT:
199 raise web.HTTPError(404, u'Directory or file does not exist.')
200 else:
201 raise web.HTTPError(400, str(e))
202 else:
203 raise web.HTTPError(403, "Cannot delete root directory where notebook server lives.")
@@ -53,7 +53,7 b' class TestContentManager(TestCase):'
53
53
54 # Raise an exception when trying to delete a
54 # Raise an exception when trying to delete a
55 # folder that does not exist.
55 # folder that does not exist.
56 self.assertRaises(TraitError, cm.delete_content, name='non_existing_folder', content_path='/')
56 self.assertRaises(HTTPError, cm.delete_content, name='non_existing_folder', content_path='/')
57
57
58 # Create a subfolder in the folder created above.
58 # Create a subfolder in the folder created above.
59 # *Recall 'name' = 'test_folder' (the new path for
59 # *Recall 'name' = 'test_folder' (the new path for
@@ -61,7 +61,7 b' class TestContentManager(TestCase):'
61 name01 = cm.new_folder(None, name)
61 name01 = cm.new_folder(None, name)
62 path01 = cm.get_os_path(name01, name)
62 path01 = cm.get_os_path(name01, name)
63 # Try to delete a subfolder that does not exist.
63 # Try to delete a subfolder that does not exist.
64 self.assertRaises(TraitError, cm.delete_content, name='non_existing_folder', content_path='/')
64 self.assertRaises(HTTPError, cm.delete_content, name='non_existing_folder', content_path='/')
65 # Delete the created subfolder
65 # Delete the created subfolder
66 cm.delete_content(name01, name)
66 cm.delete_content(name01, name)
67 self.assertFalse(os.path.isdir(path01))
67 self.assertFalse(os.path.isdir(path01))
@@ -70,6 +70,10 b' class TestContentManager(TestCase):'
70 cm.delete_content(name, '/')
70 cm.delete_content(name, '/')
71 self.assertFalse(os.path.isdir(path))
71 self.assertFalse(os.path.isdir(path))
72
72
73 self.assertRaises(HTTPError, cm.delete_content, name=None, content_path='/')
74 self.assertRaises(HTTPError, cm.delete_content, name='/', content_path='/')
75
76
73 def test_get_content_names(self):
77 def test_get_content_names(self):
74 with TemporaryDirectory() as td:
78 with TemporaryDirectory() as td:
75 # Create a few folders and subfolders
79 # Create a few folders and subfolders
General Comments 0
You need to be logged in to leave comments. Login now