-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtree.py
522 lines (446 loc) · 18.4 KB
/
tree.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 9 23:49:38 2015
@author: thor
"""
from json import loads, dumps
from time import time
from hashlib import md5, sha1
from random import randint
import traceback
import re
import logging
from user import SelfUser, OtherUser
from sqlitedb import GetOneArticle, SaveArticle, SaveTopicLabels, SaveTopic, UpdateTopic, GetRootIds, \
GetTreeAtcls, SetAtclsWithoutTopic, GetAtclByUser, GetTopicRowById, \
GetTopicRows, SetAtclStatus, SetLabel, GetAtclByUserToShow, SearchAtcl
from const import MaxOfferRootNum, TitleLength, AutoNode, SeekSelfUser, TopicNumPerPage
class Article( object ):
""
#------------------ for status
BLOCK = -1
RECEIVE = 0
PASS = 1
GOOD = 2
#------------------ for Type
NORMAL = 0
LIKE = 1
CHAT = 2
#------------------
MinTime = 1439596800000 #2015-8-15 00:00:00
LiveD = {}
def __init__( self, Id = None, itemD = None, itemStr = '', content = '', status = None, labelStr = '' ):
""
if Id is None: #create
self.ItemD = itemD
self.status = 0
self.ItemStr = dumps( itemD )
self.id = sha1( self.ItemStr ).hexdigest()
else: #receive or load from local
self.id = Id
self.status = int( AutoNode ) if status is None else status
self.ItemStr = itemStr
self.ItemD = loads( itemStr )
self.content = content
self.LabelStr = labelStr
self.NodeLabels = ( labelStr + '|' ).split( '|' )[1].split( ',' )
def Issue( self ):
""
return {
'Id': self.id,
'Items': self.ItemStr,
'Content': self.content,
'NodeLabels': u','.join( self.NodeLabels ),
'NodeEval': self.status
}
def Save( self, **kwds ):
""
kwds.update( {
'id': self.id,
'root': self.ItemD.get( 'RootID', self.id ), #for root, the itemD is filled before creating rootId. so, itemD.RootID is null
'items': self.ItemStr,
'content': self.content,
'Type': self.ItemD['Type'],
'CreateTime': self.ItemD['CreateTime'],
'DestroyTime': self.ItemD.get( 'DestroyTime', 9999999999999 ),
'GetTime': int( time() * 1000 ),
'AuthPubKey': self.ItemD['AuthPubKey'],
'status': self.status,
} )
if hasattr( self, 'RemoteLabels' ):
kwds['RemoteLabels'] = self.RemoteLabels
if hasattr( self, 'RemoteEval' ):
kwds['RemoteEval'] = self.RemoteEval
else:
kwds['GetTime'] += randint( 100000, 300000 ) #the GetTime for local created shouldn't be exact.
if SaveArticle( **kwds ):
if self.IsRoot():
Topic.New( self )
else:
Topic.AddAtcl( self )
UpdateTopic( self.ItemD['RootID'], **{
'LastAuthName': self.ItemD.get( 'NickName' ),
'LastTime': kwds['GetTime'],
} )
def Check( self ):
"do not check destroy time here"
try:
assert self.ItemD.get( 'IDHashType', 'sha1' ).lower() == 'sha1'
assert self.id == sha1( self.ItemStr ).hexdigest()
assert self.ItemD.get( 'SignHashType', 'md5' ).lower() == 'md5'
if 'ProtoID' in self.ItemD: #assert proto.user is self.user
Proto = self.Get( self.ItemD['ProtoID'] )
assert Proto.ItemD['AuthPubKey'] == self.ItemD['AuthPubKey']
content = self.content.encode( 'utf8' ) if self.ItemD['Type'] == Article.NORMAL else self.ItemD['ParentID']
AuthPubKey = self.ItemD.get( 'AuthPubKey' )
author = OtherUser( AuthPubKey )
assert author.Verify( content, self.ItemD['Sign'] )
return True
except:
logging.error( traceback.format_exc())
def Show( self, **addi ):
"show to ui"
ShowData = {
'atclId': self.id,
'content': self.content,
'status': self.status,
'LabelStr': self.LabelStr,
}
ShowData.update( addi )
ShowData.update( self.ItemD )
return ShowData
def IsRoot( self ):
""
return not self.ItemD.get( 'RootID' )
def IsPassed( self ):
""
return self.status >= Article.PASS
def SortType( self ):
""
return bool( self.ItemD.get( 'RootID' )) + bool( self.ItemD.get( 'ProtoID' ))
def GetLabels( self ):
""
return self.ItemD.get( 'Labels' ).replace( u',', ',' ).split( ',' )
def SetStatus( self, status ):
""
self.status = status
SetAtclStatus( self.id, status )
self.Uncache()
def Uncache( self ):
"del from cache."
if self.id in self.LiveD:
self.LiveD.pop( self.id )
RootId = self.ItemD.get( 'RootID', self.id )
Topic.Uncache( RootId )
@classmethod
def New( cls, user, Type = 0, content = '', life = 0, **kwds ):
"create from local"
assert Type == 0 #remove like to reduce leaf number.
#SignFunc = ( lambda s: md5( s ).hexdigest() ) if user is None else user.Sign
itemD = {} if user is None else user.InitItem() #get AuthPubKeyType, AuthPubKey, NickName
itemD['Type'] = Type
# if Type == Article.NORMAL:
assert content
itemD['Sign'] = user.Sign( content.encode( 'utf-8' ) )
# elif Type == Article.LIKE:
# content = '' #LIKE has no content, life, ProtoID, Labels
# life = 0
# for k in kwds.viewkeys() & { 'ProtoID', 'Labels' }:
# del kwds[k]
# itemD['Sign'] = user.Sign( kwds['ParentID'] )
# else:
# raise #for chat here
itemD['CreateTime'] = int( time() * 1000 )
if life > 0:
itemD['DestroyTime'] = itemD['CreateTime'] + life
itemD['SignHashType'] = 'md5'
itemD['IDHashType'] = 'sha1'
for k in kwds.viewkeys() & { 'RootID', 'ParentID', 'ProtoID', 'Labels' }:
itemD[k] = kwds[k] #all texts should be encoded to utf-8
if k == 'ParentID' and 'DestroyTime' in itemD:
Parent = cls.Get( kwds[k] )
itemD['DestroyTime'] = Parent.ItemD.get( 'DestroyTime', 9999999999999 ) #only root has destroytime
Atcl = cls( None, itemD = itemD, content = content )
if Atcl.IsRoot():
Topic.New( Atcl )
return Atcl
def SetLastNodeInfo( self, info ):
""
self.RemoteLabels = info.get( 'NodeLabels', '' )
self.RemoteEval = info.get( 'NodeEval', 1 )
@classmethod
def Search( cls, kWord, before ):
""
if re.match( r'[0-9a-f]{20}', kWord ):
try:
data = GetOneArticle( 'id', 'items', 'content', 'status', 'root', 'GetTime', id = kWord )
return [cls( Id = data[0], itemStr = data[1], content = data[2],
status = data[3] ).Show( rootId = data[4], GetTime = data[5] )]
except:
pass
return [cls( Id = data[0], itemStr = data[1], content = data[2],
status = data[3], labelStr = '' ).Show( rootId = data[4], GetTime = data[5] )
for data in SearchAtcl( kWord, before, 10 )]
@classmethod
def Receive( cls, atclData ):
"get from other nodes"
Atcl = cls( atclData['Id'], itemStr = atclData['Items'], content = atclData['Content'] )
Atcl.SetLastNodeInfo( atclData )
return Atcl
@classmethod
def Get( cls, atclId ):
""
if atclId in cls.LiveD:
return cls.LiveD[atclId]
aData = GetOneArticle( 'items', 'content', id = atclId )
if aData is not None:
return cls( Id = atclId, itemStr = aData[0], content = aData[1] )
@classmethod
def ShowByUser( cls, uPubK, before ):
"timeline for ui"
return [cls( Id = data[0], itemStr = data[1], content = data[2],
status = data[4], labelStr = data[3] or '' ).Show( rootId = data[5], GetTime = data[6] )
for data in GetAtclByUserToShow( uPubK, before, 10 )]
@classmethod
def GetByUser( cls, uPubK, From, To, exist ):
"timeline"
if SeekSelfUser or not SelfUser.IsSelf( uPubK ): #set SeekSelfUser to False for high secrety
for Id, items, content in GetAtclByUser( uPubK, From, To, exist ):
yield cls( Id = Id, itemStr = items, content = content )
@classmethod
def Cache( cls, d ): #d = { atclId: ( itemStr, content ) }
""
for atclId, ( itemStr, content, status, RmtLabels, RmtEval, LabelStr ) in d.iteritems():
if atclId not in cls.LiveD:
cls.LiveD[atclId] = cls( atclId, itemStr = itemStr, content = content,
status = status, labelStr = LabelStr )
class TreeStruct( object ):
""
def __init__( self, nodeId, parent = None, children = ()):
""
self.NodeId = nodeId
self.Parent = parent
self.Children = set( children )
def Add( self, child ):
""
self.Children.add( child )
def IsLeaf( self ):
""
return not self.Children
def IsRoot( self ):
""
return not self.Parent
def __repr__( self ):
""
return '<%s: %s, %s>' % ( self.NodeId, self.Parent, repr( self.Children ))
@classmethod
def GetAll( cls, d, rootK ):
""
for ck in d[rootK].Children:
yield ck
for k in cls.GetAll( d, ck ):
yield k
@classmethod
def GetLeaves( cls, d, rootK ):
""
children = d[rootK].Children
if children:
for ck in children:
for k in cls.GetLeaves( d, ck ):
yield k
else:
yield rootK
class Topic( object ):
""
LiveD = {}
def __init__( self, root ):
""
assert root.ItemD.get( 'Type' ) == 0
assert root.ItemD.get( 'Labels' ) > '' #every tree must has labels
self.Root = root
self.AtclD = { root.id: root }
self.StructD = None
self.Fallens = []
self.SetSnapShot()
def Save( self, **kwds ):
""
try:
SaveTopic( **kwds )
SaveTopicLabels( self.Root.id, self.Root.GetLabels())
except:
logging.error( traceback.format_exc())
def GetInfo( self ):
""
return {
"TreeId": self.Root.id,
"Length": len( self ),
"SnapShot": self.SnapShot,
}
def SetSnapShot( self ):
""
IdStr = ''.join( sorted( self.AtclD.keys()))
self.SnapShot = sha1( IdStr ).hexdigest()
# def GetLeaves( self ):
# "get the leaf nodes in the tree."
# return self.AtclD.viewkeys() - { atcl.ItemD.get( 'ParentID', '' ) for atcl in self.AtclD.itervalues() }
def Instruct( self, force = False ):
"build tree structure"
if force or self.StructD is None:
NodeD = self.AtclD
StructD = {}
for atclId, atcl in NodeD.iteritems():
ParentId = atcl.ItemD.get( 'ParentID', '' )
if atclId not in StructD:
StructD[atclId] = TreeStruct( atclId, ParentId )
while ParentId not in StructD:
Parent = NodeD.get( ParentId )
GrandId = Parent.ItemD.get( 'ParentID', '' ) if Parent else None
StructD[ParentId] = TreeStruct( ParentId, GrandId )
StructD[ParentId].Add( atclId )
if GrandId is None:
break
atclId, ParentId = ParentId, GrandId
else:
StructD[ParentId].Add( atclId )
self.Fallens = []
SubRoots = { atclId for atclId, node in StructD.items() if node.Parent is None } - { '', self.Root.id }
for sr in SubRoots:
self.Fallens.extend( TreeStruct.GetAll( StructD, sr ))
self.StructD = StructD #there may be multi articles in struct.root when editing the root. put roots in struct.children.
return self.StructD
def GetUpperIds( self, leafId ):
""
if self.StructD is None:
self.Instruct()
while leafId:
yield leafId
leafId = self.StructD[leafId].Parent
def ChkByLeaves( self, leaves, rmtFallens ):
"check remote leaves and return the more articles"
self.Instruct()
LocalLinked = set( TreeStruct.GetAll( self.StructD, '' ))
RemoteNodes = set()
AskBack = bool( leaves - LocalLinked ) #remote is more than local
leaves &= LocalLinked
for leafId in leaves:
for leaf in self.GetUpperIds( leafId ):
if leaf in RemoteNodes:
break
RemoteNodes.add( leaf )
ReturnNodes = LocalLinked - RemoteNodes - rmtFallens
return [self.AtclD[Id] for Id in ReturnNodes], AskBack
def __len__( self ):
""
return len( self.AtclD )
@classmethod
def New( cls, atcl ):
""
if atcl.id not in cls.LiveD:
cls.LiveD[atcl.id] = cls( atcl )
cls.LiveD[atcl.id].Save( root = atcl.id, title = atcl.content.split( '\n' )[0][:TitleLength],
num = 1, status = int( AutoNode ), labels = atcl.ItemD.get( 'Labels', '' ),
FirstAuthName = atcl.ItemD.get( 'NickName', '' ), LastAuthName = atcl.ItemD.get( 'NickName', '' ),
FirstTime = atcl.ItemD.get( 'CreateTime' ), LastTime = atcl.ItemD.get( 'CreateTime' ), )
@classmethod
def Filter( cls, condi ):
""
RootIds = GetRootIds( **condi )
l = len( RootIds )
for i in range( l - MaxOfferRootNum ):
RootIds.pop( randint( 0, l - i - 1 ))
for topic in cls.GetMulti( *RootIds ):
yield topic
@classmethod
def Uncache( cls, rootId ):
""
if rootId in cls.LiveD:
cls.LiveD.pop( rootId )
@classmethod
def ListPage( cls, label, before, sortCol ):
""
return GetTopicRows( label, before, sortCol, TopicNumPerPage )
@classmethod
def ListById( cls, rootId ):
""
return GetTopicRowById( rootId ) #root, title, labels, status, num, FirstAuthName, FirstTime, LastAuthName, LastTime
@classmethod
def EditLabel( cls, rootId, label, act ):
""
if rootId in cls.LiveD:
cls.LiveD.pop( rootId )
return SetLabel( rootId, label, act )
@classmethod
def ShowAll( cls, rootId ):
"show all articles in this topic."
for topic in cls.GetMulti( rootId ):
return [rootId, { Id: atcl.Show() for Id, atcl in topic.AtclD.iteritems() }]
@classmethod
def AddAtcl( cls, atcl ):
""
topic = cls.LiveD.get( atcl.ItemD['RootID'] )
if topic is not None:
topic.AtclD[atcl.id] = atcl
topic.SetSnapShot()
topic.Instruct( True )
@classmethod
def GetMulti( cls, *rootIds ):
"a generator to get multi topic objs from rootIds"
rootSet = set( rootIds )
cacheSet = cls.LiveD.viewkeys()
for k in rootSet & cacheSet:
yield cls.LiveD[k]
rootSet -= cacheSet
if rootSet:
TreeD = GetTreeAtcls( *rootSet )
for k, atclD in TreeD.iteritems():
if k not in atclD:
continue
Article.Cache( atclD )
topic = cls( Article.Get( k ))
for ak in atclD:
topic.AtclD.setdefault( ak, Article.Get( ak ))
topic.SetSnapShot()
cls.LiveD[k] = topic
yield topic
@classmethod
def Compare( cls, treeInfo ): #treeInfo = { TreeId:..., Length:..., SnapShot:... }
""
TreeId = treeInfo['TreeId']
for topic in cls.GetMulti( TreeId ):
if treeInfo['SnapShot'] == topic.SnapShot:
return
if len( topic ) >= 0.5 * treeInfo['Length']:
StructD = topic.Instruct()
return {
'TreeId': TreeId,
'Mode': 'leaf',
'Leaves': list( TreeStruct.GetLeaves( StructD, '' )),
'Fallens': topic.Fallens,
}
else:
return { 'TreeId': TreeId, 'Mode': 'all' }
@classmethod
def GetReqAtcls( cls, treeReq, askBackFunc ): #treeReq = { TreeId:..., Mode:..., Leaves:... }
""
for topic in cls.GetMulti( treeReq['TreeId'] ):
if treeReq['Mode'] == 'all':
return topic.AtclD.values()
if treeReq['Mode'] == 'leaf':
Atcls, AskBack = topic.ChkByLeaves( set( treeReq['Leaves'] ), set( treeReq.get( 'Fallens', [] )))
# AskBack and askBackFunc( { do not ask back to avoid request storm
# 'TreeId': treeReq['TreeId'],
# 'Mode': 'leaf',
# 'Leaves': list( TreeStruct.GetLeaves( topic.Instruct(), '' )),
# 'Fallens': topic.Fallens,
# } )
return Atcls
if treeReq['Mode'] in ( "higher", "lower", "relative" ):
pass
return []
@classmethod
def Patch( cls ):
"check the root articles without topic data."
SetAtclsWithoutTopic()
if __name__ == '__main__':
for topic in Topic.GetMulti( '52fbe82c1e110266da298dc3ae0ad2345bca12fe' ):
print topic.Instruct( force = True )