def test_combine_lower_version_raises(tmpdir): tmp_ipynb = 'notebook.ipynb' tmp_nbpy = 'notebook.py' with open(str(tmpdir.join(tmp_nbpy)), 'w') as fp: fp.write("""# --- # jupyter: # jupytext_formats: ipynb,py # jupytext_format_version: '0.0' # --- # New cell """) nb = new_notebook(metadata={'jupytext_formats': 'ipynb,py'}) jupytext.writef(nb, str(tmpdir.join(tmp_ipynb))) cm = TextFileContentsManager() cm.default_jupytext_formats = 'ipynb,py' cm.root_dir = str(tmpdir) with pytest.raises(HTTPError): with mock.patch('jupytext.file_format_version.FILE_FORMAT_VERSION', {'.py': '1.0'}): with mock.patch( 'jupytext.file_format_version.MIN_FILE_FORMAT_VERSION', {'.py': '1.0'}): cm.get(tmp_ipynb)
def test_combine_same_version_ok(tmpdir): tmp_ipynb = 'notebook.ipynb' tmp_nbpy = 'notebook.py' with open(str(tmpdir.join(tmp_nbpy)), 'w') as fp: fp.write("""# --- # jupyter: # jupytext_formats: ipynb,py # jupytext_format_version: '1.0' # --- # New cell """) nb = new_notebook(metadata={'jupytext_formats': 'ipynb,py'}) jupytext.writef(nb, str(tmpdir.join(tmp_ipynb))) cm = TextFileContentsManager() cm.default_jupytext_formats = 'ipynb,py' cm.root_dir = str(tmpdir) with mock.patch('jupytext.file_format_version.FILE_FORMAT_VERSION', {'.py': '1.0'}): nb = cm.get(tmp_ipynb) cells = nb['content']['cells'] assert len(cells) == 1 assert cells[0].cell_type == 'markdown' assert cells[0].source == 'New cell'
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'))
def test_outdated_text_notebook(nb_file, tmpdir): # 1. write py ipynb tmp_ipynb = u'notebook.ipynb' tmp_nbpy = u'notebook.py' cm = TextFileContentsManager() cm.default_jupytext_formats = 'py,ipynb' cm.outdated_text_notebook_margin = 0 cm.root_dir = str(tmpdir) # open ipynb, save py, reopen nb = readf(nb_file) cm.save(model=dict(type='notebook', content=nb), path=tmp_nbpy) model_py = cm.get(tmp_nbpy, load_alternative_format=False) model_ipynb = cm.get(tmp_ipynb, load_alternative_format=False) # 2. check that time of ipynb <= py assert model_ipynb['last_modified'] <= model_py['last_modified'] # 3. wait some time time.sleep(0.5) # 4. touch ipynb with open(str(tmpdir.join(tmp_ipynb)), 'a'): os.utime(str(tmpdir.join(tmp_ipynb)), None) # 5. test error with pytest.raises(HTTPError): cm.get(tmp_nbpy) # 6. test OK with cm.outdated_text_notebook_margin = 1.0 cm.get(tmp_nbpy) # 7. test OK with cm.outdated_text_notebook_margin = float("inf") cm.get(tmp_nbpy)
def test_load_save_rename_nbpy(nb_file, tmpdir): tmp_ipynb = 'notebook.ipynb' tmp_nbpy = 'notebook.nb.py' cm = TextFileContentsManager() cm.default_jupytext_formats = 'ipynb,nb.py' cm.root_dir = str(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']) # save ipynb cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb) # rename nbpy cm.rename(tmp_nbpy, 'new.nb.py') assert not os.path.isfile(str(tmpdir.join(tmp_ipynb))) assert not os.path.isfile(str(tmpdir.join(tmp_nbpy))) assert os.path.isfile(str(tmpdir.join('new.ipynb'))) assert os.path.isfile(str(tmpdir.join('new.nb.py')))