-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass_processor.py
More file actions
157 lines (140 loc) · 3.93 KB
/
class_processor.py
File metadata and controls
157 lines (140 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
import os
# open as3 class
# taking out the info of its ancestor, methods and fields
# save json
current_classname = ""
def strip_prog_token(nm):
var_names_sms="qwertyuiopasdfghjklzxcvbnm0123456789QWERTYUIOPASDFGHJKLZXCVBNM_";
res=""
for ch in nm:
if ch in var_names_sms:
res+=ch;
else:
break;
return res;
def has_class_method(ln_ar):
res=False
if len(ln_ar)>=3:
if ln_ar[0] in ["private","protected","public"] and ln_ar[1]=="function":
res=True
return res;
def has_class_field(ln_ar):
res=False
if len(ln_ar)>=3:
if ln_ar[0] in ["private","protected","public"] and ln_ar[1]=="var":
res=True
return res;
def run_interface_check(ln_ar):
res=False
if len(ln_ar)>=3:
if ln_ar[0]=="public" and ln_ar[1]=="interface":
res=True
return res;
def has_class_name(ln_ar):
res=False
if len(ln_ar)>=3:
if ln_ar[0]=="public" and ln_ar[1]=="class":
res=True
return res;
def add_vars_and_functions_from_parent(class_parent,list_vars,list_functions,list_parents):
data = None
try:
f = open('temp/jsons/'+class_parent+'.json')
data = json.load(f)
f.close()
except:
print("PARENT JSON NOT FOUND:",class_parent)
# print("data:",data)
if data:
for x in data["fields"]:
if x not in list_vars:
list_vars.append(x)
for x in data["methods"]:
if x not in list_functions:
list_functions.append(x)
if "parents" in data:
for x in data["parents"]:
if x not in list_parents:
list_parents.append(x)
def process_class(clnm, file=None):
global current_classname
current_classname = clnm
if not file:
file = "temp/as3files/"+current_classname+".as"
f = open(file)
lines = f.readlines()
f.close();
split_lns=[]
for ln in lines:
split_lns.append(
ln.replace("("," ( ")
.replace("{"," { ")
.replace(")"," ) ")
.replace("}"," } ")
.replace("["," [ ")
.replace("]"," ] ")
.split()
)
# print(split_lns);
class_parent=""
parents_list=[]
list_vars=[]
list_functions=[]
is_interface=False
# 1. looking for public class and extends
for ln_ar in split_lns:
is_interface = is_interface or run_interface_check(ln_ar)
if has_class_name(ln_ar):
try:
id=ln_ar.index("extends")
class_parent=ln_ar[id+1]
parents_list.append(ln_ar[id+1])
break
except:
pass
# 2. looking for [private/public/protected] var
# 3. looking for [private/public/protected] function
for ln_ar in split_lns:
if has_class_field(ln_ar):
list_vars.append(strip_prog_token(ln_ar[2]))
if has_class_method(ln_ar):
func_name = strip_prog_token(ln_ar[2])
if func_name=="set" or func_name=="get":
func_name = strip_prog_token(ln_ar[3])
list_vars.append(func_name)
else:
if func_name!=current_classname:#class name not included as a method
list_functions.append(func_name)
# 4. looking for ancestor's json and adding its firelds and methods, too
# 5. warning if it is not found
if class_parent!="":
add_vars_and_functions_from_parent(class_parent,list_vars,list_functions, parents_list)
# 6. generating json
ob={"class_name":current_classname, "parent":class_parent, "fields":list_vars, "methods":list_functions, "parents":parents_list}
if is_interface:
ob["is_interface"]=is_interface
with open('temp/jsons/'+current_classname+'.json', "w") as write_file:
json.dump(ob, write_file,indent=4, sort_keys=True, ensure_ascii=False)
write_file.close();
# process_class("World")
# process_class("IdleWorld")
# process_class("FarmWorld")
# process_class("BasicGameObject")
def recursively_work_directory(dir_path):
for fl in os.listdir(dir_path):
# print(fl)
fl2 = os.path.join(dir_path, fl)
if os.path.isfile(fl2):
# print(fl)
# pass
nm = os.path.basename(os.path.normpath(fl))
id = nm.find(".as")
if id!=-1:
print(nm[0:id])
process_class(nm[0:id], fl2)
else:
recursively_work_directory(fl2)
# run several times to get list of methods of parents' parents' parents'
orig_dir = "source_as3"
recursively_work_directory(orig_dir)