1- # See main.py for overview.
1+ #! /usr/bin/env python3
22
33import json
44import re
5- import sys
5+ from argparse import ArgumentParser
66
7- app = sys . argv [ 1 ]
7+ def parse_function_list ( sheet_app : str , write_json_file : bool = True ):
88
9- func_file = f'{ app } _funcs_data_list.txt'
9+ with open (f'{ sheet_app } _funcs_data_list.txt' , 'r' ) as file :
10+ lines = [line .strip () for line in file ]
1011
11- with open ( func_file , 'r' ) as file :
12- func_list = [line . strip () for line in file ]
12+ # Debug
13+ # lines = ['CALL~register_id,[argument1],...' ]
1314
14- # Debug
15- # func_list = ['CALL~register_id,[argument1],...']
15+ functions = []
1616
17- dict_list = []
17+ for line in lines :
18+ func_name , param_str = line .split ('~' )
19+ param_list = param_str .split (',' )
20+ req_param = []
21+ opt_param = []
1822
19- for line in func_list :
20- func_name = line .split ('~' )[0 ]
23+ ellipsis = False
2124
22- param_str = line .split ('~' )[1 ]
25+ for param in param_list :
26+ if param [0 ] == '[' :
27+ opt_param .append (re .sub (r'[\[\]]' , '' , param ))
2328
24- param_list = param_str .split (',' )
29+ elif param [0 ] == '.' :
30+ ellipsis = True
2531
26- req_param = []
32+ else :
33+ req_param .append (param )
2734
28- opt_param = []
35+ func_dict = dict (
36+ func_name = func_name ,
37+ req_param = req_param ,
38+ opt_param = opt_param ,
39+ ellipsis = ellipsis ,
40+ )
2941
30- ellipsis = False
42+ functions . append ( func_dict )
3143
32- for param in param_list :
33- if param [ 0 ] == '[' :
34- opt_param . append ( re . sub ( r'[\[\]]' , '' , param ) )
44+ if write_json_file :
45+ with open ( f' { sheet_app } _funcs.json' , 'w' ) as o :
46+ json . dump ( functions , o , indent = 4 )
3547
36- elif param [0 ] == '.' :
37- ellipsis = True
48+ return functions
3849
39- else :
40- req_param .append (param )
4150
42- func_dict = dict (func_name = func_name , req_param = req_param , opt_param = opt_param , ellipsis = ellipsis )
51+ if __name__ == '__main__' :
52+ parser = ArgumentParser (
53+ description = 'Parse ~-delimited spreadsheet function list from a file' )
54+ parser .add_argument (
55+ '-s' , '--spreadsheet-app' ,
56+ choices = {'excel' , 'gsheets' , 'localc' },
57+ default = 'excel' ,
58+ help = 'spreadsheet application' )
59+ parser .add_argument (
60+ '-j' , '--dump-json' ,
61+ type = bool ,
62+ default = True ,
63+ help = 'dump JSON output to a file' )
64+ args = parser .parse_args ()
4365
44- dict_list .append (func_dict )
45-
46- json_out = json .dumps (dict_list , indent = 4 )
47-
48- o = open (f'{ app } _funcs.json' , 'w' )
49-
50- o .write (json_out )
51- o .close ()
66+ parse_function_list (args .spreadsheet_app )
0 commit comments