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

Commit f0a8f48

Browse files
Merge pull request #39 from ESSS/ignore-jupytext-files
v1.8.0 - Ignore Python files generated by Jupytext
2 parents c1190b1 + 03015c9 commit f0a8f48

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
History
33
=======
44

5+
1.8.0
6+
----------
7+
8+
* Ignore Python files generated by `Jupytext`_.
9+
10+
.. _`Jupytext`: https://github.com/mwouts/jupytext
511

612
1.7.0
713
----------

esss_fix_format/cli.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,29 @@
3232
def is_cpp(filename):
3333
"""Return True if the filename is of a type that should be treated as C++ source."""
3434
from fnmatch import fnmatch
35-
return any(fnmatch(os.path.split(filename)[-1], p) for p in CPP_PATTERNS)
35+
return any(fnmatch(os.path.basename(filename), p) for p in CPP_PATTERNS)
3636

3737

3838
def should_format(filename):
39-
"""Return True if the filename is of a type that is supported by this tool."""
39+
"""
40+
Return a tuple (fmt, reason) where fmt is True if the filename
41+
is of a type that is supported by this tool.
42+
"""
4043
from fnmatch import fnmatch
41-
return any(fnmatch(os.path.split(filename)[-1], p) for p in PATTERNS)
44+
filename_no_ext, ext = os.path.splitext(filename)
45+
ipynb_filename = filename_no_ext + '.ipynb'
46+
# ignore .py file that has a jupytext configured notebook with the same base name
47+
if ext == '.py' and os.path.isfile(ipynb_filename):
48+
with open(ipynb_filename) as f:
49+
if 'jupytext' not in f.read():
50+
return True, ''
51+
with open(filename) as f:
52+
if 'jupytext:' not in f.read():
53+
return True, ''
54+
return False, 'Jupytext generated file'
55+
if any(fnmatch(os.path.basename(filename), p) for p in PATTERNS):
56+
return True, ''
57+
return False, 'Unknown file type'
4258

4359

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

158-
if not should_format(filename):
159-
click.secho(click.format_filename(filename) + ': Unknown file type', fg='white')
174+
fmt, reason = should_format(filename)
175+
176+
if not fmt:
177+
click.secho(click.format_filename(filename) + ': ' + reason, fg='white')
160178
return changed, errors, formatter
161179

162180
if is_cpp(filename):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
setup(
2222
name='esss_fix_format',
23-
version='1.6.0',
23+
version='1.8.0',
2424
description="ESSS code formatter and checker",
2525
long_description=readme + '\n\n' + changelog,
2626
author="ESSS",

tests/test_esss_fix_format.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,44 @@ def test_empty_file(tmpdir, sort_cfg_to_tmpdir):
211211
run([str(filename)], expected_exit=0)
212212

213213

214+
@pytest.mark.parametrize(
215+
'notebook_content, expected_exit', [
216+
(u'"jupytext": {"formats": "ipynb,py"}', 0),
217+
(u'Not a j-u-p-y-t-e-x-t configured notebook', 1),
218+
]
219+
)
220+
def test_ignore_jupytext(tmpdir, sort_cfg_to_tmpdir, notebook_content, expected_exit):
221+
filename_py = tmpdir.join('test.py')
222+
filename_ipynb = tmpdir.join('test.ipynb')
223+
224+
py_content = textwrap.dedent('''\
225+
# -*- coding: utf-8 -*-
226+
# ---
227+
# jupyter:
228+
# jupytext:
229+
# formats: ipynb,py:light
230+
# text_representation:
231+
# extension: .py
232+
# format_name: light
233+
# format_version: '1.3'
234+
# jupytext_version: 0.8.6
235+
# kernelspec:
236+
# display_name: Python 3
237+
# language: python
238+
# name: python3
239+
# ---
240+
import matplotlib.pyplot as plt
241+
''')
242+
243+
filename_py.write(
244+
py_content,
245+
'w'
246+
)
247+
filename_ipynb.write(notebook_content, 'w')
248+
249+
run([str(filename_py), '--check'], expected_exit=expected_exit)
250+
251+
214252
@pytest.mark.parametrize('check', [True, False])
215253
def test_python_with_bom(tmpdir, sort_cfg_to_tmpdir, check):
216254
filename = tmpdir.join('test.py')

0 commit comments

Comments
 (0)