From 9f954f7687259c297d5e97135a20993bf96661b5 Mon Sep 17 00:00:00 2001 From: Danilo Silva Date: Tue, 5 Feb 2019 17:32:08 -0200 Subject: [PATCH 1/6] v1.8.0 - Ignore Python files generated by Jupytext --- CHANGELOG.rst | 6 ++++++ esss_fix_format/cli.py | 10 ++++++++-- setup.py | 2 +- tests/test_esss_fix_format.py | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9b4ea72..02f77a8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ History ======= +1.8.0 +---------- + +* Ignore Python files generated by `Jupytext`_. + +.. _`Jupytext`: https://github.com/mwouts/jupytext 1.7.0 ---------- diff --git a/esss_fix_format/cli.py b/esss_fix_format/cli.py index b93635e..f42542a 100644 --- a/esss_fix_format/cli.py +++ b/esss_fix_format/cli.py @@ -32,13 +32,19 @@ 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.""" 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: + return not 'jupytext' in f.read() + return any(fnmatch(os.path.basename(filename), p) for p in PATTERNS) # caches which directories have the `.clang-format` file, *in or above it*, to avoid hitting the diff --git a/setup.py b/setup.py index a682ee8..a6ae8b9 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/test_esss_fix_format.py b/tests/test_esss_fix_format.py index 0c82df8..eb91e18 100644 --- a/tests/test_esss_fix_format.py +++ b/tests/test_esss_fix_format.py @@ -211,6 +211,23 @@ 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') + + # wrongly formatted, but should skip as a notebook with same base name exists + filename_py.write(u'print( "Hello World" )', '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') From 031fc9d12bdc02e7d8c6411d23c25d981d7da48f Mon Sep 17 00:00:00 2001 From: Danilo Silva Date: Tue, 5 Feb 2019 17:43:47 -0200 Subject: [PATCH 2/6] Make flake8 be happy --- esss_fix_format/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esss_fix_format/cli.py b/esss_fix_format/cli.py index f42542a..ecabead 100644 --- a/esss_fix_format/cli.py +++ b/esss_fix_format/cli.py @@ -43,7 +43,7 @@ def should_format(filename): # 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: - return not 'jupytext' in f.read() + return not ('jupytext' in f.read()) return any(fnmatch(os.path.basename(filename), p) for p in PATTERNS) From 79129bcbf1f57b283e31211cf1464acb4f8b2538 Mon Sep 17 00:00:00 2001 From: Danilo Silva Date: Tue, 5 Feb 2019 17:48:18 -0200 Subject: [PATCH 3/6] Remove outdated comment --- tests/test_esss_fix_format.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_esss_fix_format.py b/tests/test_esss_fix_format.py index eb91e18..04fae2d 100644 --- a/tests/test_esss_fix_format.py +++ b/tests/test_esss_fix_format.py @@ -221,7 +221,6 @@ def test_ignore_jupytext(tmpdir, sort_cfg_to_tmpdir, notebook_content, expected_ filename_py = tmpdir.join('test.py') filename_ipynb = tmpdir.join('test.ipynb') - # wrongly formatted, but should skip as a notebook with same base name exists filename_py.write(u'print( "Hello World" )', 'w') filename_ipynb.write(notebook_content, 'w') From 98d4adb87cbf57bd4d411264477f7a2246b354bc Mon Sep 17 00:00:00 2001 From: Danilo Silva Date: Tue, 5 Feb 2019 18:14:33 -0200 Subject: [PATCH 4/6] Fix readability minor --- esss_fix_format/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esss_fix_format/cli.py b/esss_fix_format/cli.py index ecabead..70ca789 100644 --- a/esss_fix_format/cli.py +++ b/esss_fix_format/cli.py @@ -43,7 +43,7 @@ def should_format(filename): # 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: - return not ('jupytext' in f.read()) + return 'jupytext' not in f.read() return any(fnmatch(os.path.basename(filename), p) for p in PATTERNS) From 0b1c4b31185df43f855a07ae2a7bf7ff876a6295 Mon Sep 17 00:00:00 2001 From: Danilo Silva Date: Tue, 5 Feb 2019 18:48:20 -0200 Subject: [PATCH 5/6] Distinguish output for Jupytext generated file and Unknown file type - should_format return skip reason --- esss_fix_format/cli.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/esss_fix_format/cli.py b/esss_fix_format/cli.py index 70ca789..8e7550e 100644 --- a/esss_fix_format/cli.py +++ b/esss_fix_format/cli.py @@ -36,15 +36,21 @@ def is_cpp(filename): 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 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: - return 'jupytext' not in f.read() - return any(fnmatch(os.path.basename(filename), p) for p in PATTERNS) + if 'jupytext' in f.read(): + 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 @@ -161,8 +167,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): From 03015c9d5270832d9e0c52c75f1ace4452c9ac78 Mon Sep 17 00:00:00 2001 From: Danilo Silva Date: Wed, 6 Feb 2019 09:44:19 -0200 Subject: [PATCH 6/6] Also look for jupytext: key in Jupytext generated .py --- esss_fix_format/cli.py | 8 ++++++-- tests/test_esss_fix_format.py | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/esss_fix_format/cli.py b/esss_fix_format/cli.py index 8e7550e..b6a53c1 100644 --- a/esss_fix_format/cli.py +++ b/esss_fix_format/cli.py @@ -46,8 +46,12 @@ def should_format(filename): # 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' in f.read(): - return False, 'Jupytext generated file' + 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' diff --git a/tests/test_esss_fix_format.py b/tests/test_esss_fix_format.py index 04fae2d..20a60f3 100644 --- a/tests/test_esss_fix_format.py +++ b/tests/test_esss_fix_format.py @@ -221,7 +221,29 @@ def test_ignore_jupytext(tmpdir, sort_cfg_to_tmpdir, notebook_content, expected_ filename_py = tmpdir.join('test.py') filename_ipynb = tmpdir.join('test.ipynb') - filename_py.write(u'print( "Hello World" )', 'w') + 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)