Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #39 from ESSS/ignore-jupytext-files
Browse files Browse the repository at this point in the history
v1.8.0 - Ignore Python files generated by Jupytext
  • Loading branch information
danilomendesdias authored Feb 6, 2019
2 parents c1190b1 + 03015c9 commit f0a8f48
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
History
=======

1.8.0
----------

* Ignore Python files generated by `Jupytext`_.

.. _`Jupytext`: https://github.com/mwouts/jupytext

1.7.0
----------
Expand Down
28 changes: 23 additions & 5 deletions esss_fix_format/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,29 @@
def is_cpp(filename):
"""Return True if the filename is of a type that should be treated as C++ source."""
from fnmatch import fnmatch
return any(fnmatch(os.path.split(filename)[-1], p) for p in CPP_PATTERNS)
return any(fnmatch(os.path.basename(filename), p) for p in CPP_PATTERNS)


def should_format(filename):
"""Return True if the filename is of a type that is supported by this tool."""
"""
Return a tuple (fmt, reason) where fmt is True if the filename
is of a type that is supported by this tool.
"""
from fnmatch import fnmatch
return any(fnmatch(os.path.split(filename)[-1], p) for p in PATTERNS)
filename_no_ext, ext = os.path.splitext(filename)
ipynb_filename = filename_no_ext + '.ipynb'
# ignore .py file that has a jupytext configured notebook with the same base name
if ext == '.py' and os.path.isfile(ipynb_filename):
with open(ipynb_filename) as f:
if 'jupytext' not in f.read():
return True, ''
with open(filename) as f:
if 'jupytext:' not in f.read():
return True, ''
return False, 'Jupytext generated file'
if any(fnmatch(os.path.basename(filename), p) for p in PATTERNS):
return True, ''
return False, 'Unknown file type'


# caches which directories have the `.clang-format` file, *in or above it*, to avoid hitting the
Expand Down Expand Up @@ -155,8 +171,10 @@ def _process_file(filename, check, format_code):
errors = []
formatter = None

if not should_format(filename):
click.secho(click.format_filename(filename) + ': Unknown file type', fg='white')
fmt, reason = should_format(filename)

if not fmt:
click.secho(click.format_filename(filename) + ': ' + reason, fg='white')
return changed, errors, formatter

if is_cpp(filename):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setup(
name='esss_fix_format',
version='1.6.0',
version='1.8.0',
description="ESSS code formatter and checker",
long_description=readme + '\n\n' + changelog,
author="ESSS",
Expand Down
38 changes: 38 additions & 0 deletions tests/test_esss_fix_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,44 @@ def test_empty_file(tmpdir, sort_cfg_to_tmpdir):
run([str(filename)], expected_exit=0)


@pytest.mark.parametrize(
'notebook_content, expected_exit', [
(u'"jupytext": {"formats": "ipynb,py"}', 0),
(u'Not a j-u-p-y-t-e-x-t configured notebook', 1),
]
)
def test_ignore_jupytext(tmpdir, sort_cfg_to_tmpdir, notebook_content, expected_exit):
filename_py = tmpdir.join('test.py')
filename_ipynb = tmpdir.join('test.ipynb')

py_content = textwrap.dedent('''\
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.3'
# jupytext_version: 0.8.6
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
import matplotlib.pyplot as plt
''')

filename_py.write(
py_content,
'w'
)
filename_ipynb.write(notebook_content, 'w')

run([str(filename_py), '--check'], expected_exit=expected_exit)


@pytest.mark.parametrize('check', [True, False])
def test_python_with_bom(tmpdir, sort_cfg_to_tmpdir, check):
filename = tmpdir.join('test.py')
Expand Down

0 comments on commit f0a8f48

Please sign in to comment.