-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.py
129 lines (100 loc) · 3.53 KB
/
db.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
#
# Copyright 2015-2019 - Gabriel Acosta <acostadariogabriel@gmail.com>
#
# This file is part of Pireal.
#
# Pireal is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# Pireal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pireal; If not, see <http://www.gnu.org/licenses/>.
import logging
from pireal.core.file_manager import (
generate_database,
parse_database_content,
)
from pireal.core.relation import Relation
from pireal.core.file_manager import File
logger = logging.getLogger('core.db')
class DBError(Exception):
pass
class DBIOError(DBError):
pass
class DB:
def __init__(self, path=None):
self._dirty = False
self._relations = {}
self.file = File(path=path)
if path is not None:
self._dirty = True
@property
def relations(self) -> tuple:
return tuple(self._relations.values())
@property
def relation_names(self) -> tuple:
return tuple(self._relations.keys())
def give_relation(self, name: str) -> Relation:
if name not in self._relations:
raise NameError(f'Relation {name} not found')
return self._relations[name]
def display_name(self) -> str:
return self.file.display_name
def file_path(self) -> str:
return self.file.path
def is_new(self) -> bool:
return self.file.is_new
def is_dirty(self) -> bool:
return self._dirty
def add(self, relation: Relation):
if relation.name in self._relations:
raise NameError(f'Relation {relation.name} alredy exist')
self._relations[relation.name] = relation
self._dirty = True
def remove_from_name(self, name: str):
try:
del self._relations[name]
except KeyError:
logger.warning(f'Relation {name} not exist, nothing to remove')
return
self._dirty = True
def save(self, file_path=None):
logger.debug('Generating database in %s', self.file.path)
db_content = generate_database(self._relations)
self.file.save(db_content, path=file_path)
self._dirty = False
def load(self, file_path=None):
db_content = parse_database_content(self.file.read(file_path))
self._load_relations_from_content(db_content)
def _load_relations_from_content(self, content: list):
for table in content:
relation_name = table['name']
header = table['header']
tuples = table['tuples']
relation = Relation()
relation.name = relation_name
relation.header = header
for t in tuples:
relation.insert(t)
self.add(relation)
self._dirty = False
def __iter__(self):
for name, relation in self._relations.items():
yield name, relation
def __getitem__(self, key):
return self._relations[key]
def __len__(self):
return len(self._relations)
def __repr__(self):
return (
f'DB(name={self.display_name()} '
f'path={self.file_path()} '
f'relations={len(self)})'
)