Skip to content

Commit

Permalink
simpler implementation for short_form_multiple_formats
Browse files Browse the repository at this point in the history
compatible with Python 2
  • Loading branch information
mwouts committed Jan 15, 2019
1 parent c4eab47 commit 42d4ab5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
47 changes: 19 additions & 28 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,23 +261,8 @@ def check_file_version(notebook, source_path, outputs_path):
os.path.basename(outputs_path)))


def update_formats(formats, ext, format_name):
"""Update the format list with the given format name"""
updated_formats = []
found_ext = False
for org_ext, org_format_name in formats:
if org_ext != ext:
updated_formats.append((org_ext, org_format_name))
elif not found_ext:
updated_formats.append((ext, format_name))
found_ext = True

return updated_formats


def format_name_for_ext(metadata, ext, cm_default_formats=None, explicit_default=True):
"""Return the format name for that extension"""

# Is the format information available in the text representation?
text_repr = metadata.get('jupytext', {}).get('text_representation', {})
if text_repr.get('extension', '').endswith(ext) and text_repr.get('format_name'):
Expand All @@ -298,15 +283,27 @@ def format_name_for_ext(metadata, ext, cm_default_formats=None, explicit_default
return get_format_implementation(ext).format_name


def update_jupytext_formats_metadata(notebook, ext, format_name):
def identical_format_path(fmt1, fmt2):
"""Do the two (long representation) of formats target the same file?"""
for key in ['extension', 'prefix', 'suffix']:
if fmt1.get(key) != fmt2.get(key):
return False
return True


def update_jupytext_formats_metadata(notebook, new_format):
"""Update the jupytext_format metadata in the Jupyter notebook"""
new_format = long_form_one_format(new_format)
formats = long_form_multiple_formats(notebook.metadata.get('jupytext', {}).get('formats', ''))
if not formats:
return

for fmt in formats:
if fmt['extension'] == ext:
fmt['format_name'] = format_name
if identical_format_path(fmt, new_format):
if 'format_name' in new_format:
fmt['format_name'] = new_format['format_name']
else:
fmt.pop('format_name')
break

notebook.metadata.setdefault('jupytext', {})['formats'] = short_form_multiple_formats(formats)
Expand Down Expand Up @@ -405,7 +402,7 @@ def long_form_multiple_formats(jupytext_formats):


def short_form_one_format(jupytext_format):
"""Represent one jupytext format as a string when possible"""
"""Represent one jupytext format as a string"""
fmt = jupytext_format['extension']
if 'suffix' in jupytext_format:
fmt = jupytext_format['suffix'] + fmt
Expand All @@ -415,19 +412,13 @@ def short_form_one_format(jupytext_format):
if 'format_name' in jupytext_format and jupytext_format['extension'] not in ['.md', '.Rmd']:
fmt = fmt + ':' + jupytext_format['format_name']

if set(jupytext_format) <= set(['extension', 'suffix', 'format_name']):
return fmt

return jupytext_format
return fmt


def short_form_multiple_formats(jupytext_formats):
"""Convert jupytext formats, represented as a list of dictionaries, to a more concise form when possible"""
"""Convert jupytext formats, represented as a list of dictionaries, to a comma separated list"""
jupytext_formats = [short_form_one_format(fmt) for fmt in jupytext_formats]

if all([isinstance(fmt, str) for fmt in jupytext_formats]):
return ','.join(jupytext_formats)
return jupytext_formats
return ','.join(jupytext_formats)


_VALID_FORMAT_OPTIONS = ['extension', 'format_name', 'suffix', 'prefix', 'comment_magics',
Expand Down
7 changes: 4 additions & 3 deletions jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ def writes(notebook, fmt, version=nbformat.NO_CONVERT, **kwargs):
return nbformat.writes(notebook, version, **kwargs)

if not format_name:
format_name = format_name_for_ext(notebook.metadata, ext)
format_name = format_name_for_ext(notebook.metadata, ext, explicit_default=False)

if format_name and insert_or_test_version_number():
update_jupytext_formats_metadata(notebook, ext, format_name)
if format_name:
fmt['format_name'] = format_name
update_jupytext_formats_metadata(notebook, fmt)

writer = TextNotebookConverter(fmt)
return writer.writes(notebook)
Expand Down
17 changes: 13 additions & 4 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from testfixtures import compare
from nbformat.v4.nbbase import new_notebook
from jupytext.formats import guess_format, read_format_from_metadata, rearrange_jupytext_metadata
from jupytext.formats import long_form_multiple_formats, short_form_multiple_formats
from jupytext.formats import long_form_multiple_formats, short_form_multiple_formats, update_jupytext_formats_metadata
from .utils import list_notebooks


Expand Down Expand Up @@ -57,6 +58,16 @@ def test_read_format_from_metadata(script="""---
assert read_format_from_metadata(script, '.Rmd') is None


def test_update_jupytext_formats_metadata():
nb = new_notebook(metadata={'jupytext': {'formats': 'py'}})
update_jupytext_formats_metadata(nb, 'py:light')
assert nb.metadata['jupytext']['formats'] == 'py:light'

nb = new_notebook(metadata={'jupytext': {'formats': 'ipynb,py'}})
update_jupytext_formats_metadata(nb, 'py:light')
assert nb.metadata['jupytext']['formats'] == 'ipynb,py:light'


def test_decompress_formats():
assert long_form_multiple_formats('ipynb') == [{'extension': '.ipynb'}]
assert long_form_multiple_formats('ipynb,md') == [{'extension': '.ipynb'}, {'extension': '.md'}]
Expand All @@ -75,9 +86,7 @@ def test_compress_formats():
[{'extension': '.ipynb'}, {'extension': '.py', 'format_name': 'light'}]) == 'ipynb,py:light'
assert short_form_multiple_formats([{'extension': '.ipynb'},
{'extension': '.py', 'format_name': 'light'},
{'extension': '.md', 'comment_magics': True}]) == ['ipynb', 'py:light',
{'extension': '.md',
'comment_magics': True}]
{'extension': '.md', 'comment_magics': True}]) == 'ipynb,py:light,md'
assert short_form_multiple_formats(
[{'extension': '.py', 'suffix': '.pct', 'format_name': 'percent'}]) == '.pct.py:percent'

Expand Down

0 comments on commit 42d4ab5

Please sign in to comment.