2525import os
2626import pathlib
2727import sys
28- import warnings
2928from typing import Iterable , Union
3029
3130# 3rd party
4039linter = Pointless ()
4140
4241
43- def main () -> None :
42+ def main () -> int :
4443
4544 parser = argparse .ArgumentParser (description = "Convert Jupyter Notebooks to Python scripts" )
4645
@@ -63,20 +62,19 @@ def main() -> None:
6362 if not notebooks :
6463 parser .error ("the following arguments are required: NOTEBOOK" )
6564
66- process_multiple_notebooks (notebooks , args .outdir , overwrite = args .overwrite )
65+ return process_multiple_notebooks (notebooks , args .outdir , overwrite = args .overwrite )
6766
6867
6968def process_multiple_notebooks (
7069 notebooks : Iterable [Union [str , pathlib .Path ]],
7170 outdir : Union [str , pathlib .Path , os .PathLike ],
7271 overwrite : bool = False ,
73- ) -> None :
72+ ) -> int :
7473 """
7574
7675 :param notebooks: An iterable of notebook filenames to process
7776 :param outdir: The directory to store the Python output in.
7877 :param overwrite: Whether to overwrite existing files. Default :py:obj:`False`
79- :type overwrite: bool
8078 """
8179
8280 if not isinstance (outdir , pathlib .Path ):
@@ -90,18 +88,23 @@ def process_multiple_notebooks(
9088 # print(all_notebooks)
9189 # input(">")
9290
91+ ret = 0
92+
9393 for notebook in all_notebooks :
9494 notebook = pathlib .Path (notebook )
9595 outfile = outdir / f"{ notebook .stem } .py"
9696
9797 if outfile .is_file () and not overwrite :
98- warnings . warn (f"Skipping existing file { outfile } " )
98+ print (f"Info: Skipping existing file { outfile } " )
9999 else :
100100 if notebook .is_file ():
101101 print (f"Converting { notebook } to { outfile } " )
102102 process_notebook (notebook , outfile )
103103 else :
104104 print (f"{ notebook } not found" )
105+ ret |= 1
106+
107+ return ret
105108
106109
107110def process_notebook (notebook , outfile : Union [str , pathlib .Path , os .PathLike ]) -> None :
@@ -119,6 +122,6 @@ def process_notebook(notebook, outfile: Union[str, pathlib.Path, os.PathLike]) -
119122
120123if __name__ == "__main__" :
121124 try :
122- main ()
125+ sys . exit ( main () )
123126 except KeyboardInterrupt :
124127 sys .exit (1 )
0 commit comments