-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
232 lines (215 loc) · 6.9 KB
/
script.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import sqlite3, re, unittest, sys
class ScriptReader():
"""Scans scripts for rules and keywords, which are then stored in a db"""
def __init__(self, script):
self.conn = sqlite3.connect('testdb')
self.name = script
self.create_script(self.name)
self.compile_re()
self.scan_file(self.name)
def create_script(self, name):
if not self.already_exists('SCRIPT', name):
self.conn.execute('INSERT INTO SCRIPT VALUES("' + self.name + '")')
self.conn.commit()
def scan_file(self, script):
with open(script) as f:
start = False
for line in f:
line = line.strip()
if self.get_opening(line) and start == False:
start = True
else:
if self.get_keyword(line):
pass
elif self.get_decomposition_rule(line):
pass
elif self.get_reassembly_rule(line):
pass
elif self.get_substitution_rule(line):
pass
elif self.get_equivalence_class(line):
pass
elif self.get_empty(line):
pass
self.conn.close()
print('Finished scanning.')
sys.exit()
def get_opening(self, line):
try:
temp = re.search(self.st, line).group
except AttributeError as e:
temp = None
if temp:
#res = self.clean_group(temp)
if not self.already_exists('OPENING', temp(1)):
try:
self.conn.execute('INSERT INTO OPENING VALUES("' + temp(1) +'", "' + self.name + '")')
self.conn.commit()
return True
except IndexError as e:
pass
def get_keyword(self, line):
try:
temp = re.search(self.kw, line).group
except AttributeError as e:
temp = None
if temp:
res = self.clean_group(temp)
if self.get_memory(res[0]):
pass
if not self.already_exists('KEYWORD', res[0].lower()):
try:
self.current_keyword = res[0].lower()
rank = res[1]
except IndexError as e:
rank = 0
try:
self.current_eq = res[2].lower()
except IndexError as e:
self.current_eq = self.current_keyword
self.conn.execute('INSERT INTO EQ VALUES("' + self.current_eq + '", "' + self.name + '")')
self.conn.execute('INSERT INTO KEYWORD VALUES("' + self.current_keyword + '", ' + str(rank) + \
', "' + self.current_eq + '", "' + self.name + '")')
self.conn.commit()
return True
def get_memory(self, line):
try:
temp = re.search(self.me, line).group
except AttributeError as e:
temp = None
if temp:
if not self.already_exists('MEMORY', temp(1).lower()):
try:
self.conn.execute('INSERT INTO MEMORY VALUES("' + temp(1).lower() + '", "' + self.name + '")')
self.conn.commit()
return True
except IndexError as e:
pass
def get_decomposition_rule(self, line):
copy = False
if 'Copy:' in line:
line = re.sub('Copy:', '', line).strip()
copy = True
try:
temp = re.search(self.dr, line).group
except AttributeError as e:
temp = None
if temp:
if not self.already_exists('DECOMP_RULE', temp(1).lower()):
if copy:
d_rule = temp(1).lower()
rules = self.conn.execute('SELECT * FROM REASSEM_RULE WHERE D_RULE=="' + self.current_d_rule \
+ '"')
for i in rules:
self.conn.execute('INSERT INTO REASSEM_RULE VALUES("' + i[0] + '", "' + i[1] + '", "' \
+ d_rule + '", 0, "' + i[4] + '")')
self.conn.commit()
self.current_d_rule = temp(1).lower()
self.conn.execute('INSERT INTO DECOMP_RULE VALUES("' + self.current_d_rule + '", "' + \
self.current_eq + '", "' + self.name + '")')
self.conn.commit()
return True
def get_reassembly_rule(self, line):
try:
temp = re.search(self.ar, line).group
except AttributeError as e:
temp = None
if temp:
if not self.already_exists('REASSEM_RULE', temp(1).lower()):
try:
rule_type = temp(2)
except IndexError as e:
rule_type = 'None'
self.conn.execute('INSERT INTO REASSEM_RULE VALUES("' + temp(1).lower() + '", "' + rule_type + \
'", "' + self.current_d_rule + '", 0, "' + self.name + '")')
self.conn.commit()
return True
def get_substitution_rule(self, line):
try:
temp = re.search(self.sr, line).group
except AttributeError as e:
temp = None
if temp:
res = self.clean_group(temp)
if not self.already_exists('SUBSTITUTION', res[0].lower()):
try:
self.conn.execute('INSERT INTO SUBSTITUTION VALUES("' + res[0].lower() + '", "' + \
res[1].lower() + '", "' + self.name + '")')
self.conn.commit()
return True
except IndexError as e:
pass
def get_equivalence_class(self, line):
try:
temp = re.search(self.er, line).group
except AttributeError as e:
temp = None
if temp:
if not self.already_exists('EQ', temp(1)):
self.conn.execute('INSERT INTO EQ VALUES("' + temp(1) + '", "' + self.name + '")')
self.conn.commit()
return True
def get_empty(self, line):
try:
temp = re.search(self.em, line).group
except AttributeError as e:
temp = None
if temp:
if not self.already_exists('EMPTY', temp(1).lower()):
try:
self.conn.execute('INSERT INTO EMPTY VALUES("' + temp(1).lower() + '", 0, "' + self.name + '")')
self.conn.commit()
return True
except IndexError as e:
pass
def compile_re(self):
self.st = re.compile(r'START: (.*)')
self.kw = re.compile(r'Keyword: (.*)')
self.dr = re.compile(r'D-Rule: (.*)')
self.ar = re.compile(r'R-Rule: (.*)')
self.sr = re.compile(r'Sub: (.*)')
self.er = re.compile(r'Eq: (.*)')
self.em = re.compile(r'None: (.*)')
#Helper functions
def clean_group(self, group):
temp = re.sub('\(', '', group(1))
temp = re.sub('\)', '', temp)
temp = re.sub(' ', '', temp)
res = re.split(',', temp)
return res
def already_exists(self, table, item):
exists = False
print(table)
try:
a = self.conn.execute('SELECT * FROM ' + table + ' WHERE __NAME=="' + item + '" AND __SCRIPT=="' \
+ self.name + '"')
except sqlite3.OperationalError as e:
a = self.conn.execute('SELECT * FROM ' + table + ' WHERE __NAME=="' + item + '"')
if len(a.fetchall()) != 0:
exists = True
'''
for i in a:
if item in i:
exists = True'''
return exists
class setup():
def __init__(self):
self.conn = sqlite3.connect('testdb')
self.conn.execute('CREATE TABLE SCRIPT(__NAME TEXT)')
self.conn.execute('CREATE TABLE OPENING(__NAME TEXT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE SUBSTITUTION(__NAME TEXT, SUBSTITUTION TEXT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE EQ(__NAME TEXT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE KEYWORD(__NAME TEXT, RANK INT, EQ TEXT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE DECOMP_RULE(__NAME TEXT, EQ TEXT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE REASSEM_RULE(__NAME TEXT, TYPE TEXT, D_RULE TEXT, RANK INT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE MEMORY(__NAME TEXT, __SCRIPT TEXT)')
self.conn.execute('CREATE TABLE EMPTY(__NAME TEXT, RANK INT, __SCRIPT TEXT)')
if __name__ == '__main__':
try:
setup()
except sqlite3.OperationalError as e:
pass
if len(sys.argv) > 1:
ScriptReader(sys.argv[1])
else:
print('Please supply a script.')