-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_storage.py
153 lines (130 loc) · 5.51 KB
/
mod_storage.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
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
""" mod_storage - Luanti mod_storage management
License: 3-clause BSD (see https://opensource.org/licenses/BSD-3-Clause)
Author: Hubert Tournier
# Database schema:
$ sqlite3 mod_storage.sqlite
sqlite> .schema
CREATE TABLE `entries` (
`modname` TEXT NOT NULL,
`key` BLOB NOT NULL,
`value` BLOB NOT NULL,
PRIMARY KEY (`modname`, `key`)
);
"""
import getopt
import sqlite3
import sys
# Version string used by the what(1) and ident(1) commands:
ID = "@(#) $Id: mod_storage - Luanti mod_storage management v1.0.0 (December 23, 2024) by Hubert Tournier $"
# Default parameters. Can be overcome by environment variables, then command line options
parameters = {
"database": "mod_storage.sqlite",
"modname": "",
"key": "",
"value": ""
}
####################################################################################################
def _display_help():
""" Display usage and help """
#pylint: disable=C0301
print("usage: mod_storage [--help|-?] [--version]", file=sys.stderr)
print(" [-d|--db|--database FILE]", file=sys.stderr)
print(" [-m|--mod|--modname MODNAME]", file=sys.stderr)
print(" [-k|--key KEY]", file=sys.stderr)
print(" [-v|--val|--value VALUE]", file=sys.stderr)
print(" [--] [insert|delete|view]", file=sys.stderr)
print(" ------------------- --------------------------------------------------", file=sys.stderr)
print(" List table content : mod_storage", file=sys.stderr)
print(' Insert an entry : mod_storage -m "modname" -k "key" -v "value" insert', file=sys.stderr)
print(' delete an entry : mod_storage -m "modname" -k "key" delete', file=sys.stderr)
print(' delete all MODNAME entries: mod_storage -m "modname" delete', file=sys.stderr)
print(' delete all KEY entries : mod_storage -k "key" delete', file=sys.stderr)
print(file=sys.stderr)
#pylint: enable=C0301
####################################################################################################
def _process_command_line():
""" Process command line options """
#pylint: disable=C0103, W0602
global parameters
#pylint: enable=C0103, W0602
# option letters followed by : expect an argument
# same for option strings followed by =
character_options = "d:hk:m:v:?"
string_options = [
"help",
"version",
"db=",
"database=",
"key=",
"mod=",
"modname=",
"val=",
"value=",
]
try:
options, remaining_arguments = getopt.getopt(
sys.argv[1:], character_options, string_options
)
except getopt.GetoptError as error:
print(f"Syntax error: {error}", file=sys.stderr)
_display_help()
sys.exit(1)
for option, argument in options:
if option in ("--help", "-?"):
_display_help()
sys.exit(0)
elif option == "--version":
print(ID.replace("@(" + "#)" + " $" + "Id" + ": ", "").replace(" $", ""))
sys.exit(0)
elif option in ("-d", "--db", "--database"):
parameters["database"] = argument
elif option in ("-k", "--key"):
parameters["key"] = argument.encode("utf-8")
elif option in ("-m", "--mod", "--modname"):
parameters["modname"] = argument
elif option in ("-v", "--val", "--value"):
parameters["value"] = argument.encode("utf-8")
return remaining_arguments
####################################################################################################
def main():
""" The program's main entry point """
arguments = _process_command_line()
if len(arguments) > 1:
print("Syntax error: too many arguments", file=sys.stderr)
_display_help()
sys.exit(1)
connection = sqlite3.connect(parameters["database"])
cursor = connection.cursor()
if not arguments or arguments[0] == "view":
print("TABLE 'entries':")
print("================")
for row in cursor.execute("SELECT modname, key, value FROM entries"):
print(row)
elif arguments[0] == "insert":
if parameters["modname"] and parameters["key"] and parameters["value"]:
cursor.execute(f"""INSERT INTO entries VALUES (?, ?, ?)""", (parameters["modname"], parameters["key"], parameters["value"]))
connection.commit()
else:
print("Syntax error: missing arguments", file=sys.stderr)
_display_help()
connection.close()
sys.exit(1)
elif arguments[0] == "delete":
if parameters["modname"] and parameters["key"]:
cursor.execute(f"""DELETE FROM entries WHERE modname=? AND key=?""", (parameters["modname"], parameters["key"]))
connection.commit()
elif parameters["modname"]:
cursor.execute(f"""DELETE FROM entries WHERE modname=?""", (parameters["modname"],))
connection.commit()
elif parameters["key"]:
cursor.execute(f"""DELETE FROM entries WHERE key=?""", (parameters["key"],))
connection.commit()
else:
print("Syntax error: missing arguments", file=sys.stderr)
_display_help()
connection.close()
sys.exit(1)
connection.close()
sys.exit(0)
main()