|
22 | 22 | # # |
23 | 23 | ################################################################################ |
24 | 24 |
|
25 | | -# stdlib |
26 | | -import os |
27 | | -import pathlib |
28 | | -from typing import Union |
29 | | - |
30 | 25 | # 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 |
31 | 31 | from nbconvert import PythonExporter # type: ignore |
32 | 32 |
|
| 33 | +# this package |
| 34 | +import notebook2script |
| 35 | + |
33 | 36 | __all__ = ["convert_notebook"] |
34 | 37 |
|
35 | 38 | py_exporter = PythonExporter() |
36 | 39 |
|
37 | 40 |
|
38 | 41 | 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, |
41 | 44 | ): |
42 | 45 | """ |
43 | 46 | Convert a notebook to a python file. |
44 | 47 |
|
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. |
47 | 50 | """ |
48 | 51 |
|
| 52 | + nb_file = PathPlus(nb_file) |
| 53 | + outfile = PathPlus(outfile) |
| 54 | + outfile.parent.maybe_make() |
| 55 | + |
49 | 56 | script, *_ = py_exporter.from_file(str(nb_file)) |
50 | 57 |
|
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() |
53 | 83 |
|
54 | | - if not outfile.parent.is_dir(): |
55 | | - outfile.parent.mkdir(parents=True) |
| 84 | + return ret |
56 | 85 |
|
57 | | - with outfile.open('w') as fp: |
58 | | - fp.write(script) |
| 86 | + finally: |
| 87 | + isort.settings.CONFIG_SECTIONS = old_isort_settings |
0 commit comments