77
88import argparse
99import os
10+ import re
1011import subprocess
1112import sys
1213from datetime import date
14+ from pathlib import Path
1315
1416
1517# --------------------------------------------------
@@ -20,22 +22,44 @@ def get_args():
2022 description = 'Create Python argparse/simple program' ,
2123 formatter_class = argparse .ArgumentDefaultsHelpFormatter )
2224
25+ defaults = get_defaults ()
26+
2327 parser .add_argument ('program' , help = 'Program name' , type = str )
2428
2529 parser .add_argument ('-s' ,
2630 '--simple' ,
2731 help = 'Use simple format' ,
2832 action = 'store_true' )
2933
34+ parser .add_argument ('-n' ,
35+ '--name' ,
36+ type = str ,
37+ default = defaults .get ('name' , os .getenv ('USER' )),
38+ help = 'Name for docstring' )
39+
40+ parser .add_argument ('-e' ,
41+ '--email' ,
42+ type = str ,
43+ default = defaults .get ('email' , '' ),
44+ help = 'Email for docstring' )
45+
46+ parser .add_argument ('-p' ,
47+ '--purpose' ,
48+ type = str ,
49+ default = 'Rock the Casbah' ,
50+ help = 'Purpose for docstring' )
51+
3052 parser .add_argument ('-f' ,
3153 '--force' ,
3254 help = 'Overwrite existing' ,
3355 action = 'store_true' )
3456
3557 args = parser .parse_args ()
3658
37- if not args .program .strip ():
38- parser .error ('program is not a usable filename' )
59+ args .program = args .program .strip ().replace ('-' , '_' )
60+
61+ if not args .program :
62+ parser .error ('Not a usable filename "{}"' .format (args .program ))
3963
4064 return args
4165
@@ -45,38 +69,41 @@ def main():
4569 """Make a jazz noise here"""
4670
4771 args = get_args ()
48- out_file = args .program . strip (). replace ( '-' , '_' )
72+ program = args .program
4973
50- if not out_file .endswith ('.py' ):
51- out_file += '.py'
52-
53- if os .path .isfile (out_file ) and not args .force :
54- answer = input ('"{}" exists. Overwrite? [yN] ' .format (out_file ))
74+ if os .path .isfile (program ) and not args .force :
75+ answer = input ('"{}" exists. Overwrite? [yN] ' .format (program ))
5576 if not answer .lower ().startswith ('y' ):
5677 print ('Will not overwrite. Bye!' )
5778 sys .exit ()
5879
59- out_fh = open (out_file , 'w' )
60- preamble = PREAMBLE .format (os .getenv ('USER' ), str (date .today ()))
61- text = SIMPLE if args .simple else ARGPARSE
80+ header = preamble (name = args .name ,
81+ email = args .email ,
82+ purpose = args .purpose ,
83+ date = str (date .today ()))
84+ text = simple () if args .simple else body ()
6285
63- out_fh .write (preamble )
64- out_fh .write (text )
65- subprocess .run (['chmod' , '+x' , out_file ])
66- print ('Done, see new script "{}."' .format (out_file ))
86+ out_fh = open (program , 'w' )
87+ out_fh .write (header + text )
88+ out_fh .close ()
89+ subprocess .run (['chmod' , '+x' , program ])
90+ print ('Done, see new script "{}."' .format (program ))
6791
6892
6993# --------------------------------------------------
70- PREAMBLE = """#!/usr/bin/env python3
94+ def preamble (** args ):
95+ return f"""#!/usr/bin/env python3
7196\" \" \"
72- Author : {}
73- Date : {}
74- Purpose: Rock the Casbah
97+ Author : { args [ 'name' ] } { ' <' + args [ 'email' ] + '>' if args [ 'email' ] else '' }
98+ Date : { args [ 'date' ] }
99+ Purpose: { args [ 'purpose' ] }
75100\" \" \"
76101"""
77102
103+
78104# --------------------------------------------------
79- SIMPLE = """
105+ def simple ():
106+ return """
80107import os
81108import sys
82109
@@ -101,8 +128,10 @@ def main():
101128 main()
102129"""
103130
131+
104132# --------------------------------------------------
105- ARGPARSE = """
133+ def body ():
134+ return """
106135import argparse
107136import os
108137import sys
@@ -156,11 +185,13 @@ def main():
156185 args = get_args()
157186 str_arg = args.arg
158187 int_arg = args.int
159- flag_arg = args.flag
188+ file_arg = args.file
189+ flag_arg = args.on
160190 pos_arg = args.positional
161191
162192 print('str_arg = "{}"'.format(str_arg))
163193 print('int_arg = "{}"'.format(int_arg))
194+ print('file_arg = "{}"'.format(file_arg.name))
164195 print('flag_arg = "{}"'.format(flag_arg))
165196 print('positional = "{}"'.format(pos_arg))
166197
@@ -170,6 +201,23 @@ def main():
170201 main()
171202"""
172203
204+ # --------------------------------------------------
205+ def get_defaults ():
206+ """Get defaults from ~/.new.py"""
207+
208+ rc = os .path .join (str (Path .home ()), '.new.py' )
209+ defaults = {}
210+ if os .path .isfile (rc ):
211+ for line in open (rc ):
212+ match = re .match ('([^=]+)=([^=]+)' , line )
213+ if match :
214+ key , val = map (str .strip , match .groups ())
215+ if key and val :
216+ defaults [key ] = val
217+
218+ return defaults
219+
220+
173221# --------------------------------------------------
174222if __name__ == '__main__' :
175223 main ()
0 commit comments