forked from Vocab-Apps/anki-hyper-tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_status.py
145 lines (116 loc) · 5.25 KB
/
batch_status.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
import sys
constants = __import__('constants', globals(), locals(), [], sys._addon_import_level_base)
errors = __import__('errors', globals(), locals(), [], sys._addon_import_level_base)
logging_utils = __import__('logging_utils', globals(), locals(), [], sys._addon_import_level_base)
logger = logging_utils.get_child_logger(__name__)
class NoteStatus():
def __init__(self, note_id):
self.note_id = note_id
self.source_text = None
self.processed_text = None
self.sound_file = None
self.error = None
self.status = None
class BatchNoteActionContext():
def __init__(self, batch_status, note_id):
self.batch_status = batch_status
self.note_id = note_id
def __enter__(self):
return self
def __exit__(self, exception_type, exception_value, traceback):
if exception_value != None:
if isinstance(exception_value, errors.HyperTTSError):
self.batch_status.report_known_error(self.note_id, exception_value)
else:
self.batch_status.report_unknown_exception(self.note_id, exception_value)
self.batch_status.notify_change(self.note_id)
return True
self.batch_status.notify_change(self.note_id)
return False
def set_sound(self, sound_file):
self.batch_status.set_sound_file(self.note_id, sound_file)
def set_source_text(self, source_text):
self.batch_status.set_source_text(self.note_id, source_text)
def set_processed_text(self, processed_text):
self.batch_status.set_processed_text(self.note_id, processed_text)
def set_status(self, status):
self.batch_status.set_status(self.note_id, status)
class BatchRunningActionContext():
def __init__(self, batch_status):
self.batch_status = batch_status
def __enter__(self):
self.batch_status.task_running = True
self.batch_status.must_continue = True
self.batch_status.notify_start()
return self
def __exit__(self, exception_type, exception_value, traceback):
self.batch_status.task_running = False
completed = self.batch_status.must_continue
self.batch_status.notify_end(completed)
return False
class BatchStatus():
def __init__(self, anki_utils, note_id_list, change_listener):
self.anki_utils = anki_utils
self.note_id_list = note_id_list
self.change_listener = change_listener
self.note_status_array = []
self.note_status_map = {}
self.note_id_map = {}
self.task_running = False
self.must_continue = False
i = 0
for note_id in self.note_id_list:
note_status = NoteStatus(note_id)
self.note_status_array.append(note_status)
self.note_status_map[note_id] = note_status
self.note_id_map[note_id] = i
i += 1
def is_running(self):
return self.task_running
def stop(self):
logger.info('stopping current batch')
self.must_continue = False
def __getitem__(self, array_index):
return self.note_status_array[array_index]
def get_batch_running_action_context(self):
return BatchRunningActionContext(self)
def get_note_action_context(self, note_id, blank_fields):
note_status = self.note_status_map[note_id]
note_status.error = None
note_status.status = constants.BatchNoteStatus.Processing
if blank_fields:
note_status.source_text = None
note_status.processed_text = None
note_status.sound_file = None
return BatchNoteActionContext(self, note_id)
# error reporting
def report_known_error(self, note_id, exception_value):
self.note_status_map[note_id].status = constants.BatchNoteStatus.Error
self.note_status_map[note_id].error = exception_value
self.notify_change(note_id)
def report_unknown_exception(self, note_id, exception_value):
self.note_status_map[note_id].status = constants.BatchNoteStatus.Error
self.note_status_map[note_id].error = exception_value
self.anki_utils.report_unknown_exception_background(exception_value)
self.notify_change(note_id)
# set the various fields on the NoteStatus
def set_source_text(self, note_id, source_text):
self.note_status_map[note_id].source_text = source_text
self.notify_change(note_id)
def set_processed_text(self, note_id, processed_text):
self.note_status_map[note_id].processed_text = processed_text
self.notify_change(note_id)
def set_sound_file(self, note_id, sound_file):
self.note_status_map[note_id].sound_file = sound_file
self.notify_change(note_id)
def set_status(self, note_id, status):
self.note_status_map[note_id].status = status
self.notify_change(note_id)
def notify_start(self):
self.start_time = self.anki_utils.get_current_time()
self.change_listener.batch_start()
def notify_change(self, note_id):
row = self.note_id_map[note_id]
self.change_listener.batch_change(note_id, row, len(self.note_id_list), self.start_time, self.anki_utils.get_current_time())
def notify_end(self, completed):
self.change_listener.batch_end(completed)