-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
60 lines (49 loc) · 1.78 KB
/
decode.py
File metadata and controls
60 lines (49 loc) · 1.78 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
from datetime import datetime
import re
import sqlite3
from json import loads
LOG_SOURCE = "robot.log"
DESTINATION_DB = "Motion.db"
with open(LOG_SOURCE, 'r', encoding='utf-8') as f:
data = f.read()
columns = {"$timestamp"}
arry_names = ['x', 'y', 'z', 'w']
packs = []
for match in re.finditer(r'##([0-9.]*?)##(.*?)##$', data, re.MULTILINE):
timestamp = int(float(match.group(1)) * 1000)
json_bundle = loads(match.group(2))
json_bundle["$timestamp"] = timestamp
output_bundle = {}
for k, v in json_bundle.items():
safecolname = re.sub(r'["\']', '_', k)
if type(v) == list:
for i in range(0, len(v)):
index_name = f'{safecolname}_{arry_names[i] if i < len(arry_names) else str(i+1)}'
columns.add(index_name)
output_bundle[index_name] = v[i]
elif type(v) == int or type(v) == float or type(v) == str or type(v) == bool:
columns.add(safecolname)
output_bundle[safecolname] = v
else:
raise ValueError(f"cannot generate columns for type {type(v)}")
packs.append(output_bundle)
tablename = f'Logs_{datetime.now().strftime("%d-%m-%Y_%H:%M:%S")}'
# tablename=input('table name? ')
sorted_cols = sorted(columns)
coldecl = ', '.join(map(lambda x: f"\"{x}\"", sorted_cols))
conn = sqlite3.connect(DESTINATION_DB)
cursor = conn.cursor()
cursor.execute(f"CREATE TABLE \"{tablename}\"({coldecl})")
sql_cmd = f"INSERT INTO \"{tablename}\" VALUES({', '.join(['?'] * len(sorted_cols))})"
print(sql_cmd)
ordered_packages = []
for package in packs:
splat = []
for colname in sorted_cols:
if colname in package:
splat.append(package[colname])
else:
splat.append(None)
cursor.execute(sql_cmd, splat)
conn.commit()
conn.close()