-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlm_sqllite.py
129 lines (110 loc) · 3.57 KB
/
tlm_sqllite.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
from tlm_dictonary import CType_FlatNames,GetTlmPackets
import sqlite3
tdict = {int: "INT",
'str': "TEXT",
long:"BIGINT",
float: "FLOAT"}
def CNameToSQLName(name):
name = name.split('.')
name = '.'.join(name[1:])
name = name.replace('[','_')
name = name.replace(']','_')
name = name.replace('.','_')
if name == 'rcv_time':
name = "_rcv_time"
return name
def _BuildTblFromStruct(struct, drop=True):
name = type(struct).__name__
txt = ""
if drop:
txt += "DROP TABLE IF EXISTS {name};\n".format(name=name)
txt = "CREATE TABLE IF NOT EXISTS {name} (\n".format(name=name)
rows = [ " rcv_time LONG NOT NULL"]
num_cols = 1
for x in CType_FlatNames(struct):
cname = x[0]
cname = CNameToSQLName(cname)
ttype = x[1]
ctype = tdict[ttype]
rowtxt = " {cname} {data_type} ".format(cname=cname, data_type=ctype)
if num_cols < 2000:
rows.append(rowtxt)
num_cols += 1
rowtxt = ',\n'.join(rows)
txt += rowtxt
txt += "\n);"
if num_cols > 2000:
print name, num_cols
print "Can't have more than 2000 columns, dumping top ones"
return txt
def _BuildTblInsertFromStruct(struct):
name = type(struct).__name__
txt = 'def SQLiteInsert_{name}({name},rcv_time,cur):\n'.format(name=name)
txt += ' query = """Insert into {name}'.format(name=name)
cnames = ["rcv_time"]
qs = ["?"]
names = ["rcv_time"]
num_cols = 1
for x in CType_FlatNames(struct):
if num_cols < 999:
names.append(x[0])
cname = x[0]
cname = CNameToSQLName(cname)
cnames.append(cname)
qs.append('?')
num_cols += 1
txt += '\n(\n ' + ',\n '.join(cnames) + '\n)\n'
txt += '\nvalues(\n ' + ',\n '.join(qs)
txt += '\n);"""\n'
txt += ' print ' + str(len(qs) ) + ' , ' + str( len(cnames) ) + '\n'
txt += ' return cur.execute(query, (' + ',\n '.join(names) +') )\n\n'
return txt
def _BuildDatabaseTxtFromModule(module):
packets = GetTlmPackets(module)
txt = ""
for pkt in packets:
txt += _BuildTblFromStruct(pkt[1])
txt += "\n\n\n"
# fname = 'Log_' + pkt[0]
# funcs[ pkt[0]] = ns[fname]
return txt
def GetOpenMCTTlmLoggers(module):
packets = GetTlmPackets(module)
pkt = packets[0]
logger = """import sqlite3
"""
for pkt in packets:
logger += _BuildTblInsertFromStruct(pkt[1])
#Now we have a bunch of txt of our functions we want
code = compile(logger,'SQLLoggerModule','exec')
#Compile our logging options into namespace
ns = {}
exec(code) in ns
#Just grab our functions
funcs = {}
for pkt in packets:
fname = 'SQLiteInsert_' + pkt[0]
funcs[ pkt[0]] = ns[fname]
return funcs
def CreateDatabase(name,module):
conn = sqlite3.connect(name,check_same_thread=False)
c = conn.cursor()
query = _BuildDatabaseTxtFromModule(module)
c.executescript(query)
conn.commit()
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
conn.row_factory = dict_factory
return conn
if __name__ == "__main__":
#CType_FlatNames
import proj_example.rdl.messages as msg
tlms = type('tlms',(),{})
tlms.Example_M = msg.Example_M()
tlms.SPS_M = msg.SPS_M()
print _BuildTblInsertFromStruct(tlms.Example_M)
print GetOpenMCTTlmLoggers(tlms)
CreateDatabase('test.db', tlms)