Projekt

Obecné

Profil

« Předchozí | Další » 

Revize fa03de5c

Přidáno uživatelem Michal Seják před asi 4 roky(ů)

Re #8476 - Auxiliary script for database initialization, FileAnchor

Zobrazit rozdíly:

src/constants.py
1
from src.utils.file_anchor import FileAnchor
2

  
1 3
DATABASE_FILE = "db/database_sqlite.db"
4
DATABASE_FILE_LOCATION = FileAnchor("aswi2021jmsd", DATABASE_FILE)
5

  
2 6

  
3 7
# Types of certificates
4 8
ROOT_CA_ID = 1
src/db/setup_database.py
1
import sqlite3
2

  
3
from src.constants import DATABASE_FILE_LOCATION
4
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
5

  
6

  
7
def setup_database():
8
    co = sqlite3.connect(DATABASE_FILE_LOCATION.shortest_relative_path())
9
    cu = co.cursor()
10
    cu.executescript(SCHEMA_SQL)
11
    cu.executescript(DEFAULT_VALUES_SQL)
12

  
13

  
14
if __name__ == '__main__':
15
    setup_database()
src/utils/file_anchor.py
1
import os
2

  
3

  
4
class FileAnchor:
5
    def __init__(self, root_dir, tail, searching=False):
6
        self.__root_dir = root_dir
7
        self.__tail = tail
8
        if searching:
9
            self.shortest_relative_path = self.__srp_with_tree_search
10
        else:
11
            self.shortest_relative_path = self.__srp
12

  
13
    def __srp_with_tree_search(self, src=None):
14
        """
15
        Shortest relative path handling the situation where src is not a descendant of root_dir.
16
        Searches the subtree of src until it finds root_dir.
17
        :param src: source node or cwd
18
        :return: path from source to the anchored file
19
        """
20
        raise NotImplementedError("Not implemented yet.")
21

  
22
    def __srp(self, src=None):
23
        """
24
        Shortest relative path from src to the anchored file. If src is not a descendant of root_dir, returns None.
25
        :param src: source node or cwd
26
        :return: path from source to the anchored file or None
27
        """
28
        if src is None:
29
            src = os.getcwd()
30

  
31
        ret = ""
32
        h = src
33
        while len(h) > 0 and h != "/":
34
            h, t = os.path.split(h)
35
            if t != self.__root_dir:
36
                ret = os.path.join(ret, "..")
37
            else:
38
                return os.path.join(ret, self.__tail)
39

  
40
        return None
41

  
42
    def __str__(self):
43
        return self.shortest_relative_path(None)

Také k dispozici: Unified diff