Skip to content

Commit 032ed50

Browse files
committed
Use yapf-isort for reformat output files.
1 parent fe3132e commit 032ed50

9 files changed

Lines changed: 459 additions & 17 deletions

File tree

.isort.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ known_third_party =
1818
astroid
1919
coverage
2020
coverage_pyver_pragma
21+
domdf_python_tools
2122
github
2223
nbconvert
2324
pylint
@@ -27,5 +28,6 @@ known_third_party =
2728
pytest_rerunfailures
2829
pytest_timeout
2930
requests
31+
yapf_isort
3032
known_first_party = notebook2script
3133
remove_redundant_aliases = True

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ include __pkginfo__.py
22
include LICENSE
33
include requirements.txt
44
prune **/__pycache__
5+
include notebook2script/isort.cfg
6+
include notebook2script/style.yapf
57
recursive-include notebook2script *.pyi
68
include notebook2script/py.typed

notebook2script/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def process_multiple_notebooks(
8484
for notebook in notebooks:
8585
all_notebooks += glob.glob(str(notebook))
8686

87-
print(all_notebooks)
87+
# print(all_notebooks)
8888
# input(">")
8989

9090
for notebook in all_notebooks:

notebook2script/ipynb2py.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,66 @@
2222
# #
2323
################################################################################
2424

25-
# stdlib
26-
import os
27-
import pathlib
28-
from typing import Union
29-
3025
# 3rd party
26+
import isort
27+
import yapf_isort
28+
from domdf_python_tools.compat import importlib_resources
29+
from domdf_python_tools.paths import PathPlus
30+
from domdf_python_tools.typing import PathLike
3131
from nbconvert import PythonExporter # type: ignore
3232

33+
# this package
34+
import notebook2script
35+
3336
__all__ = ["convert_notebook"]
3437

3538
py_exporter = PythonExporter()
3639

3740

3841
def convert_notebook(
39-
nb_file: Union[str, pathlib.Path],
40-
outfile: Union[str, pathlib.Path, os.PathLike],
42+
nb_file: PathLike,
43+
outfile: PathLike,
4144
):
4245
"""
4346
Convert a notebook to a python file.
4447
45-
:param nb_file: Filename of the Jupyter Notebook to convert
46-
:param outfile: Filename to save the output script as
48+
:param nb_file: Filename of the Jupyter Notebook to convert.
49+
:param outfile: Filename to save the output script as.
4750
"""
4851

52+
nb_file = PathPlus(nb_file)
53+
outfile = PathPlus(outfile)
54+
outfile.parent.maybe_make()
55+
4956
script, *_ = py_exporter.from_file(str(nb_file))
5057

51-
if not isinstance(outfile, pathlib.Path):
52-
outfile = pathlib.Path(outfile)
58+
outfile.write_clean(script)
59+
60+
with importlib_resources.path(notebook2script, "isort.cfg") as isort_config:
61+
with importlib_resources.path(notebook2script, "style.yapf") as yapf_style:
62+
reformat_file(outfile, yapf_style=str(yapf_style), isort_config_file=str(isort_config))
63+
64+
65+
def reformat_file(filename: PathLike, yapf_style: str, isort_config_file: str) -> int:
66+
"""
67+
Reformat the given file.
68+
69+
:param filename:
70+
:param yapf_style: The name of the yapf style, or the path to the yapf style file.
71+
:param isort_config_file: The filename of the isort configuration file.
72+
"""
73+
74+
old_isort_settings = isort.settings.CONFIG_SECTIONS.copy()
75+
76+
try:
77+
isort.settings.CONFIG_SECTIONS["isort.cfg"] = ("settings", "isort")
78+
79+
isort_config = isort.Config(settings_file=str(isort_config_file))
80+
r = yapf_isort.Reformatter(filename, yapf_style, isort_config)
81+
ret = r.run()
82+
r.to_file()
5383

54-
if not outfile.parent.is_dir():
55-
outfile.parent.mkdir(parents=True)
84+
return ret
5685

57-
with outfile.open('w') as fp:
58-
fp.write(script)
86+
finally:
87+
isort.settings.CONFIG_SECTIONS = old_isort_settings

notebook2script/isort.cfg

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[settings]
2+
line_length = 115
3+
force_to_top = True
4+
indent = " "
5+
multi_line_output = 8
6+
balanced_wrapping = False
7+
lines_between_types = 0
8+
use_parentheses = True
9+
default_section = THIRDPARTY
10+
remove_redundant_aliases = True

notebook2script/pointless.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def process_file(self, filename):
207207
line = file_lines[lineno]
208208
line_pre_statement = line[:col]
209209
line_post_statement = line[col + len(value):]
210-
print(f"{line_pre_statement}print({value}){line_post_statement}")
210+
# print(f"{line_pre_statement}print({value}){line_post_statement}")
211211
file_lines[lineno] = f"{line_pre_statement}print({value}){line_post_statement}"
212212

213213
if file_lines[-1]:

0 commit comments

Comments
 (0)