-
Notifications
You must be signed in to change notification settings - Fork 7
/
operations.py
187 lines (146 loc) · 6.4 KB
/
operations.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from threading import Thread
from abc import ABCMeta, abstractmethod
import time
class Operation(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None):
Thread.__init__(self, group, target, name, args, kwargs, Verbose)
self.callback = None
self.exception_callback = None
def set_callback(self, callback, kwargs={}):
self.callback = callback
self.callback_kwargs = kwargs
def set_exception_callback(self, callback):
self.exception_callback = callback
def join(self):
Thread.join(self)
if self.callback:
result = self.get_result()
if not isinstance(result, Exception):
self.callback( result, **self.callback_kwargs )
elif self.exception_callback:
self.exception_callback( result )
else:
print(str(result))
def get_result(self):
return None
def get_run_finished_text(self):
return None
def get_update_run_text(self):
return None
class NoteCreator(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
self.simplenote_instance = simplenote_instance
def run(self):
print('QuickSimplenote: Creating note')
operation_result = self.simplenote_instance.add_note('')
if operation_result[1] == 0:
self.result = operation_result[0]
else:
self.result = Exception("Error creating note")
def get_run_finished_text(self):
return 'QuickSimplenote: Done'
def get_update_run_text(self):
return 'QuickSimplenote: Creating note'
def get_result(self):
return self.result
class NoteDownloader(Thread):
def __init__(self, note_id, semaphore, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None):
Thread.__init__(self, group, target, name, args, kwargs, Verbose)
self.note_id = note_id
self.semaphore = semaphore
self.simplenote_instance = simplenote_instance
def run(self):
self.semaphore.acquire()
print('QuickSimplenote: Downloading %s' % self.note_id)
operation_result = self.simplenote_instance.get_note(self.note_id)
if operation_result[1] == 0:
self.result = operation_result[0]
else:
self.result = Exception("Error getting note")
self.semaphore.release()
def join(self):
Thread.join(self)
return self.result
class MultipleNoteContentDownloader(Operation):
def __init__(self, semaphore, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None, notes=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
self.notes = notes
self.semaphore = semaphore
self.simplenote_instance = simplenote_instance
def run(self):
threads = []
for current_note in self.notes:
new_thread = NoteDownloader(current_note['key'], self.semaphore, simplenote_instance=self.simplenote_instance)
threads.append(new_thread)
new_thread.start()
operation_result = [thread.join() for thread in threads]
error = False
for an_object in operation_result:
if isinstance(an_object, Exception):
error = True
if not error:
self.result = operation_result
else:
self.result = Exception("Error getting note")
def get_result(self):
return self.result
def get_run_finished_text(self):
return 'QuickSimplenote: Done'
def get_update_run_text(self):
return 'QuickSimplenote: Downloading contents'
class GetNotesDelta(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
self.note_resume = []
self.simplenote_instance = simplenote_instance
def run(self):
note_resume_operation = self.simplenote_instance.get_note_list()
if note_resume_operation[1] == 0:
note_resume = note_resume_operation[0]
self.result = [note for note in note_resume if note['deleted'] == 0]
else:
self.result = Exception("Error getting notes")
def get_result(self):
return self.result
def get_run_finished_text(self):
return 'QuickSimplenote: Done'
def get_update_run_text(self):
return 'QuickSimplenote: Downloading note list'
class NoteDeleter(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, note=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
self.note = note
self.simplenote_instance = simplenote_instance
def get_run_finished_text(self):
return None
def get_update_run_text(self):
return 'QuickSimplenote: Deleting note'
def run(self):
print('QuickSimplenote: Deleting %s' % self.note['key'])
deletion_operation = self.simplenote_instance.trash_note(self.note['key'])
if deletion_operation[1] == 0:
self.result = True
else:
self.result = Exception("Error deleting note")
def get_result(self):
return self.result
class NoteUpdater(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, note=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
self.note = note
self.simplenote_instance = simplenote_instance
def run(self):
print('QuickSimplenote: Updating %s' % self.note['key'])
self.note['modifydate'] = time.time()
note_update_operation = self.simplenote_instance.update_note(self.note)
if note_update_operation[1] == 0:
self.result = note_update_operation[0]
else:
self.result = Exception("Error updating note")
def get_result(self):
return self.result
def get_run_finished_text(self):
return 'QuickSimplenote: Done'
def get_update_run_text(self):
return 'QuickSimplenote: Updating note'