-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.py
More file actions
200 lines (172 loc) · 6.19 KB
/
repository.py
File metadata and controls
200 lines (172 loc) · 6.19 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
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
#coding=utf8
import sqlite3, os
import cPickle
from sc.struct_proxy import *
class Repository(object):
def __init__(self, db_path):
self.db_path = db_path
if not os.path.exists(self.db_path):
self.__conn = sqlite3.connect(self.db_path)
self.__deploy()
else:
self.__conn = sqlite3.connect(self.db_path)
self.__conn.text_factory = str
def __del__(self):
self.__conn.close()
def __unpack_prog_data(self, data):
return ProgramProxy(
_id = data[0],
name = data[1],
source_code = data[2],
count_of_control_blocks = data[3],
num_of_headers = cPickle.loads(str(data[4])),
num_of_control_types = cPickle.loads(str(data[5])),
num_of_global_variables = cPickle.loads(str(data[6])),
num_of_local_variables = cPickle.loads(str(data[7])),
)
def __deploy(self):
c = self.__conn.cursor()
# programs
c.execute('''CREATE TABLE program (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
source_code TEXT,
added_date INTEGER,
count_of_control_blocks INTEGER,
num_of_headers TEXT,
num_of_control_types TEXT,
num_of_global_variables TEXT,
num_of_local_variables TEXT
)''')
self.__conn.commit()
# functions
c.execute("CREATE TABLE function (\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
program_id INTEGER,\
name TEXT,\
ret_type TEXT,\
num_of_local_variables TEXT,\
num_of_arguments_variables TEXT,\
seq_of_control_types TEXT,\
num_of_control_types TEXT,\
FOREIGN KEY(program_id) REFERENCES program(id)\
)")
self.__conn.commit()
def clear(self):
if os.path.exists(self.db_path):
print self.db_path
self.__conn.close()
os.remove(self.db_path)
self.__conn = sqlite3.connect(self.db_path)
self.__deploy()
def info(self):
c = self.__conn.cursor()
prog_count = c.execute('''SELECT COUNT(id) FROM program''').fetchone()[0]
func_count =c.execute('''SELECT COUNT(id) FROM function''').fetchone()[0]
return u'В базе данных сохранено:\r\n\t - программ %d\r\n\t - функций %d' %(prog_count, func_count)
def insert_program(self, program):
c = self.__conn.cursor()
c.execute('''INSERT INTO program (
name,
source_code,
count_of_control_blocks,
num_of_headers,
num_of_control_types,
num_of_global_variables,
num_of_local_variables
) VALUES (?,?,?,?,?,?,?)''', (
program.name,
program.source_code,
program.count_of_control_blocks(),
cPickle.dumps(program.num_of_headers()),
cPickle.dumps(program.num_of_control_types()),
cPickle.dumps(program.num_of_global_variables()),
cPickle.dumps(program.num_of_local_variables())
))
functions = []
for function in program.functions:
functions.append((
c.lastrowid,
function.name,
cPickle.dumps(function.ret_type),
cPickle.dumps(function.num_of_local_variables()),
cPickle.dumps(function.num_of_arguments_variables()),
cPickle.dumps(function.seq_of_control_types()),
cPickle.dumps(function.num_of_control_types()),
))
c.executemany('''INSERT INTO function (
program_id,
name,
ret_type,
num_of_local_variables,
num_of_arguments_variables,
seq_of_control_types,
num_of_control_types
) VALUES (?,?,?,?,?,?,?)''', functions)
self.__conn.commit()
def get_program(self, prog_id):
c = self.__conn.cursor()
c.execute('''SELECT
id,
name,
source_code,
count_of_control_blocks,
num_of_headers,
num_of_control_types,
num_of_global_variables,
num_of_local_variables FROM program WHERE id = ?''', (prog_id,)
)
return self.__unpack_prog_data(c.fetchone())
def get_programs(self):
c = self.__conn.cursor()
c.execute('''SELECT
id,
name,
source_code,
count_of_control_blocks,
num_of_headers,
num_of_control_types,
num_of_global_variables,
num_of_local_variables FROM program'''
)
programs = []
for data in c.fetchall():
programs.append(self.__unpack_prog_data(data))
return programs
def get_functions(self, prog_id = None):
c = self.__conn.cursor()
if (prog_id):
c.execute('''SELECT
id,
program_id,
name,
ret_type,
num_of_local_variables,
num_of_arguments_variables,
seq_of_control_types,
num_of_control_types FROM function WHERE program_id = ?''', (prog_id,)
)
else:
c.execute('''SELECT
id,
program_id,
name,
ret_type,
num_of_local_variables,
num_of_arguments_variables,
seq_of_control_types,
num_of_control_types FROM function'''
)
functions = []
for data in c.fetchall():
functions.append(FunctionProxy(
_id = data[0],
prog_id = data[1],
name = data[2],
ret_type = cPickle.loads(str(data[3])),
num_of_local_variables = cPickle.loads(str(data[4])),
num_of_arguments_variables = cPickle.loads(str(data[5])),
seq_of_control_types = cPickle.loads(str(data[6])),
num_of_control_types = cPickle.loads(str(data[7])),
))
return functions