split ContentsManager.new, add ContentsManager.new_untitled

pull/37/head
Min RK 12 years ago
parent 39041a9f03
commit da832ef59f

@ -92,18 +92,11 @@ class ContentsHandler(IPythonHandler):
model = self.contents_manager.new(model, path)
self.set_status(201)
self._finish_model(model)
def _new(self, path, type='notebook', ext=''):
"""Create an empty file or directory in directory specified by path
ContentsManager picks the filename.
"""
def _new_untitled(self, path, type='', ext=''):
"""Create a new, empty untitled entity"""
self.log.info(u"Creating new %s in %s", type or 'file', path)
if type:
model = {'type': type}
else:
model = None
model = self.contents_manager.new(model, path=path, ext=ext)
model = self.contents_manager.new_untitled(path=path, type=type, ext=ext)
self.set_status(201)
self._finish_model(model)
@ -140,13 +133,13 @@ class ContentsHandler(IPythonHandler):
if model is not None:
copy_from = model.get('copy_from')
ext = model.get('ext', '')
type = model.get('type')
type = model.get('type', '')
if copy_from:
self._copy(copy_from, path)
else:
self._new(path, type=type, ext=ext)
self._new_untitled(path, type=type, ext=ext)
else:
self._new(path)
self._new_untitled(path)
@web.authenticated
@json_errors
@ -170,7 +163,7 @@ class ContentsHandler(IPythonHandler):
else:
self._upload(model, path)
else:
self._new(path)
self._new_untitled(path)
@web.authenticated
@json_errors

@ -221,28 +221,60 @@ class ContentsManager(LoggingConfigurable):
e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'),
)
return model
def new(self, model=None, path='', ext=''):
def new_untitled(self, path='', type='', ext=''):
"""Create a new untitled file or directory in path
path must be a directory
File extension can be specified.
Use `new` to create files with a fully specified path (including filename).
"""
path = path.strip('/')
if not self.dir_exists(path):
raise HTTPError(404, 'No such directory: %s' % path)
model = {}
if type:
model['type'] = type
if ext == '.ipynb':
model.setdefault('type', 'notebook')
else:
model.setdefault('type', 'file')
if model['type'] == 'directory':
untitled = self.untitled_directory
elif model['type'] == 'notebook':
untitled = self.untitled_notebook
ext = '.ipynb'
elif model['type'] == 'file':
untitled = self.untitled_file
else:
raise HTTPError(400, "Unexpected model type: %r" % model['type'])
name = self.increment_filename(untitled + ext, path)
path = u'{0}/{1}'.format(path, name)
return self.new(model, path)
def new(self, model=None, path=''):
"""Create a new file or directory and return its model with no content.
If path is a directory, a new untitled file/directory is created in path.
Otherwise, a new file/directory is created exactly at path.
To create a new untitled entity in a directory, use `new_untitled`.
"""
path = path.strip('/')
if model is None:
model = {}
else:
model.pop('path', None)
if ext and ext != '.ipynb':
model.setdefault('type', 'file')
else:
if path.endswith('.ipynb'):
model.setdefault('type', 'notebook')
else:
model.setdefault('type', 'file')
# no content, not a directory, so fill out new-file model
if 'content' not in model and model['type'] != 'directory':
if model['type'] == 'notebook':
ext = '.ipynb'
model['content'] = new_notebook()
model['format'] = 'json'
else:
@ -250,19 +282,6 @@ class ContentsManager(LoggingConfigurable):
model['type'] = 'file'
model['format'] = 'text'
# if path is a directory, create an untitled file or directory
if self.dir_exists(path):
if model['type'] == 'directory':
untitled = self.untitled_directory
elif model['type'] == 'notebook':
untitled = self.untitled_notebook
elif model['type'] == 'file':
untitled = self.untitled_file
else:
raise HTTPError(400, "Unexpected model type: %r" % model['type'])
name = self.increment_filename(untitled + ext, path)
path = u'{0}/{1}'.format(path, name)
model = self.save(model, path)
return model

@ -49,7 +49,7 @@ class API(object):
def read(self, path):
return self._req('GET', path)
def create_untitled(self, path='/', ext=None):
def create_untitled(self, path='/', ext='.ipynb'):
body = None
if ext:
body = json.dumps({'ext': ext})

