From a37783ef4efca7b469c43cb7935278ec7de7f773 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 30 Dec 2021 21:40:04 -0500 Subject: rename lib/ to py/ for ~reasons~ --- lib/xas/message.py | 24 --------------- lib/xas/status.py | 88 ------------------------------------------------------ py/xas/message.py | 24 +++++++++++++++ py/xas/status.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 112 deletions(-) delete mode 100644 lib/xas/message.py delete mode 100644 lib/xas/status.py create mode 100644 py/xas/message.py create mode 100644 py/xas/status.py diff --git a/lib/xas/message.py b/lib/xas/message.py deleted file mode 100644 index e297e40..0000000 --- a/lib/xas/message.py +++ /dev/null @@ -1,24 +0,0 @@ -class Formatter(): - def __init__(self, id: int, db): - self.id = id - self.db = db - - def fmt(self, lookup, code, text=None): - status = lookup(code) - output = "%04d :: Code %03d" % (self.id, code) - - if status.typeobj.name == status.description: - output += " :: " + status.typeobj.name - else: - output += " :: " + status.description - - if text is not None: - output += " :: " + text - - return output - - def message(self, code, text=None): - return self.fmt(self.db.status, code, text) - - def error(self, code, text=None): - return self.fmt(self.db.error, code, text) diff --git a/lib/xas/status.py b/lib/xas/status.py deleted file mode 100644 index 1465616..0000000 --- a/lib/xas/status.py +++ /dev/null @@ -1,88 +0,0 @@ -import sqlite3 - -class StatusType(): - def __init__(self, id, name): - self.id = id - self.name = name - - def __str__(self): - return self.name - -class ErrorType(StatusType): - pass - -class Status(): - def __init__(self, id: int, typeobj: StatusType, description: str): - self.id = id - self.typeobj = typeobj - self.description = description - - def __str__(self): - return self.description - -class Error(Status): - pass - -class Database(): - def __init__(self, filename: str): - self.db = sqlite3.connect(filename) - self.db.row_factory = sqlite3.Row - - self.statusTypes = dict() - self.statuses = dict() - - self.errorTypes = dict() - self.errors = dict() - - self.load_types() - self.load_statuses() - - def load_types(self): - targets = { - "xas_status_type": (StatusType, self.statusTypes), - "xas_error_type": (ErrorType, self.errorTypes) - } - - for table in targets.keys(): - typeof = targets[table][0] - target = targets[table][1] - - cr = self.db.cursor() - cr.execute(f"select id, name from {table}") - - for row in cr.fetchall(): - typeobj = typeof(row['id'], row['name']) - - target[typeobj.id] = typeobj - - def status_type(self, id: int): - return self.statusTypes[id] - - def error_type(self, id: int): - return self.errorTypes[id] - - def load_statuses(self): - targets = { - "xas_status": (Status, self.statusTypes, self.statuses), - "xas_error": (Error, self.errorTypes, self.errors) - } - - for table in targets.keys(): - typeof = targets[table][0] - types = targets[table][1] - target = targets[table][2] - - cr = self.db.cursor() - cr.execute(f"select id, type_id, description from {table}") - - for row in cr.fetchall(): - typeobj = types[row['type_id']] - obj = typeof(row['id'], types[row['type_id']], row['description']) - - target[obj.id] = obj - - def status(self, id): - return self.statuses[id] - - def error(self, id): - return self.errors[id] diff --git a/py/xas/message.py b/py/xas/message.py new file mode 100644 index 0000000..e297e40 --- /dev/null +++ b/py/xas/message.py @@ -0,0 +1,24 @@ +class Formatter(): + def __init__(self, id: int, db): + self.id = id + self.db = db + + def fmt(self, lookup, code, text=None): + status = lookup(code) + output = "%04d :: Code %03d" % (self.id, code) + + if status.typeobj.name == status.description: + output += " :: " + status.typeobj.name + else: + output += " :: " + status.description + + if text is not None: + output += " :: " + text + + return output + + def message(self, code, text=None): + return self.fmt(self.db.status, code, text) + + def error(self, code, text=None): + return self.fmt(self.db.error, code, text) diff --git a/py/xas/status.py b/py/xas/status.py new file mode 100644 index 0000000..1465616 --- /dev/null +++ b/py/xas/status.py @@ -0,0 +1,88 @@ +import sqlite3 + +class StatusType(): + def __init__(self, id, name): + self.id = id + self.name = name + + def __str__(self): + return self.name + +class ErrorType(StatusType): + pass + +class Status(): + def __init__(self, id: int, typeobj: StatusType, description: str): + self.id = id + self.typeobj = typeobj + self.description = description + + def __str__(self): + return self.description + +class Error(Status): + pass + +class Database(): + def __init__(self, filename: str): + self.db = sqlite3.connect(filename) + self.db.row_factory = sqlite3.Row + + self.statusTypes = dict() + self.statuses = dict() + + self.errorTypes = dict() + self.errors = dict() + + self.load_types() + self.load_statuses() + + def load_types(self): + targets = { + "xas_status_type": (StatusType, self.statusTypes), + "xas_error_type": (ErrorType, self.errorTypes) + } + + for table in targets.keys(): + typeof = targets[table][0] + target = targets[table][1] + + cr = self.db.cursor() + cr.execute(f"select id, name from {table}") + + for row in cr.fetchall(): + typeobj = typeof(row['id'], row['name']) + + target[typeobj.id] = typeobj + + def status_type(self, id: int): + return self.statusTypes[id] + + def error_type(self, id: int): + return self.errorTypes[id] + + def load_statuses(self): + targets = { + "xas_status": (Status, self.statusTypes, self.statuses), + "xas_error": (Error, self.errorTypes, self.errors) + } + + for table in targets.keys(): + typeof = targets[table][0] + types = targets[table][1] + target = targets[table][2] + + cr = self.db.cursor() + cr.execute(f"select id, type_id, description from {table}") + + for row in cr.fetchall(): + typeobj = types[row['type_id']] + obj = typeof(row['id'], types[row['type_id']], row['description']) + + target[obj.id] = obj + + def status(self, id): + return self.statuses[id] + + def error(self, id): + return self.errors[id] -- cgit v1.2.3