Handle a compound extension in new_untitled

pull/2949/head
Steven Silvester 8 years ago
parent f763c03d10
commit f5fa0c180e

@ -4,7 +4,6 @@
# Distributed under the terms of the Modified BSD License.
from fnmatch import fnmatch
import gettext
import itertools
import json
import os
@ -32,6 +31,7 @@ from traitlets import (
from ipython_genutils.py3compat import string_types
from notebook.base.handlers import IPythonHandler
copy_pat = re.compile(r'\-Copy\d*\.')
@ -317,22 +317,31 @@ class ContentsManager(LoggingConfigurable):
The name of a file, including extension
path : unicode
The API path of the target's directory
insert: unicode
The characters to insert after the base filename
Returns
-------
name : unicode
A filename that is unique, based on the input filename.
"""
path = path.strip('/')
basename, ext = os.path.splitext(filename)
# Extract the full suffix from the filename (e.g. .tar.gz)
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
parts = basename.split('.')
basename = os.path.join(dirname, parts[0])
suffix = '.' + '.'.join(parts[1:])
if suffix == '.':
suffix = ''
for i in itertools.count():
if i:
insert_i = '{}{}'.format(insert, i)
else:
insert_i = ''
name = u'{basename}{insert}{ext}'.format(basename=basename,
insert=insert_i, ext=ext)
if not self.exists(u'{}/{}'.format(path, name)):
name = u'{basename}{insert}{suffix}'.format(basename=basename,
insert=insert_i, suffix=suffix)
if not self.exists(os.path.join(path, name)):
break
return name

@ -300,6 +300,12 @@ class TestContentsManager(TestCase):
self.assertEqual(model['name'], 'untitled')
self.assertEqual(model['path'], '%s/untitled' % sub_dir)
# Test with a compound extension
model = cm.new_untitled(path=sub_dir, ext='.foo.bar')
self.assertEqual(model['name'], 'untitled.foo.bar')
model = cm.new_untitled(path=sub_dir, ext='.foo.bar')
self.assertEqual(model['name'], 'untitled1.foo.bar')
def test_modified_date(self):
cm = self.contents_manager

Loading…
Cancel
Save