@ -101,7 +101,7 @@ class TestContentsManager(TestCase):
def new_notebook(self):
cm = self.contents_manager
model = cm.new()
model = cm.new_untitled(type='notebook')
name = model['name']
path = model['path']
@ -112,30 +112,42 @@ class TestContentsManager(TestCase):
cm.save(full_model, path)
return nb, name, path
def test_new(self):
def test_new_untitled(self):
cm = self.contents_manager
# Test in root directory
model = cm.new()
model = cm.new_untitled(type='notebook')
assert isinstance(model, dict)
self.assertIn('name', model)
self.assertIn('path', model)
self.assertIn('type', model)
self.assertEqual(model['type'], 'notebook')
self.assertEqual(model['name'], 'Untitled0.ipynb')
self.assertEqual(model['path'], 'Untitled0.ipynb')
# Test in sub-directory
sub_dir = '/foo/'
self.make_dir(cm.root_dir, 'foo')
model = cm.new(path=sub_dir)
model = cm.new_untitled(type='directory')
assert isinstance(model, dict)
self.assertIn('name', model)
self.assertIn('path', model)
self.assertEqual(model['name'], 'Untitled0.ipynb')
self.assertEqual(model['path'], 'foo/Untitled0.ipynb')
self.assertIn('type', model)
self.assertEqual(model['type'], 'directory')
self.assertEqual(model['name'], 'Untitled Folder0')
self.assertEqual(model['path'], 'Untitled Folder0')
sub_dir = model['path']
model = cm.new_untitled(path=sub_dir)
assert isinstance(model, dict)
self.assertIn('name', model)
self.assertIn('path', model)
self.assertIn('type', model)
self.assertEqual(model['type'], 'file')
self.assertEqual(model['name'], 'untitled0')
self.assertEqual(model['path'], '%s/untitled0' % sub_dir)
def test_get(self):
cm = self.contents_manager
# Create a notebook
model = cm.new()
model = cm.new_untitled(type='notebook')
name = model['name']
path = model['path']
@ -150,7 +162,7 @@ class TestContentsManager(TestCase):
# Test in sub-directory
sub_dir = '/foo/'
self.make_dir(cm.root_dir, 'foo')
model = cm.new(path=sub_dir, ext='.ipynb')
model = cm.new_untitled(path=sub_dir, ext='.ipynb')
model2 = cm.get_model(sub_dir + name)
assert isinstance(model2, dict)
self.assertIn('name', model2)
@ -165,7 +177,7 @@ class TestContentsManager(TestCase):
path = 'test bad symlink'
os_path = self.make_dir(cm.root_dir, path)
file_model = cm.new(path=path, ext='.txt')
file_model = cm.new_untitled(path=path, ext='.txt')
# create a broken symlink
os.symlink("target", os.path.join(os_path, "bad symlink"))
@ -180,12 +192,11 @@ class TestContentsManager(TestCase):
path = '{0}/{1}'.format(parent, name)
os_path = self.make_dir(cm.root_dir, parent)
file_model = cm.new(path=parent, ext='.txt')
file_model = cm.new(path=parent + '/zfoo.txt')
# create a good symlink
os.symlink(file_model['name'], os.path.join(os_path, name))
symlink_model = cm.get_model(path, content=False)
dir_model = cm.get_model(parent)
self.assertEqual(
sorted(dir_model['content'], key=lambda x: x['name']),
@ -195,7 +206,7 @@ class TestContentsManager(TestCase):
def test_update(self):
cm = self.contents_manager
# Create a notebook
model = cm.new()
model = cm.new_untitled(type='notebook')
name = model['name']
path = model['path']
@ -214,7 +225,7 @@ class TestContentsManager(TestCase):
# Create a directory and notebook in that directory
sub_dir = '/foo/'
self.make_dir(cm.root_dir, 'foo')
model = cm.new(None, sub_dir)
model = cm.new_untitled(path=sub_dir, type='notebook')
name = model['name']
path = model['path']
@ -234,7 +245,7 @@ class TestContentsManager(TestCase):
def test_save(self):
cm = self.contents_manager
# Create a notebook
model = cm.new()
model = cm.new_untitled(type='notebook')
name = model['name']
path = model['path']
@ -253,7 +264,7 @@ class TestContentsManager(TestCase):
# Create a directory and notebook in that directory
sub_dir = '/foo/'
self.make_dir(cm.root_dir, 'foo')
model = cm.new(None, sub_dir)
model = cm.new_untitled(path=sub_dir, type='notebook')
name = model['name']
path = model['path']
model = cm.get_model(path)

Loading…
Cancel
Save