-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_app_data.py
executable file
·142 lines (109 loc) · 4.92 KB
/
create_app_data.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
import DeviceData
import logging
import os
import execute_db_queries
import sqlite3
import version
def configure_logging():
if not os.path.exists("app_data"):
os.mkdir("app_data")
logging.basicConfig(
filename='app_data/error_log.txt',
level=logging.ERROR,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
def initialise_database():
database_path = 'app_data/app_records.db'
if not os.path.exists(database_path):
conn, cursor = execute_db_queries.create_db_connection()
execute_db_queries.execute_queries(cursor, parse_sql_file_into_array("create_tables.sql")) # Create database tables
execute_db_queries.execute_queries(cursor, parse_sql_file_into_array("create_triggers.sql")) # Create database triggers
execute_db_queries.execute_queries(cursor, parse_sql_file_into_array("insert_settings.sql")) # Create database triggers
cursor.execute("""INSERT INTO UserSettings(key, value) VALUES ('version', ?)""", (version.__version__,))
add_devices(cursor)
conn.commit()
conn.close()
def parse_sql_file_into_array(sql_file_path):
with open (sql_file_path , "r") as sql_file:
sql_contents = sql_file.read()
queries = sql_contents.split("-- ### QUERY_SEPARATOR ###")
return_array = []
for query in queries:
formatted_query = query.strip()
if formatted_query: # Skip empty queries
return_array.append(formatted_query)
return return_array
def add_devices(cursor):
try:
for device in DeviceData.logitech_devices:
cursor.execute("""
INSERT INTO Devices (
device_id,
device_name,
is_user_device,
config_file_device_name,
device_pids,
min_dpi,
max_dpi,
default_dpi,
has_thumbwheel,
thumbwheel_tap,
thumbwheel_proxy,
thumbwheel_touch,
thumbwheel_timestamp,
smartshift_support,
hires_scroll_support,
number_of_sensors,
has_scrollwheel
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
device.device_id,
device.device_name,
0,
device.config_file_device_name,
str(device.device_pids),
device.min_dpi,
device.max_dpi,
device.default_dpi,
device.has_thumbwheel,
device.thumbwheel_tap_support,
device.thumbwheel_proxy_support,
device.thumbwheel_touch_support,
device.thumbwheel_timestamp_support,
device.smartshift_support,
device.hires_scroll_support,
device.number_of_sensors,
device.has_scrollwheel
))
for button in device.buttons:
cursor.execute("""
INSERT INTO Buttons (
device_id,
button_cid,
button_name,
reprog,
fn_key,
mouse_key,
gesture_support,
accessible
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (
device.device_id,
button.button_cid,
button.button_name,
button.reprogrammable,
button.fn_key,
button.mouse_key,
button.gesture_support,
button.accessible
))
except sqlite3.Error as e:
logging.error(e)
def main():
conn, cursor = execute_db_queries.create_db_connection()
add_devices(cursor)
execute_db_queries.commit_changes_and_close(conn)
if __name__ == "__main__":
main()