/
adapters.py
168 lines (130 loc) · 3.85 KB
/
adapters.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from typing import (
TypeVar,
Union,
)
from abc import ABC, abstractmethod
import os
from os import path
import json
try:
import yaml
except ImportError as e:
print('yaml is not installed, please install it with pip install PyYAML')
except Exception:
print('Error cannot import yaml')
__all__ = [
'Adapter',
'TextFile',
'JsonFile',
'Memory',
'YAMLFile',
]
T = TypeVar('T')
class Adapter(ABC):
"""
The abstract base class for all Adapters.
An Adapter (de)serializes the current state of the database and stores it in
some place (memory, file on disk, ...).
"""
@abstractmethod
def read(self) -> T:
"""
Any kind of deserialization should go here.
Return ``None`` here to indicate that the storage is empty.
"""
raise NotImplementedError('To be overridden!')
@abstractmethod
def write(self, data: T) -> None:
"""
Any kind of serialization should go here.
"""
raise NotImplementedError('To be overridden!')
class TextFile(Adapter):
"""
Adapter for reading and writing text. Useful for creating custom adapters.
"""
def __init__(self, filename: str) -> None:
"""
Create a new instance.
Also creates the storage file, if it doesn't exist.
:param filename: Where to store the data.
:type filename: str
"""
super().__init__()
self.filename: str = filename
self.tmp_filename: str = path.join(
path.dirname(filename),
f'.{path.basename(filename)}.tmp'
)
with open(self.filename, 'a'):
os.utime(self.filename)
def read(self) -> Union[str, None]:
try:
with open(self.filename, mode='r+', encoding='utf-8') as f:
data = f.read()
except Exception as e:
print(e)
return None
else:
return data or None
def write(self, data: str) -> None:
with open(self.tmp_filename, 'w') as f:
f.write(data)
os.remove(self.filename)
os.rename(self.tmp_filename, self.filename)
class JSONFile(Adapter):
"""
Adapter for reading and writing JSON files.
"""
def __init__(self, filename: str) -> None:
"""
Create a new instance.
Also creates the storage file, if it doesn't exist.
:param filename: Where to store the JSON data.
:type filename: str
"""
self.adapter: Adapter = TextFile(filename=filename)
def read(self) -> Union[T, None]:
data = self.adapter.read()
if not data or data is None:
return None
else:
return json.loads(data)
def write(self, data: T) -> None:
self.adapter.write(json.dumps(data))
class Memory(Adapter):
"""
In-memory adapter. Useful for speeding up unit tests.
Store the data as it is in memory.
"""
def __init__(self) -> None:
"""
Create a new instance.
"""
super().__init__()
self.data: T = None
def read(self) -> T:
return self.data or None
def write(self, data: T) -> None:
self.data = data
class YAMLFile(Adapter):
"""
Adapter for reading and writing YAML files.
"""
def __init__(self, filename: str) -> None:
"""
Create a new instance.
Also creates the storage file, if it doesn't exist.
:param filename: Where to store the YAML data.
:type filename: str
"""
super().__init__()
self.adapter: Adapter = TextFile(filename)
def read(self) -> Union[T, None]:
data = self.adapter.read()
if not data or data is None:
return None
else:
return yaml.safe_load(data)
def write(self, data: T) -> None:
self.adapter.write(yaml.dump(data))