33# python -m wvpy.pysheet test.py
44# python -m wvpy.pysheet test.ipynb
55
6+ from typing import Iterable
67import argparse
78import os
89import shutil
1112from wvpy .jtools import convert_py_file_to_notebook , convert_notebook_file_to_py
1213
1314
14- def main () -> int :
15- parser = argparse .ArgumentParser (description = "Convert between .py and .ipynb or back (can have suffix, or guess suffix)" )
16- parser .add_argument ('--quiet' , action = 'store_true' , help = 'quite operation' )
17- parser .add_argument ('--delete' , action = 'store_true' , help = 'delete input file' )
18- parser .add_argument ('--black' , action = 'store_true' , help = 'use black to re-format cells' )
19- parser .add_argument (
20- 'infile' ,
21- metavar = 'infile' ,
22- type = str ,
23- nargs = '+' ,
24- help = 'name of input file(s)' )
25- args = parser .parse_args ()
15+ def pysheet (
16+ infiles : Iterable [str ],
17+ * ,
18+ quiet : bool = False ,
19+ delete : bool = False ,
20+ black : bool = False ,
21+ ) -> int :
22+ """
23+ Convert between .ipynb and .py files.
24+
25+ :param infiles: list of file names to process
26+ :param quiet: if True do the work quietly
27+ :param delete: if True, delete input
28+ :param black: if True, use black to re-format Python code cells
29+ :return: 0 if successful
30+ """
2631 # some pre-checks
27- assert len (args .infile ) > 0
28- assert len (set (args .infile )) == len (args .infile )
29- assert isinstance (args .quiet , bool )
30- assert isinstance (args .delete , bool )
31- assert isinstance (args .black , bool )
32+ assert not isinstance (infiles , str ) # common error
33+ infiles = list (infiles )
34+ assert len (infiles ) > 0
35+ assert len (set (infiles )) == len (infiles )
36+ assert isinstance (quiet , bool )
37+ assert isinstance (delete , bool )
38+ assert isinstance (black , bool )
3239 # set up the work request
3340 base_names_seen = set ()
3441 input_suffices_seen = set ()
3542 tasks = []
3643 other_suffix = {'.py' : '.ipynb' , '.ipynb' : '.py' }
37- for input_file_name in args . infile :
44+ for input_file_name in infiles :
3845 assert isinstance (input_file_name , str )
3946 assert len (input_file_name ) > 0
4047 suffix_seen = 'error' # placeholder/sentinel
@@ -66,51 +73,73 @@ def main() -> int:
6673 tasks .append ((input_file_name , output_file_name ))
6774 # do the work
6875 for input_file_name , output_file_name in tasks :
69- if not args . quiet :
76+ if not quiet :
7077 print (f'from "{ input_file_name } " to "{ output_file_name } "' )
7178 # back up result target if present
7279 if os .path .exists (output_file_name ):
7380 output_backup_file = f'{ output_file_name } ~'
74- if not args . quiet :
81+ if not quiet :
7582 print (f' copying previous output target "{ output_file_name } " to "{ output_backup_file } "' )
7683 shutil .copy2 (output_file_name , output_backup_file )
7784 # convert
7885 if input_file_name .endswith ('.py' ):
79- if not args . quiet :
86+ if not quiet :
8087 print (f" converting Python { input_file_name } to Jupyter notebook { output_file_name } " )
8188 convert_py_file_to_notebook (
8289 py_file = input_file_name ,
8390 ipynb_file = output_file_name ,
84- use_black = args . black ,
91+ use_black = black ,
8592 )
8693 elif input_file_name .endswith ('.ipynb' ):
87- if not args . quiet :
94+ if not quiet :
8895 print (f' converting Jupyter notebook "{ input_file_name } " to Python "{ output_file_name } "' )
8996 convert_notebook_file_to_py (
9097 ipynb_file = input_file_name ,
9198 py_file = output_file_name ,
92- use_black = args . black ,
99+ use_black = black ,
93100 )
94101 else :
95102 raise ValueError ("input file name must end with .py or .ipynb" )
96103 # do any deletions
97- if args . delete :
104+ if delete :
98105 input_backup_file = f'{ input_file_name } ~'
99- if not args . quiet :
106+ if not quiet :
100107 print (f" moving input { input_file_name } to { input_backup_file } " )
101108 try :
102109 os .remove (input_backup_file )
103110 except FileNotFoundError :
104111 pass
105112 os .rename (input_file_name , input_backup_file )
106- if not args . quiet :
113+ if not quiet :
107114 print ()
108115 return 0
109116
110117
111118if __name__ == '__main__' :
112119 try :
113- ret = main ()
120+ parser = argparse .ArgumentParser (description = "Convert between .py and .ipynb or back (can have suffix, or guess suffix)" )
121+ parser .add_argument ('--quiet' , action = 'store_true' , help = 'quite operation' )
122+ parser .add_argument ('--delete' , action = 'store_true' , help = 'delete input file' )
123+ parser .add_argument ('--black' , action = 'store_true' , help = 'use black to re-format cells' )
124+ parser .add_argument (
125+ 'infile' ,
126+ metavar = 'infile' ,
127+ type = str ,
128+ nargs = '+' ,
129+ help = 'name of input file(s)' )
130+ args = parser .parse_args ()
131+ # some pre-checks
132+ assert len (args .infile ) > 0
133+ assert len (set (args .infile )) == len (args .infile )
134+ assert isinstance (args .quiet , bool )
135+ assert isinstance (args .delete , bool )
136+ assert isinstance (args .black , bool )
137+ ret = pysheet (
138+ infiles = args .infile ,
139+ quiet = args .quiet ,
140+ delete = args .delete ,
141+ black = args .black ,
142+ )
114143 sys .exit (ret )
115144 except AssertionError :
116145 _ , _ , tb = sys .exc_info ()
@@ -120,4 +149,3 @@ def main() -> int:
120149 except Exception as ex :
121150 print (ex )
122151 sys .exit (- 1 )
123-
0 commit comments