Skip to content

Commit 3645986

Browse files
committed
base3: Move sha1sum() to db_sqlite3.py (fixed #50)
1 parent 1004e0d commit 3645986

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

base3.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
"""
1616

1717
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
18-
__version__ = '2022021502'
18+
__version__ = '2022021503'
1919

2020
import collections
2121
import datetime
22-
import hashlib
2322
import numbers
2423
import operator
2524
import os
@@ -576,10 +575,6 @@ def parse_atom(atom, default):
576575
return (True, True ^ invert)
577576

578577

579-
def sha1sum(string):
580-
return hashlib.sha1(string.encode('utf-8')).hexdigest()
581-
582-
583578
def mltext2array(input, skip_header=False, sort_key=-1):
584579
input = input.strip(' \t\n\r').split('\n')
585580
lines = []

db_sqlite3.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,31 @@
2626
"""
2727

2828
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
29-
__version__ = '2021110901'
29+
__version__ = '2022021501'
3030

31+
import hashlib
3132
import os
3233
import re
3334
import sqlite3
3435

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()
3647

3748

3849
def close(conn):
3950
"""This closes the database connection. Note that this does not
4051
automatically call commit(). If you just close your database connection
4152
without calling commit() first, your changes will be lost.
4253
"""
43-
4454
try:
4555
conn.close()
4656
except:
@@ -51,7 +61,6 @@ def close(conn):
5161
def commit(conn):
5262
"""Save (commit) any changes.
5363
"""
54-
5564
try:
5665
conn.commit()
5766
except Exception as e:
@@ -64,7 +73,6 @@ def connect(path='', filename=''):
6473
temporary directory is used. If filename is ommitted,
6574
`linuxfabrik-plugins.db` is used.
6675
"""
67-
6876
def get_filename(path='', filename=''):
6977
"""Returns a path including filename to a sqlite database file.
7078
@@ -104,10 +112,9 @@ def get_filename(path='', filename=''):
104112
def create_index(conn, column_list, table='perfdata', unique=False):
105113
"""Creates one index on a list of/one database column/s.
106114
"""
107-
108115
table = base3.filter_str(table)
109116

110-
index_name = 'idx_{}'.format(base3.sha1sum(table + column_list))
117+
index_name = 'idx_{}'.format(__sha1sum(table + column_list))
111118
c = conn.cursor()
112119
if unique:
113120
sql = 'CREATE UNIQUE INDEX IF NOT EXISTS {} ON "{}" ({});'.format(
@@ -131,7 +138,6 @@ def create_table(conn, definition, table='perfdata', drop_table_first=False):
131138
>>> create_table('test', 'a,b,c INTEGER NOT NULL')
132139
results in 'CREATE TABLE "test" (a TEXT, b TEXT, c INTEGER NOT NULL)'
133140
"""
134-
135141
table = base3.filter_str(table)
136142

137143
# create table if it does not exist
@@ -153,7 +159,6 @@ def create_table(conn, definition, table='perfdata', drop_table_first=False):
153159
def cut(conn, table='perfdata', max=5):
154160
"""Keep only the latest "max" records, using the sqlite built-in "rowid".
155161
"""
156-
157162
table = base3.filter_str(table)
158163

159164
c = conn.cursor()
@@ -175,7 +180,6 @@ def delete(conn, sql, data={}, fetchone=False):
175180
clause boolean expression is true are deleted. Rows for which the
176181
expression is false or NULL are retained.
177182
"""
178-
179183
c = conn.cursor()
180184

181185
try:
@@ -194,7 +198,6 @@ def drop_table(conn, table='perfdata'):
194198
table can not be recovered. All indices and triggers associated with the
195199
table are also deleted.
196200
"""
197-
198201
table = base3.filter_str(table)
199202

200203
c = conn.cursor()
@@ -211,7 +214,6 @@ def drop_table(conn, table='perfdata'):
211214
def insert(conn, data, table='perfdata'):
212215
"""Insert a row of values (= dict).
213216
"""
214-
215217
table = base3.filter_str(table)
216218

217219
c = conn.cursor()
@@ -255,7 +257,6 @@ def replace(conn, data, table='perfdata'):
255257
constraint occurs, the REPLACE statement will abort the action and roll
256258
back the transaction.
257259
"""
258-
259260
table = base3.filter_str(table)
260261

261262
c = conn.cursor()
@@ -283,7 +284,6 @@ def select(conn, sql, data={}, fetchone=False, as_dict=True):
283284
of columns. A SELECT statement does not make any changes to the
284285
database.
285286
"""
286-
287287
c = conn.cursor()
288288

289289
try:
@@ -309,7 +309,6 @@ def select(conn, sql, data={}, fetchone=False, as_dict=True):
309309
def get_tables(conn):
310310
"""List all tables in a database.
311311
"""
312-
313312
sql = "SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';"
314313
return select(conn, sql)
315314

@@ -331,7 +330,6 @@ def compute_load(conn, sensorcol, datacols, count, table='perfdata'):
331330
{...},
332331
]
333332
"""
334-
335333
table = base3.filter_str(table)
336334

337335
# count the number of different sensors in the perfdata table

0 commit comments

Comments
 (0)