Skip to content

Commit

Permalink
Current extension cannot go to 'formats'
Browse files Browse the repository at this point in the history
Format is not appended to 'formats' when extension is not present there #93  #95
Format name and extension in 'text_representation' section
  • Loading branch information
mwouts committed Oct 10, 2018
1 parent 3eb3c1c commit bf8b476
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
38 changes: 28 additions & 10 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def read_format_from_metadata(text, ext):

transition_to_jupytext_section_in_metadata(metadata, ext.endswith('.ipynb'))

format_name = metadata.get('jupytext', {}).get('this_document', {}).get('format_name')
format_name = metadata.get('jupytext', {}).get('text_representation', {}).get('format_name')
if format_name:
return format_name

Expand Down Expand Up @@ -224,10 +224,8 @@ def check_file_version(notebook, source_path, outputs_path):
_, ext = os.path.splitext(source_path)
if ext.endswith('.ipynb'):
return
version = notebook.metadata.get('jupytext', {}).get('this_document', {}).get('format_version')
version = notebook.metadata.get('jupytext', {}).get('text_representation', {}).get('format_version')
format_name = format_name_for_ext(notebook.metadata, ext)
if 'this_document' in notebook.metadata['jupytext']:
del notebook.metadata['jupytext']['this_document']

fmt = get_format(ext, format_name)
current = fmt.current_version_number
Expand Down Expand Up @@ -290,8 +288,6 @@ def update_formats(formats, ext, format_name):
elif not found_ext:
updated_formats.append((ext, format_name))
found_ext = True
if not found_ext:
updated_formats.append((ext, format_name))

return updated_formats

Expand All @@ -311,14 +307,34 @@ def formats_as_string(formats):
for ext, format_name in formats])


def auto_ext_from_metadata(metadata):
auto_ext = metadata.get('language_info', {}).get('file_extension')
if auto_ext == '.r':
return '.R'
return auto_ext


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

# Current format: Don't change it unless an explicit instruction is given in the 'formats' field.
text_repr = metadata.get('jupytext', {}).get('text_representation')
if text_repr and text_repr.get('extension') == ext and text_repr.get('format_name'):
current_format = text_repr.get('format_name')
else:
current_format = None

auto_ext = auto_ext_from_metadata(metadata)

formats = metadata.get('jupytext', {}).get('formats', '')
formats = parse_formats(formats)
for fmt_ext, ext_format_name in formats:
if fmt_ext.endswith(ext):
if fmt_ext.endswith(ext) or (fmt_ext.endswith('.auto') and auto_ext and ext.endswith(auto_ext)):
if (not explicit_default) or ext_format_name:
return ext_format_name
return ext_format_name or current_format

if current_format:
return current_format

if (not explicit_default) or ext in ['.Rmd', 'md']:
return None
Expand All @@ -330,7 +346,8 @@ def update_jupytext_formats_metadata(notebook, ext, format_name):
"""Update the jupytext_format metadata in the Jupyter notebook"""
formats = parse_formats(notebook.metadata.get('jupytext', {}).get('formats', ''))
formats = update_formats(formats, ext, format_name)
notebook.metadata.setdefault('jupytext', {})['formats'] = formats_as_string(formats)
if formats:
notebook.metadata.setdefault('jupytext', {})['formats'] = formats_as_string(formats)


def transition_to_jupytext_section_in_metadata(metadata, is_ipynb):
Expand All @@ -344,7 +361,8 @@ def transition_to_jupytext_section_in_metadata(metadata, is_ipynb):
if 'jupytext_formats' in metadata:
metadata.setdefault('jupytext', {})['formats'] = metadata.pop('jupytext_formats')
if 'jupytext_format_version' in metadata:
metadata.setdefault('jupytext', {})['this_document'] = {'format_version': metadata.pop('jupytext_format_version')}
metadata.setdefault('jupytext', {})['text_representation'] = {
'format_version': metadata.pop('jupytext_format_version')}
if 'main_language' in metadata:
metadata.setdefault('jupytext', {})['main_language'] = metadata.pop('main_language')
for entry in ['encoding', 'executable']:
Expand Down
10 changes: 6 additions & 4 deletions jupytext/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def encoding_and_executable(notebook, ext):
return lines


def metadata_and_cell_to_header(notebook, text_format):
def metadata_and_cell_to_header(notebook, text_format, ext):
"""
Return the text header corresponding to a notebook, and remove the
first cell of the notebook if it contained the header
Expand All @@ -94,9 +94,11 @@ def metadata_and_cell_to_header(notebook, text_format):
metadata = _as_dict(notebook.get('metadata', {}))

if insert_or_test_version_number():
metadata.setdefault('jupytext', {})['this_document'] = {'format_name': text_format.format_name,
'format_version': text_format.current_version_number,
'jupytext_version': __version__}
metadata.setdefault('jupytext', {})['text_representation'] = {
'extension': ext,
'format_name': text_format.format_name,
'format_version': text_format.current_version_number,
'jupytext_version': __version__}

if 'jupytext' in metadata and not metadata['jupytext']:
del metadata['jupytext']
Expand Down
4 changes: 3 additions & 1 deletion jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def writes(self, nb, **kwargs):
del nb.metadata['jupytext']['main_language']

lines = encoding_and_executable(nb, self.ext)
lines.extend(metadata_and_cell_to_header(nb, self.format))
lines.extend(metadata_and_cell_to_header(nb, self.format, self.ext))

cell_exporters = []
for cell in nb.cells:
Expand Down Expand Up @@ -130,6 +130,8 @@ def reads(text, ext, format_name=None,
if format_name == 'sphinx-rst2md' and rst2md:
format_name = 'sphinx'
update_jupytext_formats_metadata(notebook, ext, format_name)
notebook.metadata.setdefault('jupytext', {}).setdefault('text_representation', {}).update(
{'extension': ext, 'format_name': format_name})

return notebook

Expand Down

0 comments on commit bf8b476

Please sign in to comment.