diff options
Diffstat (limited to 'scripts/common')
-rw-r--r-- | scripts/common/__init__.py | 20 | ||||
-rw-r--r-- | scripts/common/postgres.py | 21 | ||||
-rw-r--r-- | scripts/common/queue.py | 23 |
3 files changed, 32 insertions, 32 deletions
diff --git a/scripts/common/__init__.py b/scripts/common/__init__.py index 83f1b66..e349610 100644 --- a/scripts/common/__init__.py +++ b/scripts/common/__init__.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 -''' common functions and stuff ''' +""" common functions and stuff """ import logging import os import sys from datetime import datetime -QUEUE = bool(os.environ.get('el_QUEUE', False)) +QUEUE = bool(os.environ.get("el_QUEUE", False)) name = os.path.splitext(os.path.split(sys.argv[0])[-1])[0] @@ -17,17 +17,17 @@ logging.basicConfig(format="%(name)s: %(levelname)s %(message)s") # Use queue or postgres if QUEUE is True: - log.debug('Importing dbi from queue') + log.debug("Importing dbi from queue") from .queue import dbi else: - log.debug('Importing dbi from postgres') + log.debug("Importing dbi from postgres") from .postgres import dbi # Insert state def statein(sensor, value, device_class, unit, **kwargs): - ''' Insert new state in db ''' - verbose = bool(kwargs['verbose']) if 'verbose' in kwargs else False + """Insert new state in db""" + verbose = bool(kwargs["verbose"]) if "verbose" in kwargs else False sql = """INSERT INTO states (sensor_id, @@ -38,10 +38,6 @@ def statein(sensor, value, device_class, unit, **kwargs): SELECT sensors.id, %s, %s, %s, %s FROM sensors WHERE sensors.name = %s;""" - values = (value, - device_class, - unit, - datetime.utcnow(), - sensor) - log.debug('dbi(' + sql + ',' + str(values) + ')') + values = (value, device_class, unit, datetime.utcnow(), sensor) + log.debug("dbi(" + sql + "," + str(values) + ")") dbi(sql, values, verbose=verbose) diff --git a/scripts/common/postgres.py b/scripts/common/postgres.py index efa4b5a..5f6ea31 100644 --- a/scripts/common/postgres.py +++ b/scripts/common/postgres.py @@ -1,28 +1,31 @@ #!/usr/bin/env python3 -''' common functions and stuff ''' +""" common functions and stuff """ import os import sys import psycopg -pg_db = os.environ['el_pg_db'] -pg_host = os.environ['el_pg_host'] -pg_user = os.environ.get('el_pg_user','') -pg_pass = os.environ.get('el_pg_pass','') +pg_db = os.environ["el_pg_db"] +pg_host = os.environ["el_pg_host"] +pg_user = os.environ.get("el_pg_user", "") +pg_pass = os.environ.get("el_pg_pass", "") + def dbi(sql, values, **kwargs): - ''' insert into db ''' - verbose = bool(kwargs['verbose']) if 'verbose' in kwargs else False + """insert into db""" + verbose = bool(kwargs["verbose"]) if "verbose" in kwargs else False # pylint: disable=E1129 - with psycopg.connect(dbname=pg_db, host=pg_host, user=pg_user, password=pg_pass) as conn: + with psycopg.connect( + dbname=pg_db, host=pg_host, user=pg_user, password=pg_pass + ) as conn: cur = conn.cursor() if isinstance(values, list): cur.executemany(sql, values) elif isinstance(values, tuple): cur.execute(sql, values) else: - print('`values` is a', type(values), 'but it needs to be tuple or list') + print("`values` is a", type(values), "but it needs to be tuple or list") sys.exit(1) if verbose is True: print("Inserted and/or changed", cur.rowcount, "rows in db") diff --git a/scripts/common/queue.py b/scripts/common/queue.py index 4510d55..de49802 100644 --- a/scripts/common/queue.py +++ b/scripts/common/queue.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -''' common functions and stuff ''' +""" common functions and stuff """ import logging import os @@ -7,26 +7,27 @@ import pickle from litequeue import SQLQueue -QUEUE_DB = os.environ.get('el_QUEUE_db', 'litequeue.db') -QUEUE_DIR = os.environ.get('el_QUEUE_dir', 'queue') +QUEUE_DB = os.environ.get("el_QUEUE_db", "litequeue.db") +QUEUE_DIR = os.environ.get("el_QUEUE_dir", "queue") -QUEUE_DB = QUEUE_DIR + "/" + QUEUE_DB +QUEUE_DB = QUEUE_DIR + "/" + QUEUE_DB log = logging.getLogger(__name__) # Initialize queue q = SQLQueue(QUEUE_DB, maxsize=None) -def dbi(sql,values,**kwargs): - ''' insert into queue ''' - log.debug('function dbi()') - verbose = bool(kwargs['verbose']) if 'verbose' in kwargs else False - log.debug('Inserting into sqlite-queue') +def dbi(sql, values, **kwargs): + """insert into queue""" + log.debug("function dbi()") + verbose = bool(kwargs["verbose"]) if "verbose" in kwargs else False + + log.debug("Inserting into sqlite-queue") q.put(pickle.dumps([sql, values])) if verbose is True: - log.debug('verbose = True') - table = sql.split(' ')[2] + log.debug("verbose = True") + table = sql.split(" ")[2] num = 1 if isinstance(values, tuple) else len(values) print("Inserted " + str(num) + " item(s) into queue for " + str(table)) return True |