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

Conversation

blink1073
Copy link
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

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
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.

basename = os.path.join(dirname, parts[0])
suffix = '.' + '.'.join(parts[1:])
if suffix == '.':
suffix = ''
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Updated.

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
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
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
Member

Thanks, merging.

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

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

@gnestor
Copy link
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.

None yet

3 participants