summaryrefslogtreecommitdiff
path: root/src/pyssg/database.py
blob: 61ca502939d9b68e573fa49e1b1625c88195ef8e (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
89
90
91
92
93
import os


# db class that works for both html and md files
class Database:
    def __init__(self, db_path: str):
        self.db_path: str = db_path
        self.e: dict[str, tuple[float, float, list[str]]] = dict()

        self.__read()


    def update_tags(self, file_name: str,
                    tags: list[str]) -> None:
        if file_name in self.e:
            cts, mts, _ = self.e[file_name]
            self.e[file_name] = (cts, mts, tags)


    # returns a bool that indicates if the entry
    # was (includes new entries) or wasn't updated
    # 0.0 means no mod
    def update(self, file_name: str,
               remove: str=None) -> bool:
        # initial default values
        f: str = file_name
        tags: list[str] = []
        if remove is not None:
            f = file_name.replace(remove, '')


        # get current time, needs actual file name
        time: float = os.stat(file_name).st_mtime

        # three cases, 1) entry didn't exist,
        # 2) entry hasn't been mod and,
        # 3) entry has been mod
        #1)
        if f not in self.e:
            self.e[f] = (time, 0.0, tags)
            return True

        old_time, old_mod_time, tags = self.e[f]

        # 2)
        if old_mod_time == 0.0:
            if time > old_time:
                self.e[f] = (old_time, time, tags)
                return True
        # 3)
        else:
            if time > old_mod_time:
                self.e[f] = (old_time, time, tags)
                return True

        return False


    def write(self) -> None:
        with open(self.db_path, 'w') as file:
            # write each k,v pair in dict to db file
            for k, v in self.e.items():
                t: str = None
                if len(v[2]) == 0:
                    t = '-'
                else:
                    t = ','.join(v[2])
                file.write(f'{k} {v[0]} {v[1]} {t}\n')


    def __read(self) -> None:
        # only if the path exists and it is a file
        if os.path.exists(self.db_path) and os.path.isfile(self.db_path):
            # get all db file lines
            lines: list[str] = None
            with open(self.db_path, 'r') as file:
                lines = file.readlines()

            # parse each entry and populate accordingly
            l: list[str] = None
            # l=list of values in entry
            for line in lines:
                l = tuple(line.strip().split())
                if len(l) != 4:
                    raise Exception('db entry doesn\'t contain 4 elements')

                t: list[str] = None
                if l[3] == '-':
                    t = []
                else:
                    t = l[3].split(',')

                self.e[l[0]] = (float(l[1]), float(l[2]), t)