-
Notifications
You must be signed in to change notification settings - Fork 0
/
svxlink_conf2sqlite.v1.py
79 lines (65 loc) · 3.02 KB
/
svxlink_conf2sqlite.v1.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
#!/usr/bin/env python3
import sqlite3
import re
config_file_path = 'svxlink.conf.in' # The path to the configuration file
db_path = 'svxlink_conf_v1.db' # The path to the SQLite database
# Regular expressions for parsing
section_regex = re.compile(r'^\[(.+)\]$')
key_value_regex = re.compile(r'^([^#=]+)=(.*)$')
def parse_config(file_path):
"""
Parse the configuration file into a dictionary of sections with their key-value pairs.
"""
config = {}
current_section = None
print("Starting to parse the configuration file...")
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if not line or line.startswith('#'):
continue # Skip empty lines and comments
section_match = section_regex.match(line)
if section_match:
current_section = section_match.group(1)
config[current_section] = []
print(f"Found section: {current_section}")
else:
key_value_match = key_value_regex.match(line)
if key_value_match and current_section is not None:
key, value = key_value_match.groups()
config[current_section].append((key.strip(), value.strip()))
print(f"Added key-value pair: {key.strip()} = {value.strip()}")
print("Finished parsing the configuration file.")
return config
def create_db_from_config(config, db_path):
"""
Create an SQLite database from the parsed configuration dictionary.
"""
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
print("Creating SQLite database from configuration data...")
for section, key_values in config.items():
# Adjust table schema to include an auto-increment ID, creation, and update timestamps
cursor.execute(f'''CREATE TABLE IF NOT EXISTS "{section}" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT,
value TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)''')
# Insert key-value pairs into the table, adjusting for the new schema
print(f"Inserting key-value pairs into table: {section}")
for key, value in key_values:
cursor.execute(f'''INSERT INTO "{section}" (key, value)
VALUES (?, ?)''', (key, value))
cursor.execute(f'''UPDATE "{section}"
SET updated_at = CURRENT_TIMESTAMP
WHERE rowid = last_insert_rowid()''')
conn.commit()
conn.close()
print("SQLite database creation completed.")
# Parse the configuration file
config = parse_config(config_file_path)
# Create the SQLite database from the parsed configuration
create_db_from_config(config, db_path)
print("SQLite database has been created with the configuration data.")