Skip to content

Handle a compound extension in new_untitled#2949

Merged
takluyver merged 5 commits into
jupyter:masterfrom
blink1073:fix-compound-extension
Oct 20, 2017
Merged

Handle a compound extension in new_untitled#2949
takluyver merged 5 commits into
jupyter:masterfrom
blink1073:fix-compound-extension

Conversation

@blink1073
Copy link
Copy Markdown
Contributor

This allows file names with compound extensions (e.g. .tar.gz) to be properly incremented when creating a new file. Previously we would end up with untitled.tar1.gz.

cf jupyterlab/jupyterlab#3113

Comment thread notebook/services/contents/manager.py Outdated
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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be summarised as dirname, basename = os.path.split(filename).

Also, I think we should leave in the .strip('/') that was there before, so that passing in foo/bar/ gives you foo/bar1, not foo/bar/1.

Comment thread notebook/services/contents/manager.py Outdated
basename = os.path.join(dirname, parts[0])
suffix = '.' + '.'.join(parts[1:])
if suffix == '.':
suffix = ''
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using str.partition():

name, dot, ext = basename.partition('.')
basename = os.path.join(dirname, name)
suffix = dot+ext

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updated.

Comment thread notebook/services/contents/manager.py Outdated
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)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, hang on. This function works with an API path, which is always / separated, rather than a platform-dependent native path. So using os.path.split and os.path.join is wrong here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Looking again, filename should never have a directory in it.

@takluyver takluyver added this to the 5.3 milestone Oct 20, 2017
@takluyver
Copy link
Copy Markdown
Member

Thanks, merging.

@takluyver takluyver merged commit c888b6a into jupyter:master Oct 20, 2017
@blink1073
Copy link
Copy Markdown
Contributor Author

Ha, that was easy, thanks for your patience ;).

@gnestor
Copy link
Copy Markdown
Contributor

gnestor commented Oct 26, 2017

@meeseeksdev backport to 5.2.1

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants