Skip to content

Commit

Permalink
Reflowing code and text
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 29, 2018
1 parent 9ad17ca commit ce56823
Showing 1 changed file with 33 additions and 59 deletions.
92 changes: 33 additions & 59 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@

def _jupytext_writes(ext, format_name):
def _writes(nbk, version=nbformat.NO_CONVERT, **kwargs):
return jupytext.writes(nbk, version=version, ext=ext,
format_name=format_name, **kwargs)
return jupytext.writes(nbk, version=version, ext=ext, format_name=format_name, **kwargs)

return _writes


def _jupytext_reads(ext, format_name, rst2md):
def _reads(text, as_version, **kwargs):
return jupytext.reads(text, ext=ext,
format_name=format_name, rst2md=rst2md,
as_version=as_version, **kwargs)
return jupytext.reads(text, ext=ext, format_name=format_name, rst2md=rst2md, as_version=as_version, **kwargs)

return _reads

Expand All @@ -51,10 +48,8 @@ def check_formats(formats):
if not isinstance(formats, list):
formats = [group.split(',') for group in formats.split(';')]

expected_format = ("Notebook metadata 'jupytext_formats' should "
"be a list of extension groups, like 'ipynb,Rmd'.\n"
"Groups can be separated with colon, for instance: "
"'ipynb,nb.py;script.ipynb,py'")
expected_format = ("Notebook metadata 'jupytext_formats' should be a list of extension groups, like 'ipynb,Rmd'.\n"
"Groups can be separated with colon, for instance: 'ipynb,nb.py;script.ipynb,py'")

allowed_extension = NOTEBOOK_EXTENSIONS + ['auto']

Expand All @@ -73,8 +68,7 @@ def check_formats(formats):
continue
fmt, _ = parse_one_format(fmt)
if not any([fmt.endswith(ext) for ext in allowed_extension]):
raise ValueError('Group extension {} contains {}, '
'which does not end with either {}.\n{}'
raise ValueError('Group extension {} contains {}, which does not end with either {}.\n{}'
.format(str(group), fmt, str(allowed_extension), expected_format))
if fmt.endswith('.ipynb'):
has_ipynb = True
Expand Down Expand Up @@ -156,8 +150,8 @@ def all_nb_extensions(self):

outdated_text_notebook_margin = Float(
1.0,
help='Refuse to overwrite inputs of a ipynb notebooks with those of a'
'text notebook when the text notebook plus margin is older than'
help='Refuse to overwrite inputs of a ipynb notebooks with those of a '
'text notebook when the text notebook plus margin is older than '
'the ipynb notebook',
config=True)

Expand Down Expand Up @@ -226,11 +220,9 @@ def _read_notebook(self, os_path, as_version=4):
with mock.patch('nbformat.reads',
_jupytext_reads(fmt, format_name,
self.sphinx_convert_rst2md)):
return super(TextFileContentsManager, self) \
._read_notebook(os_path, as_version)
return super(TextFileContentsManager, self)._read_notebook(os_path, as_version)
else:
return super(TextFileContentsManager, self) \
._read_notebook(os_path, as_version)
return super(TextFileContentsManager, self)._read_notebook(os_path, as_version)

def set_comment_magics_if_none(self, nb):
"""Set the 'comment_magics' metadata if default is not None"""
Expand All @@ -247,8 +239,7 @@ def _save_notebook(self, os_path, nb):
alt_ext = '.' + alt_fmt.split('.')[-1]

if alt_ext in self.nb_extensions:
format_name = format_name_for_ext(nb.metadata, alt_fmt,
self.default_jupytext_formats,
format_name = format_name_for_ext(nb.metadata, alt_fmt, self.default_jupytext_formats,
explicit_default=False) or \
self.preferred_format(alt_fmt, self.preferred_jupytext_formats_save)
with mock.patch('nbformat.writes',
Expand All @@ -263,9 +254,7 @@ def get(self, path, content=True, type=None, format=None,
path = path.strip('/')
nb_file, fmt, ext = file_fmt_ext(path)

if self.exists(path) and \
(type == 'notebook' or
(type is None and ext in self.all_nb_extensions())):
if self.exists(path) and (type == 'notebook' or (type is None and ext in self.all_nb_extensions())):
model = self._notebook_model(path, content=content)
if fmt != ext and content:
model['name'], _ = os.path.splitext(model['name'])
Expand All @@ -283,15 +272,13 @@ def get(self, path, content=True, type=None, format=None,
# Source format is first non ipynb format found on disk
if fmt.endswith('.ipynb'):
for alt_fmt in fmt_group:
if not alt_fmt.endswith('.ipynb') and \
self.exists(nb_file + alt_fmt):
if not alt_fmt.endswith('.ipynb') and self.exists(nb_file + alt_fmt):
source_format = alt_fmt
break
# Outputs taken from ipynb if in group, if file exists
else:
for alt_fmt in fmt_group:
if alt_fmt.endswith('.ipynb') and \
self.exists(nb_file + alt_fmt):
if alt_fmt.endswith('.ipynb') and self.exists(nb_file + alt_fmt):
outputs_format = alt_fmt
break

Expand All @@ -305,60 +292,49 @@ def get(self, path, content=True, type=None, format=None,
elif outputs_format != fmt:
self.log.info(u'Reading OUTPUTS from {}'.format(
os.path.basename(nb_file + outputs_format)))
model_outputs = self.get(nb_file + outputs_format,
content=content,
model_outputs = self.get(nb_file + outputs_format, content=content,
type=type, format=format,
load_alternative_format=False)
else:
model_outputs = None

try:
check_file_version(model['content'],
nb_file + source_format,
nb_file + outputs_format)
check_file_version(model['content'], nb_file + source_format, nb_file + outputs_format)
except ValueError as err:
raise HTTPError(400, str(err))

# Make sure we're not overwriting ipynb cells with an outdated
# text file
try:
if model_outputs and model_outputs['last_modified'] > \
model['last_modified'] + \
if model_outputs and model_outputs['last_modified'] > model['last_modified'] + \
timedelta(seconds=self.outdated_text_notebook_margin):
raise HTTPError(
400,
u'\n'
'{out} (last modified {out_last})\n'
'seems more recent than '
'{src} (last modified {src_last})\n'
'Please either:\n'
'- open {src} in a text editor, make sure it is '
'up to date, and save it,\n'
'- or delete {src} if not up to date,\n'
'- or increase check margin by adding, say, \n'
' c.ContentsManager.'
'outdated_text_notebook_margin = 5 '
'# in seconds # or float("inf")\n'
'to your .jupyter/jupyter_notebook_config.py '
'file\n'.format(src=nb_file + source_format,
src_last=model['last_modified'],
out=nb_file + outputs_format,
out_last=model_outputs[
'last_modified']))
'''{out} (last modified {out_last})
seems more recent than {src} (last modified {src_last})
Please either:
- open {src} in a text editor, make sure it is up to date, and save it,
- or delete {src} if not up to date,
- or increase check margin by adding, say,
c.ContentsManager.outdated_text_notebook_margin = 5 # in seconds # or float("inf")
to your .jupyter/jupyter_notebook_config.py file
'''.format(src=nb_file + source_format,
src_last=model['last_modified'],
out=nb_file + outputs_format,
out_last=model_outputs[
'last_modified']))
except OverflowError:
pass

if model_outputs:
combine_inputs_with_outputs(model['content'],
model_outputs['content'])
combine_inputs_with_outputs(model['content'], model_outputs['content'])
elif not fmt.endswith('.ipynb'):
self.notary.sign(model['content'])
self.mark_trusted_cells(model['content'], path)

return model

return super(TextFileContentsManager, self) \
.get(path, content, type, format)
return super(TextFileContentsManager, self).get(path, content, type, format)

def trust_notebook(self, path):
"""Trust the current notebook"""
Expand All @@ -382,8 +358,6 @@ def rename_file(self, old_path, new_path):
if org_fmt == new_fmt:
for alt_fmt in self.format_group(org_fmt):
if self.file_exists(old_file + alt_fmt):
super(TextFileContentsManager, self) \
.rename_file(old_file + alt_fmt, new_file + alt_fmt)
super(TextFileContentsManager, self).rename_file(old_file + alt_fmt, new_file + alt_fmt)
else:
super(TextFileContentsManager, self) \
.rename_file(old_path, new_path)
super(TextFileContentsManager, self).rename_file(old_path, new_path)

0 comments on commit ce56823

Please sign in to comment.