Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle a compound extension in new_untitled #2949

Merged
merged 5 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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