diff --git a/jupytext/contentsmanager.py b/jupytext/contentsmanager.py index 8e36ef90a..ec2cfb1c5 100644 --- a/jupytext/contentsmanager.py +++ b/jupytext/contentsmanager.py @@ -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): @@ -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), @@ -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, diff --git a/requirements.txt b/requirements.txt index 44fd03cb6..800aacc61 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ nbformat>=4.0.0 pyyaml mock -six testfixtures diff --git a/setup.py b/setup.py index de56fc0c8..1dc31b8ae 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tests/test_contentsmanager.py b/tests/test_contentsmanager.py index a27d83d8e..3d5c7639a 100644 --- a/tests/test_contentsmanager.py +++ b/tests/test_contentsmanager.py @@ -1,3 +1,5 @@ +# coding: utf-8 + import os import sys import pytest @@ -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'))