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
Member

@blink1073 blink1073 commented Oct 18, 2017

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

@takluyver takluyver Oct 20, 2017

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.

Loading

basename = os.path.join(dirname, parts[0])
suffix = '.' + '.'.join(parts[1:])
if suffix == '.':
suffix = ''
Copy link
Member

@takluyver takluyver Oct 20, 2017

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

Loading

Copy link
Member Author

@blink1073 blink1073 Oct 20, 2017

Choose a reason for hiding this comment

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

Thanks! Updated.

Loading

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

@takluyver takluyver Oct 20, 2017

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.

Loading

Copy link
Member Author

@blink1073 blink1073 Oct 20, 2017

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.

Loading

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

@takluyver takluyver commented Oct 20, 2017

Thanks, merging.

Loading

@takluyver takluyver merged commit c888b6a into jupyter:master Oct 20, 2017
4 checks passed
Loading
@blink1073
Copy link
Member Author

@blink1073 blink1073 commented Oct 20, 2017

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

Loading

@gnestor
Copy link
Contributor

@gnestor gnestor commented Oct 26, 2017

@meeseeksdev backport to 5.2.1

Loading

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants