summaryrefslogtreecommitdiffstats
path: root/py/xas/status.py
blob: 1465616253014617d42d45a4ccf63a47b29d2306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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]