-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
313 lines (237 loc) · 12.1 KB
/
server.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from concurrent import futures
import time
import grpc
import os
from api.archetype import Archetype
from api.conditional import Conditional
from api.connection import Connection
from api.container import Container
from api.document import Document
from api.note import Note
import tasks_pb2
import tasks_pb2_grpc
documents = dict()
class DocumentServicer(tasks_pb2_grpc.DocumentManagerServicer):
def CreateDocument(self, request, context):
document = Document()
documents[document.id] = document
return tasks_pb2.CreateDocumentReply(id=document.id)
def CloseDocument(self, request, context):
del documents[request.id]
return tasks_pb2.BoolWrapper(val=True)
def SaveDocument(self, request, context):
documents[request.document_id].save_document(request.filename)
return tasks_pb2.BoolWrapper(val=True)
def LoadDocument(self, request, context):
document = Document.load_document(request.file)
documents[document.id] = document
return tasks_pb2.CreateDocumentReply(id=document.id)
def SyncDrive(self, request, context):
document = documents[request.document_id]
new_notes = document.sync_with_drive(request.drive)
for note in new_notes:
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs)
def CreateNotesFromDrive(self, request, context):
document = documents[request.document_id]
new_notes = document.create_notes_from_gdrive(request.item_id)
for note in new_notes:
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs)
def UploadToDrive(self, request, context):
document = documents[request.document_id]
for note in document.upload_notes_to_drive(request.drive):
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs)
class NoteServicer(tasks_pb2_grpc.NoteManagerServicer):
def CreateNote(self, request, context):
document = documents[request.document_id]
container_id = request.parent_container_id
note = Note(title=request.attrs["title"],
text=request.attrs["text"],
id=request.id,
attrs=dict(request.attrs),
parent_container=document.children[container_id]
if len(request.parent_container_id) > 0
else None)
document.children[note.id] = note
return tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs,
parent_container_id=container_id)
def UpdateNoteAttr(self, request, context):
note = documents[request.document_id].children[request.note_id]
updated_notes = note.update_attr(request.attr, request.new_value)
for note in updated_notes:
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs,
inherited_attrs=list(note.inherited_attrs))
def CreateDescendantNote(self, request, context):
document = documents[request.document_id]
note = documents[request.document_id].children[request.id]
container_id = request.parent_container_id
descendant = note.create_descendant(request.descendant_id)
document.children[descendant.id] = descendant
return tasks_pb2.NoteReply(id=descendant.id,
attrs=descendant.attrs,
parent_container_id=container_id,
prototype_id=note.id,
inherited_attrs=list(descendant.inherited_attrs))
def DeleteNoteAttr(self, request, context):
note = documents[request.document_id].children[request.note_id]
note.delete_attr(request.attr)
return tasks_pb2.NoteReply(attrs=note.attrs,
inherited_attrs=list(note.inherited_attrs))
def DeleteNote(self, request, context):
note = documents[request.document_id].children[request.id]
descendants = note.prepare_deletion()
descendant_ids = [descendant.id for descendant in descendants]
del documents[request.document_id].children[request.id]
return tasks_pb2.DeleteNoteReply(descendant_notes=descendant_ids)
def CreateArchetype(self, request, context):
archetype = Archetype(name=request.name, attrs=request.attrs)
documents[request.document_id].children[archetype.id] = archetype
return tasks_pb2.ArchetypeReply(name=archetype.name,
id=archetype.id,
attrs=archetype.attrs)
def EditArchetypeName(self, request, context):
archetype = documents[request.document_id].children[request.id]
archetype.name = request.name
return tasks_pb2.ArchetypeReply(name=archetype.name,
id=archetype.id)
def CreateInheritor(self, request, context):
archetype = documents[request.document_id].children[request.archetype_id]
note = archetype.create_inheritor(id=request.note_id)
documents[request.document_id].children[note.id] = note
return tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs,
prototype_id=archetype.id,
inherited_attrs=list(note.inherited_attrs))
def UpdateArchetypeAttr(self, request, context):
archetype = documents[request.document_id].children[request.archetype_id]
updated = archetype.update_attr(attr=request.attr, val=request.val)
for note in updated:
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs,
inherited_attrs=list(note.inherited_attrs))
def CreateNoteFromFile(self, request, context):
document = documents[request.document_id]
for path in request.paths:
# todo: make recursive
if os.path.isdir(path):
for filename in os.listdir(path):
if not filename.startswith("."):
note = Note.from_file(os.path.join(path, filename))
if note:
document.children[note.id] = note
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs)
else:
note = Note.from_file(path)
if note:
document.children[note.id] = note
yield tasks_pb2.NoteReply(id=note.id,
attrs=note.attrs)
class ConnectionServicer(tasks_pb2_grpc.ConnectionManagerServicer):
def CreateConnection(self, request, context):
document = documents[request.document_id]
endpoint_one_id = request.endpoint_one_id
endpoint_one = (document.children[request.endpoint_one_id]
if request.endpoint_one_id
else None)
endpoint_two_id = request.endpoint_two_id
endpoint_two = (document.children[request.endpoint_two_id]
if request.endpoint_two_id
else None)
connection = Connection(endpoint_one=endpoint_one,
endpoint_two=endpoint_two,
text=request.text,
id=request.id)
documents[request.document_id].children[connection.id] = connection
return tasks_pb2.ConnectionReply(id=connection.id,
endpoint_one_id=endpoint_one_id,
endpoint_two_id=endpoint_two_id,
text=request.text)
def AddEndpoint(self, request, context):
document = documents[request.document_id]
new_endpoint_id = request.endpoint_two_id
new_endpoint = document.children[new_endpoint_id]
connection = documents[request.document_id].children[request.id]
connection.change_second_endpoint(new_endpoint)
return tasks_pb2.ConnectionReply(id=connection.id,
endpoint_two_id=new_endpoint_id)
def AddLabel(self, request, context):
connection = documents[request.document_id].children[request.id]
connection.change_label(request.text)
return tasks_pb2.ConnectionReply(id=connection.id)
def DeleteConnection(self, request, context):
del documents[request.document_id].children[request.id]
return tasks_pb2.BoolWrapper(val=True)
class ContainerServicer(tasks_pb2_grpc.ContainerManagerServicer):
def CreateContainer(self, request, context):
document = documents[request.document_id]
notes = [document.children[note_id]
for note_id in request.child_note_ids]
container = Container(attrs=request.attrs,
notes=notes)
document.children[container.id] = container
return tasks_pb2.ContainerReply(id=container.id,
attrs=container.attrs,
child_note_ids=request.child_note_ids)
def AddNote(self, request, context):
document = documents[request.document_id]
container = document.children[request.container_id]
note = document.children[request.note_id]
container.add_note(note)
child_note_ids = [note.id for note in container.notes]
return tasks_pb2.ContainerReply(id=container.id,
child_note_ids=child_note_ids)
def RemoveNote(self, request, context):
document = documents[request.document_id]
container = document.children[request.container_id]
note = document.children[request.note_id]
container.remove_note(note)
child_note_ids = [note.id for note in container.notes]
return tasks_pb2.ContainerReply(id=container.id,
child_note_ids=child_note_ids)
def SearchChildNoteAttrs(self, request, context):
document = documents[request.document_id]
container = document.children[request.container_id]
condition = document.children[request.conditional_id]
attrs = request.attrs
result = container.search_child_note_attrs(condition=condition,
attrs=attrs)
result = [note.id for note in result]
return tasks_pb2.ContainerSearchReply(result=result)
def DeleteContainer(self, request, context):
pass
class ConditionalServicer(tasks_pb2_grpc.ConditionalManagerServicer):
def CreateConditional(self, request, context):
document = documents[request.document_id]
condition = Conditional(target=request.target,
condition=request.condition)
document.children[condition.id] = condition
return tasks_pb2.ConditionalReply(id=condition.id,
target=request.target,
condition=request.condition)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
tasks_pb2_grpc.add_DocumentManagerServicer_to_server(DocumentServicer(),
server)
tasks_pb2_grpc.add_NoteManagerServicer_to_server(NoteServicer(),
server)
tasks_pb2_grpc.add_ConnectionManagerServicer_to_server(
ConnectionServicer(),
server)
tasks_pb2_grpc.add_ContainerManagerServicer_to_server(ContainerServicer(),
server)
tasks_pb2_grpc.add_ConditionalManagerServicer_to_server(
ConditionalServicer(),
server)
server.add_insecure_port('[::]:50051')
server.start()
try:
while True:
time.sleep(100000)
except KeyboardInterrupt:
server.stop(0)
serve()