Skip to content

Commit

Permalink
Merge pull request #2949 from blink1073/fix-compound-extension
Browse files Browse the repository at this point in the history
Handle a compound extension in new_untitled
  • Loading branch information
takluyver authored Oct 20, 2017
2 parents aa461d9 + 7e71c00 commit c888b6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions notebook/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,6 +31,7 @@
from ipython_genutils.py3compat import string_types
from notebook.base.handlers import IPythonHandler


copy_pat = re.compile(r'\-Copy\d*\.')


Expand Down Expand Up @@ -317,21 +317,26 @@ def increment_filename(self, filename, path='', insert=''):
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.
"""
# Extract the full suffix from the filename (e.g. .tar.gz)
path = path.strip('/')
basename, ext = os.path.splitext(filename)
basename, dot, ext = filename.partition('.')
suffix = dot + ext

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)
name = u'{basename}{insert}{suffix}'.format(basename=basename,
insert=insert_i, suffix=suffix)
if not self.exists(u'{}/{}'.format(path, name)):
break
return name
Expand Down
6 changes: 6 additions & 0 deletions notebook/services/contents/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ def test_new_untitled(self):
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
Expand Down

0 comments on commit c888b6a

Please sign in to comment.