44def main ():
55 # TODO add logic for OnGameEvent so that the end result is "OnGameEvent_${1:name}(${2:table params})$0"
66 # Change data management to dicts and json
7- with open ("defs.txt" , "r" , encoding = "utf-8" ) as defs_file :
8- defs_data = defs_file .readlines ()
7+ try :
8+ with open ("defs.txt" , "r" , encoding = "utf-8" ) as defs_file :
9+ defs_data = defs_file .readlines ()
10+ except IOError :
11+ print ("Unable to locate defs file" )
12+ quit ()
913
14+ functions = parse_defs (defs_data )
15+ sorted_keys = sorted (list (functions .keys ()))# Good for comparing diffs
16+ function_registry = generate_snippet_format (sorted_keys , functions )
17+
18+ with open ("squirrel.json" , "w" ) as json_test :
19+ dump (function_registry , json_test , indent = 2 )
20+
21+ def parse_defs (defs_data : str ) -> dict :
22+ '''
23+ ### Summary
24+ Takes raw data from file and sorts it based on what line the entry was, in units of 4 lines.
25+ ### Parameters
26+ 1. defs_data : str
27+ - The raw data from the `defs.txt` file.
28+ ### Returns
29+ 1. functions : dict
30+ - All of the raw data parsed into a primitive dict that will be later reformated to the snippet formatting.
31+ '''
1032 functions = {}
11- i = 0
33+ quad_defs_entry = 0
1234 most_recent_function = ""
13-
1435 for line in defs_data :
15- text = line [0 :- 1 ]
36+ text = line [0 :- 1 ] # necessary because ".readlines() injects newlines"
1637
17- match ["function" , "signature" , "description" , "space" ][i % 4 ]:
38+ match ["function" , "signature" , "description" , "space" ][quad_defs_entry % 4 ]:
1839 case "function" :
1940 most_recent_function = text
2041 functions [most_recent_function ] = {
@@ -34,11 +55,23 @@ def main():
3455 most_recent_function
3556 ]["description" ].replace ('"' , '\\ "' )
3657
37- i += 1
58+ quad_defs_entry += 1
59+ return functions
3860
39- sorted_keys = sorted (list (functions .keys ()))
61+ def generate_snippet_format (sorted_keys : list [str ], functions : dict ) -> dict :
62+ '''
63+ ### Summary
64+ Takes the sorted keys of the functions dict and formats where unset variables exist in the function body.
65+ ### Parameters
66+ 1. sorted_keys : list[str]
67+ - A sorted list of the names of all of the functions.
68+ 2. functions : dict
69+ - Primitive dictionary of all the functions that need to be formatted to include their unset variable syntax as added to the registry.
70+ ### Returns
71+ 1. function_registry : dict
72+ - Formatted dict of all functions as VSCode snippets with unset variable syntax included.
73+ '''
4074 function_registry = {}
41-
4275 for key in sorted_keys :
4376 prefix , body = "" , ""
4477 base_signature = functions [key ]["signature" ]
@@ -48,13 +81,13 @@ def main():
4881 # We need to parse params
4982 params = base_signature [paren_index + 1 : - 1 ].split (", " )
5083 body = prefix + "("
51- i = 1
84+ quad_defs_entry = 1
5285 if params [0 ] != "" :
5386 for param in params :
54- body += "${" + str (i ) + ":" + param + "}"
55- if i != len (params ):
87+ body += "${" + str (quad_defs_entry ) + ":" + param + "}"
88+ if quad_defs_entry != len (params ):
5689 body += ", "
57- i += 1
90+ quad_defs_entry += 1
5891 body += ")$0"
5992 else :
6093 # This is a constant
@@ -70,9 +103,7 @@ def main():
70103 }
71104 }
72105 function_registry .update (function_entry )
73-
74- with open ("squirrel_.json" , "w" ) as json_test :
75- dump (function_registry , json_test , indent = 2 )
106+ return function_registry
76107
77108if __name__ == "__main__" :
78109 main ()
0 commit comments