Skip to content

Commit c888b6a

Browse files
authored
Merge pull request jupyter#2949 from blink1073/fix-compound-extension
Handle a compound extension in new_untitled
2 parents aa461d9 + 7e71c00 commit c888b6a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

notebook/services/contents/manager.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Distributed under the terms of the Modified BSD License.
55

66
from fnmatch import fnmatch
7-
import gettext
87
import itertools
98
import json
109
import os
@@ -32,6 +31,7 @@
3231
from ipython_genutils.py3compat import string_types
3332
from notebook.base.handlers import IPythonHandler
3433

34+
3535
copy_pat = re.compile(r'\-Copy\d*\.')
3636

3737

@@ -317,21 +317,26 @@ def increment_filename(self, filename, path='', insert=''):
317317
The name of a file, including extension
318318
path : unicode
319319
The API path of the target's directory
320+
insert: unicode
321+
The characters to insert after the base filename
320322
321323
Returns
322324
-------
323325
name : unicode
324326
A filename that is unique, based on the input filename.
325327
"""
328+
# Extract the full suffix from the filename (e.g. .tar.gz)
326329
path = path.strip('/')
327-
basename, ext = os.path.splitext(filename)
330+
basename, dot, ext = filename.partition('.')
331+
suffix = dot + ext
332+
328333
for i in itertools.count():
329334
if i:
330335
insert_i = '{}{}'.format(insert, i)
331336
else:
332337
insert_i = ''
333-
name = u'{basename}{insert}{ext}'.format(basename=basename,
334-
insert=insert_i, ext=ext)
338+
name = u'{basename}{insert}{suffix}'.format(basename=basename,
339+
insert=insert_i, suffix=suffix)
335340
if not self.exists(u'{}/{}'.format(path, name)):
336341
break
337342
return name

notebook/services/contents/tests/test_manager.py

+6
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ def test_new_untitled(self):
300300
self.assertEqual(model['name'], 'untitled')
301301
self.assertEqual(model['path'], '%s/untitled' % sub_dir)
302302

303+
# Test with a compound extension
304+
model = cm.new_untitled(path=sub_dir, ext='.foo.bar')
305+
self.assertEqual(model['name'], 'untitled.foo.bar')
306+
model = cm.new_untitled(path=sub_dir, ext='.foo.bar')
307+
self.assertEqual(model['name'], 'untitled1.foo.bar')
308+
303309
def test_modified_date(self):
304310

305311
cm = self.contents_manager

0 commit comments

Comments
 (0)