Skip to content

Commit

Permalink
Test and fix unicode file names #70
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Sep 11, 2018
1 parent e861670 commit b4c1234
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
23 changes: 14 additions & 9 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
import os
import nbformat
import mock
import six
from tornado.web import HTTPError
from traitlets import Unicode
from traitlets.config import Configurable

from notebook.services.contents.filemanager import FileContentsManager

try:
import notebook.transutils # noqa
except ImportError:
pass
from notebook.services.contents.filemanager import FileContentsManager
from tornado.web import HTTPError
from traitlets import Unicode
from traitlets.config import Configurable
import jupytext

import jupytext
from . import combine
from .file_format_version import check_file_version

try:
unicode # Python 2
except NameError:
unicode = str # Python 3


def _jupytext_writes(ext):
def _writes(nbk, version=nbformat.NO_CONVERT, **kwargs):
Expand Down Expand Up @@ -58,7 +63,7 @@ def check_formats(formats):
return check_formats([formats])
validated_group = []
for fmt in group:
if not isinstance(fmt, six.string_types):
if not isinstance(fmt, unicode):
raise ValueError('Extensions should be strings among {}'
', not {}.\n{}'
.format(str(jupytext.NOTEBOOK_EXTENSIONS),
Expand Down Expand Up @@ -173,14 +178,14 @@ def _read_notebook(self, os_path, as_version=4,
break

if source_format != fmt:
self.log.info('Reading SOURCE from {}'
self.log.info(u'Reading SOURCE from {}'
.format(os.path.basename(file + source_format)))
nb_outputs = nbk
nbk = self._read_notebook(file + source_format,
as_version=as_version,
load_alternative_format=False)
elif outputs_format != fmt:
self.log.info('Reading OUTPUTS from {}'
self.log.info(u'Reading OUTPUTS from {}'
.format(os.path.basename(file + outputs_format)))
if outputs_format != fmt:
nb_outputs = self._read_notebook(file + outputs_format,
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
nbformat>=4.0.0
pyyaml
mock
six
testfixtures
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'pynotebook = jupytext:PyNotebookExporter',
'rnotebook = jupytext:RNotebookExporter']},
tests_require=['pytest'],
install_requires=['nbformat>=4.0.0', 'mock', 'pyyaml', 'six', 'testfixtures'],
install_requires=['nbformat>=4.0.0', 'mock', 'pyyaml', 'testfixtures'],
license='MIT',
classifiers=('Development Status :: 4 - Beta',
'Environment :: Console',
Expand Down
44 changes: 44 additions & 0 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding: utf-8

import os
import sys
import pytest
Expand Down Expand Up @@ -119,3 +121,45 @@ def test_load_save_rename_nbpy_default_config(nb_file, tmpdir):

assert not os.path.isfile(str(tmpdir.join('new.ipynb')))
assert os.path.isfile(str(tmpdir.join('new.nb.py')))


@pytest.mark.skipif(isinstance(TextFileContentsManager, str),
reason=TextFileContentsManager)
@pytest.mark.parametrize('nb_file', list_py_notebooks('.ipynb'))
def test_load_save_rename_non_ascii_path(nb_file, tmpdir):
tmp_ipynb = u'notebôk.ipynb'
tmp_nbpy = u'notebôk.nb.py'

cm = TextFileContentsManager()
cm.default_jupytext_formats = 'ipynb'
tmpdir = u'' + str(tmpdir)
cm.root_dir = tmpdir

# open ipynb, save nb.py, reopen
nb = readf(nb_file)
cm.save(model=dict(type='notebook', content=nb), path=tmp_nbpy)
nbpy = cm.get(tmp_nbpy)
compare_notebooks(nb, nbpy['content'])

# open ipynb
nbipynb = cm.get(tmp_ipynb)
compare_notebooks(nb, nbipynb['content'])

# save ipynb
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)

# rename nbpy
cm.rename(tmp_nbpy, u'nêw.nb.py')
assert not os.path.isfile(os.path.join(tmpdir, tmp_ipynb))
assert not os.path.isfile(os.path.join(tmpdir, tmp_nbpy))

assert os.path.isfile(os.path.join(tmpdir, u'nêw.ipynb'))
assert os.path.isfile(os.path.join(tmpdir, u'nêw.nb.py'))

# rename ipynb
cm.rename(u'nêw.ipynb', tmp_ipynb)
assert os.path.isfile(os.path.join(tmpdir, tmp_ipynb))
assert not os.path.isfile(os.path.join(tmpdir, tmp_nbpy))

assert not os.path.isfile(os.path.join(tmpdir, u'nêw.ipynb'))
assert os.path.isfile(os.path.join(tmpdir, u'nêw.nb.py'))

0 comments on commit b4c1234

Please sign in to comment.