-
Notifications
You must be signed in to change notification settings - Fork 0
/
svxlink_conf2sqlite.v3.py
85 lines (69 loc) · 3.34 KB
/
svxlink_conf2sqlite.v3.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
#!/usr/bin/env python3
import sqlite3
import re
config_file_path = 'svxlink.conf.in' # The path to the configuration file
db_path = 'svxlink_conf_v3.db' # The path to the SQLite database
# Regular expressions for parsing
section_regex = re.compile(r'^\[(.+)\]$')
# Adjusted regex to match key-value pairs, including those starting with '#'
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,
including those starting with '#' and followed by an identifier.
"""
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()
# Skip lines that are pure comments and not key-value pairs
if line == '' or (line.startswith('#') and '=' not in line):
continue
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,
including entries that start with '#'.
"""
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Create a table for storing sections
cursor.execute('CREATE TABLE IF NOT EXISTS svxlink_conf_sections (id INTEGER PRIMARY KEY AUTOINCREMENT, section_name TEXT UNIQUE)')
print("Creating SQLite database from configuration data...")
for section, key_values in config.items():
# Insert the section name into svxlink_conf_sections
cursor.execute('INSERT OR IGNORE INTO svxlink_conf_sections (section_name) VALUES (?)', (section,))
# Create or adjust table schema for each section
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
print(f"Inserting key-value pairs into table: {section}")
cursor.executemany(f'''INSERT INTO "{section}" (key, value)
VALUES (?, ?)''', key_values)
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, including entries starting with '#'.")