• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PyMassSpec / notebook2script / 14915518696

08 May 2025 08:28PM UTC coverage: 90.698% (-0.3%) from 90.96%
14915518696

push

github

web-flow
Updated files with 'repo_helper'. (#53)

Co-authored-by: repo-helper[bot] <74742576+repo-helper[bot]@users.noreply.github.com>

156 of 172 relevant lines covered (90.7%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/notebook2script/__init__.py
1
#!/usr/bin/env python3
2
#
3
#  __init__.py
4
"""
5
Convert Jupyter Notebooks to Python Scripts.
6
"""
7
#
8
#  Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
9
#
10
#  This program is free software; you can redistribute it and/or modify
11
#  it under the terms of the GNU General Public License version 2
12
#  as published by the Free Software Foundation.
13
#
14
#  This program is distributed in the hope that it will be useful,
15
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
#  GNU General Public License for more details.
18
#
19
#  You should have received a copy of the GNU General Public License
20
#  along with this program; if not, write to the Free Software
21
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#  MA 02110-1301, USA.
23
#
24

25
# stdlib
26
import os
1✔
27
from typing import Iterable
1✔
28

29
# 3rd party
30
import isort
1✔
31
import yapf_isort
1✔
32
from domdf_python_tools.compat import importlib_resources
1✔
33
from domdf_python_tools.paths import PathPlus
1✔
34
from domdf_python_tools.typing import PathLike
1✔
35
from nbconvert import PythonExporter
1✔
36
from pre_commit_hooks.fix_encoding_pragma import fix_encoding_pragma  # type: ignore[import-untyped]
1✔
37

38
# this package
39
from notebook2script.pointless import Pointless
1✔
40

41
__author__: str = "Dominic Davis-Foster"
1✔
42
__copyright__: str = "2020-2021 Dominic Davis-Foster"
1✔
43
__license__: str = "GPLv2"
1✔
44
__version__: str = "0.2.1"
1✔
45
__email__: str = "dominic@davis-foster.co.uk"
1✔
46

47
__all__ = [
1✔
48
                "process_multiple_notebooks",
49
                "convert_notebook",
50
                "reformat_file",
51
                ]
52

53
linter = Pointless()
1✔
54
py_exporter = PythonExporter()
1✔
55

56

57
def convert_notebook(
1✔
58
                nb_file: PathLike,
59
                outfile: PathLike,
60
                ) -> None:
61
        """
62
        Convert a notebook to a Python file.
63

64
        :param nb_file: Filename of the Jupyter Notebook to convert.
65
        :param outfile: The filename to store the Python output as.
66
        """
67

68
        nb_file = PathPlus(nb_file)
1✔
69
        outfile = PathPlus(outfile)
1✔
70
        outfile.parent.maybe_make()
1✔
71

72
        script, *_ = py_exporter.from_file(str(nb_file))
1✔
73

74
        outfile.write_clean(script)
1✔
75

76
        with importlib_resources.path("notebook2script", "isort.cfg") as isort_config:
1✔
77
                with importlib_resources.path("notebook2script", "style.yapf") as yapf_style:
1✔
78
                        reformat_file(outfile, yapf_style=str(yapf_style), isort_config_file=str(isort_config))
1✔
79

80
        linter.process_file(os.fspath(outfile))
1✔
81

82
        with outfile.open("r+b") as f:
1✔
83
                fix_encoding_pragma(f, remove=True, expected_pragma=b"# coding: utf-8")
1✔
84

85

86
def reformat_file(filename: PathLike, yapf_style: str, isort_config_file: str) -> int:
1✔
87
        """
88
        Reformat the given file.
89

90
        :param filename:
91
        :param yapf_style: The name of the yapf style, or the path to the yapf style file.
92
        :param isort_config_file: The filename of the isort configuration file.
93
        """
94

95
        old_isort_settings = isort.settings.CONFIG_SECTIONS.copy()
1✔
96

97
        try:
1✔
98
                isort.settings.CONFIG_SECTIONS["isort.cfg"] = ("settings", "isort")
1✔
99

100
                isort_config = isort.Config(settings_file=str(isort_config_file))
1✔
101
                r = yapf_isort.Reformatter(filename, yapf_style, isort_config)
1✔
102
                ret = r.run()
1✔
103
                r.to_file()
1✔
104

105
                return ret
1✔
106

107
        finally:
108
                isort.settings.CONFIG_SECTIONS = old_isort_settings
1✔
109

110

111
def process_multiple_notebooks(
1✔
112
                notebooks: Iterable[PathLike],
113
                outdir: PathLike,
114
                overwrite: bool = False,
115
                ) -> int:
116
        """
117
        Process multiple Jupyter notebooks for conversion into Python scripts.
118

119
        :param notebooks: An iterable of notebook filenames to process
120
        :param outdir: The directory to store the Python output in.
121
        :param overwrite: Whether to overwrite existing files.
122
        """
123

124
        ret = 0
1✔
125
        outdir = PathPlus(outdir)
1✔
126

127
        for notebook in notebooks:
1✔
128
                notebook = PathPlus(notebook)
1✔
129
                outfile = outdir / f"{notebook.stem}.py"
1✔
130

131
                if outfile.is_file() and not overwrite:
1✔
132
                        print(f"Info: Skipping existing file {outfile}")
×
133
                else:
134
                        if notebook.is_file():
1✔
135
                                print(f"Converting {notebook} to {outfile}")
1✔
136
                                convert_notebook(notebook, outfile)
1✔
137
                        else:
138
                                print(f"{notebook} not found")
×
139
                                ret |= 1
×
140

141
        return ret
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc