diff --git a/notebook/services/contents/manager.py b/notebook/services/contents/manager.py index e8b34e684..af645a851 100644 --- a/notebook/services/contents/manager.py +++ b/notebook/services/contents/manager.py @@ -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 diff --git a/notebook/services/contents/tests/test_manager.py b/notebook/services/contents/tests/test_manager.py index 0b4e5a4ea..0e6b0fb2b 100644 --- a/notebook/services/contents/tests/test_manager.py +++ b/notebook/services/contents/tests/test_manager.py @@ -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