26
26
"""
27
27
28
28
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
29
- __version__ = '2021110901 '
29
+ __version__ = '2022021501 '
30
30
31
+ import hashlib
31
32
import os
32
33
import re
33
34
import sqlite3
34
35
35
- from . import base3 , disk3
36
+ from . import base3
37
+ from . import disk3
38
+
39
+
40
+ def __sha1sum (string ):
41
+ """Returns a sha1-encoded string.
42
+
43
+ >>> __sha1sum('linuxfabrik')
44
+ '74301e766db4a4006ec1fbd6e031760e7e322223'
45
+ """
46
+ return hashlib .sha1 (string .encode ('utf-8' )).hexdigest ()
36
47
37
48
38
49
def close (conn ):
39
50
"""This closes the database connection. Note that this does not
40
51
automatically call commit(). If you just close your database connection
41
52
without calling commit() first, your changes will be lost.
42
53
"""
43
-
44
54
try :
45
55
conn .close ()
46
56
except :
@@ -51,7 +61,6 @@ def close(conn):
51
61
def commit (conn ):
52
62
"""Save (commit) any changes.
53
63
"""
54
-
55
64
try :
56
65
conn .commit ()
57
66
except Exception as e :
@@ -64,7 +73,6 @@ def connect(path='', filename=''):
64
73
temporary directory is used. If filename is ommitted,
65
74
`linuxfabrik-plugins.db` is used.
66
75
"""
67
-
68
76
def get_filename (path = '' , filename = '' ):
69
77
"""Returns a path including filename to a sqlite database file.
70
78
@@ -104,10 +112,9 @@ def get_filename(path='', filename=''):
104
112
def create_index (conn , column_list , table = 'perfdata' , unique = False ):
105
113
"""Creates one index on a list of/one database column/s.
106
114
"""
107
-
108
115
table = base3 .filter_str (table )
109
116
110
- index_name = 'idx_{}' .format (base3 . sha1sum (table + column_list ))
117
+ index_name = 'idx_{}' .format (__sha1sum (table + column_list ))
111
118
c = conn .cursor ()
112
119
if unique :
113
120
sql = 'CREATE UNIQUE INDEX IF NOT EXISTS {} ON "{}" ({});' .format (
@@ -131,7 +138,6 @@ def create_table(conn, definition, table='perfdata', drop_table_first=False):
131
138
>>> create_table('test', 'a,b,c INTEGER NOT NULL')
132
139
results in 'CREATE TABLE "test" (a TEXT, b TEXT, c INTEGER NOT NULL)'
133
140
"""
134
-
135
141
table = base3 .filter_str (table )
136
142
137
143
# create table if it does not exist
@@ -153,7 +159,6 @@ def create_table(conn, definition, table='perfdata', drop_table_first=False):
153
159
def cut (conn , table = 'perfdata' , max = 5 ):
154
160
"""Keep only the latest "max" records, using the sqlite built-in "rowid".
155
161
"""
156
-
157
162
table = base3 .filter_str (table )
158
163
159
164
c = conn .cursor ()
@@ -175,7 +180,6 @@ def delete(conn, sql, data={}, fetchone=False):
175
180
clause boolean expression is true are deleted. Rows for which the
176
181
expression is false or NULL are retained.
177
182
"""
178
-
179
183
c = conn .cursor ()
180
184
181
185
try :
@@ -194,7 +198,6 @@ def drop_table(conn, table='perfdata'):
194
198
table can not be recovered. All indices and triggers associated with the
195
199
table are also deleted.
196
200
"""
197
-
198
201
table = base3 .filter_str (table )
199
202
200
203
c = conn .cursor ()
@@ -211,7 +214,6 @@ def drop_table(conn, table='perfdata'):
211
214
def insert (conn , data , table = 'perfdata' ):
212
215
"""Insert a row of values (= dict).
213
216
"""
214
-
215
217
table = base3 .filter_str (table )
216
218
217
219
c = conn .cursor ()
@@ -255,7 +257,6 @@ def replace(conn, data, table='perfdata'):
255
257
constraint occurs, the REPLACE statement will abort the action and roll
256
258
back the transaction.
257
259
"""
258
-
259
260
table = base3 .filter_str (table )
260
261
261
262
c = conn .cursor ()
@@ -283,7 +284,6 @@ def select(conn, sql, data={}, fetchone=False, as_dict=True):
283
284
of columns. A SELECT statement does not make any changes to the
284
285
database.
285
286
"""
286
-
287
287
c = conn .cursor ()
288
288
289
289
try :
@@ -309,7 +309,6 @@ def select(conn, sql, data={}, fetchone=False, as_dict=True):
309
309
def get_tables (conn ):
310
310
"""List all tables in a database.
311
311
"""
312
-
313
312
sql = "SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';"
314
313
return select (conn , sql )
315
314
@@ -331,7 +330,6 @@ def compute_load(conn, sensorcol, datacols, count, table='perfdata'):
331
330
{...},
332
331
]
333
332
"""
334
-
335
333
table = base3 .filter_str (table )
336
334
337
335
# count the number of different sensors in the perfdata table
0 commit comments