-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManagerBase.py
More file actions
51 lines (42 loc) · 1.67 KB
/
DataManagerBase.py
File metadata and controls
51 lines (42 loc) · 1.67 KB
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
import threading
import yaml
from FileManager import FileManager
from models.NoteList import NoteList
from utils.YamlUtils import YamlUtils
class DataManagerBase:
def __init__(self, file_manager: FileManager) -> None:
self.file_manager = file_manager
def read_data_from_file_async(self, first_callback: callable, args: dict = None):
thread = threading.Thread(
target=self._read_data_from_file,
kwargs={
"read_instance": self.file_manager.get_read_instance(),
"first_callback":first_callback,
"args": args
})
thread.start()
def _read_data_from_file(self, read_instance, first_callback, args) -> None:
with read_instance as file:
data = yaml.load(file, Loader=YamlUtils.get_loader())
if args is not None:
first_callback(data, args)
else:
first_callback(data)
def get_data_from_file(self) -> NoteList:
with self.file_manager.get_read_instance() as file:
data = yaml.load(file, Loader=YamlUtils.get_loader())
return data
def _write_data_to_file_async(self, note_list: NoteList, callback: callable):
thread = threading.Thread(
target=self.__write_data_to_file,
kwargs={
"write_instance": self.file_manager.get_write_instance(),
"note_list": note_list,
"callback":callback
})
thread.start()
@staticmethod
def __write_data_to_file(write_instance, note_list: NoteList, callback):
with (write_instance as file):
yaml.dump(note_list, file)
callback(note_list)