fix: logger and match-case statements

This commit is contained in:
Lunaresk 2021-12-17 17:25:46 +01:00
parent f137400399
commit b711644f55
4 changed files with 47 additions and 36 deletions

View File

@ -46,5 +46,5 @@ def send_scan(user: str, scanned: dict[int: int], date:str = None):
PORT)]) + '/scan2kasse/insert', json=infos, timeout=1) PORT)]) + '/scan2kasse/insert', json=infos, timeout=1)
return True if response.status_code == 201 else response.json() return True if response.status_code == 201 else response.json()
except Exception as e: except Exception as e:
LOGGER.exception() LOGGER.exception("")
return infos return infos

21
client/src/constants.py Normal file
View File

@ -0,0 +1,21 @@
from yaml import safe_load
class Barcode_CodeID:
with open("config.yaml", 'r') as file:
data = safe_load(file)['options']
CODEID_POS = data['barcode']['codeid']['position'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'position' in data['barcode']['codeid'] else None
CODE128 = data['barcode']['codeid']['Code128'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'Code128' in data['barcode']['codeid'] else "A"
EAN8 = data['barcode']['codeid']['EAN8'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN8' in data['barcode']['codeid'] else "C"
EAN13 = data['barcode']['codeid']['EAN13'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN13' in data['barcode']['codeid'] else "D"
del(data)
class Offline_Login:
with open("config.yaml", 'r') as file:
data = safe_load(file)['options']
OFFLINE_LOGIN = data['users'] if "users" in data else ""
del(data)
class Scan_Options:
DELETE = "delete"
LOGOUT = "logout"

View File

@ -1,3 +1,4 @@
from constants import Barcode_CodeID, Scan_Options
from copy import deepcopy from copy import deepcopy
from datetime import date from datetime import date
from json import dump as jdump, load as jload from json import dump as jdump, load as jload
@ -27,17 +28,6 @@ consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.DEBUG) consoleHandler.setLevel(logging.DEBUG)
LOGGER.addHandler(consoleHandler) LOGGER.addHandler(consoleHandler)
with open("config.yaml", 'r') as file:
data = safe_load(file)['options']
CODEID_POS = data['barcode']['codeid']['position'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'position' in data['barcode']['codeid'] else None
CODE128 = data['barcode']['codeid']['Code128'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'Code128' in data['barcode']['codeid'] else "A"
EAN8 = data['barcode']['codeid']['EAN8'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN8' in data['barcode']['codeid'] else "C"
EAN13 = data['barcode']['codeid']['EAN13'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN13' in data['barcode']['codeid'] else "D"
OFFLINE_LOGIN = data['users'] if "users" in data else ""
del(data)
TEMPFILE = "scans.json" TEMPFILE = "scans.json"
TIMEOUT = 60 # Number of seconds for a timeout after being logged in TIMEOUT = 60 # Number of seconds for a timeout after being logged in
@ -63,7 +53,7 @@ def delete(scanned: list[dict[int: int]]):
scan = stdin.readline().strip() scan = stdin.readline().strip()
codeid, scan = split_codeid(scan, "") codeid, scan = split_codeid(scan, "")
match codeid: match codeid:
case str(EAN8): case Barcode_CodeID.EAN8:
try: try:
scanned.remove({scan: amount}) scanned.remove({scan: amount})
except ValueError as e: except ValueError as e:
@ -71,7 +61,7 @@ def delete(scanned: list[dict[int: int]]):
LOGGER.debug(f"Tried to delete {scan} with amount {amount}.") LOGGER.debug(f"Tried to delete {scan} with amount {amount}.")
finally: finally:
break break
case str(EAN13): case Barcode_CodeID.EAN13:
try: try:
scanned.remove({scan: amount}) scanned.remove({scan: amount})
except ValueError as e: except ValueError as e:
@ -79,20 +69,20 @@ def delete(scanned: list[dict[int: int]]):
LOGGER.debug(f"Tried to delete {scan} with amount {amount}.") LOGGER.debug(f"Tried to delete {scan} with amount {amount}.")
finally: finally:
break break
case str(CODE128): case Barcode_CodeID.CODE128:
match scan: match scan:
case "delete": case Scan_Options.DELETE:
try: try:
scanned.pop() scanned.pop()
except IndexError as e: except IndexError as e:
LOGGER.exception() LOGGER.exception("")
finally: finally:
break break
case _: case _:
try: try:
amount += int(scan) amount += int(scan)
except ValueError as e: except ValueError as e:
LOGGER.exception() LOGGER.exception("")
def group_previous_scans(previous_scans: list): def group_previous_scans(previous_scans: list):
newscans = [] newscans = []
@ -116,9 +106,9 @@ def group_scanning(scanned: list[dict[int: int]]) -> dict[int: int]:
for scan in scanned: for scan in scanned:
for key, value in scan.items(): for key, value in scan.items():
if key not in scan_dict: if key not in scan_dict:
scan_dict[scan] = value scan_dict[key] = value
else: else:
scan_dict[scan] += value scan_dict[key] += value
for key, value in scan_dict.items(): for key, value in scan_dict.items():
if value <= 0: if value <= 0:
del(scan_dict[key]) del(scan_dict[key])
@ -127,14 +117,14 @@ def group_scanning(scanned: list[dict[int: int]]) -> dict[int: int]:
def login(user: str = None): def login(user: str = None):
if not user: if not user:
user = input("Enter Login: ") user = input("Enter Login: ")
codeid, user = split_codeid(user, CODE128) codeid, user = split_codeid(user, Barcode_CodeID.CODE128)
else: else:
codeid = CODE128 codeid = Barcode_CodeID.CODE128
if codeid != CODE128: if codeid != Barcode_CodeID.CODE128:
return None return None
if not connection.check_login(user): if not connection.check_login(user):
LOGGER.debug("Login failed") LOGGER.debug("Login failed")
if not user in OFFLINE_LOGIN: if not user in Barcode_CodeID.OFFLINE_LOGIN:
return None return None
LOGGER.debug("Using local login") LOGGER.debug("Using local login")
return user return user
@ -149,17 +139,17 @@ def scanning(user: str) -> dict[int: int]:
scan = stdin.readline().strip() scan = stdin.readline().strip()
codeid, scan = split_codeid(scan, "A") codeid, scan = split_codeid(scan, "A")
match codeid: match codeid:
case str(EAN8): case Barcode_CodeID.EAN8:
scanned.append({scan: amount}) scanned.append({scan: amount})
amount = 1 amount = 1
case str(EAN13): case Barcode_CodeID.EAN13:
scanned.append({scan: amount}) scanned.append({scan: amount})
amount = 1 amount = 1
case str(CODE128): case Barcode_CodeID.CODE128:
match scan: match scan:
case "logout": case Scan_Options.LOGOUT:
break break
case "delete": case Scan_Options.DELETE:
delete(scanned) delete(scanned)
case _: case _:
try: try:
@ -202,7 +192,7 @@ def send_scan(user: str, scanned: dict[int: int], previous_scans: list[dict] = [
LOGGER.info(previous_scans) LOGGER.info(previous_scans)
def split_codeid(scan: str, default_codeid: str = ""): def split_codeid(scan: str, default_codeid: str = ""):
match CODEID_POS: match Barcode_CodeID.CODEID_POS:
case "prefix": case "prefix":
return(scan[0], scan[1:]) return(scan[0], scan[1:])
case "suffix": case "suffix":

View File

@ -66,9 +66,9 @@ class Database:
try: try:
result = cursor.fetchall() result = cursor.fetchall()
except ProgrammingError as e: except ProgrammingError as e:
LOGGER.exception() LOGGER.exception("")
except Exception as e: except Exception as e:
LOGGER.exception() LOGGER.exception("")
return result return result
@connectionpersistence @connectionpersistence
@ -97,9 +97,9 @@ class Database:
try: try:
result = cursor.fetchall() result = cursor.fetchall()
except ProgrammingError as e: except ProgrammingError as e:
LOGGER.exception() LOGGER.exception("")
except Exception as e: except Exception as e:
LOGGER.exception() LOGGER.exception("")
return result return result
@connectionpersistence @connectionpersistence
@ -125,9 +125,9 @@ class Database:
failed = {'user': user, 'items': {value['item']: value['amount']}} failed = {'user': user, 'items': {value['item']: value['amount']}}
if date: if date:
failed['date'] = date failed['date'] = date
LOGGER.exception() LOGGER.exception("")
except Exception as e: except Exception as e:
LOGGER.exception() LOGGER.exception("")
return failed return failed
def __delete__(self): def __delete__(self):