Skip to content

Commit

Permalink
Merge pull request #6265 from cel4/fix_parse_filename
Browse files Browse the repository at this point in the history
parse_filename: Fixed issues when file name contains dots.
  • Loading branch information
takluyver committed Aug 6, 2014
2 parents 0bcb548 + 41d26d2 commit bc12c3e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
14 changes: 8 additions & 6 deletions IPython/nbformat/v2/__init__.py
Expand Up @@ -16,6 +16,8 @@
# Imports
#-----------------------------------------------------------------------------

import os

from .nbbase import (
NotebookNode,
new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
Expand Down Expand Up @@ -64,15 +66,15 @@ def parse_filename(fname):
(fname, name, format) : (unicode, unicode, unicode)
The filename, notebook name and format.
"""
if fname.endswith(u'.ipynb'):
basename, ext = os.path.splitext(fname)
if ext == u'.ipynb':
format = u'json'
elif fname.endswith(u'.json'):
elif ext == u'.json':
format = u'json'
elif fname.endswith(u'.py'):
elif ext == u'.py':
format = u'py'
else:
basename = fname
fname = fname + u'.ipynb'
format = u'json'
name = fname.split('.')[0]
return fname, name, format

return fname, basename, format
14 changes: 8 additions & 6 deletions IPython/nbformat/v3/__init__.py
Expand Up @@ -16,6 +16,8 @@
# Imports
#-----------------------------------------------------------------------------

import os

from .nbbase import (
NotebookNode,
new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet,
Expand Down Expand Up @@ -61,15 +63,15 @@ def parse_filename(fname):
(fname, name, format) : (unicode, unicode, unicode)
The filename, notebook name and format.
"""
if fname.endswith(u'.ipynb'):
basename, ext = os.path.splitext(fname)
if ext == u'.ipynb':
format = u'json'
elif fname.endswith(u'.json'):
elif ext == u'.json':
format = u'json'
elif fname.endswith(u'.py'):
elif ext == u'.py':
format = u'py'
else:
basename = fname
fname = fname + u'.ipynb'
format = u'json'
name = fname.split('.')[0]
return fname, name, format

return fname, basename, format
33 changes: 33 additions & 0 deletions IPython/nbformat/v3/tests/test_misc.py
@@ -0,0 +1,33 @@
import os
from unittest import TestCase

from IPython.utils.py3compat import unicode_type
from .. import parse_filename


class MiscTests(TestCase):

def check_filename(self, path, exp_fname, exp_bname, exp_format):
fname, bname, format = parse_filename(path)
self.assertEqual(fname, exp_fname)
self.assertEqual(bname, exp_bname)
self.assertEqual(format, exp_format)

def test_parse_filename(self):

# check format detection
self.check_filename("test.ipynb", "test.ipynb", "test", "json")
self.check_filename("test.json", "test.json", "test", "json")
self.check_filename("test.py", "test.py", "test", "py")

# check parsing an unknown format
self.check_filename("test.nb", "test.nb.ipynb", "test.nb", "json")

# check parsing a full file path
abs_path = os.path.abspath("test.ipynb")
basename, ext = os.path.splitext(abs_path)
self.check_filename(abs_path, abs_path, basename, "json")

# check parsing a file name containing dots
self.check_filename("test.nb.ipynb", "test.nb.ipynb", "test.nb",
"json")

0 comments on commit bc12c3e

Please sign in to comment.