Skip to content
Snippets Groups Projects
Commit 9b69600b authored by Vincent Texier's avatar Vincent Texier
Browse files

Fix problem with deepcopy on Graph class

parent 6d9b0f5b
No related branches found
No related tags found
No related merge requests found
...@@ -6,17 +6,35 @@ from cutecoin.core.person import Person ...@@ -6,17 +6,35 @@ from cutecoin.core.person import Person
from cutecoin.gui.views.wot import NODE_STATUS_HIGHLIGHTED, NODE_STATUS_OUT, ARC_STATUS_STRONG, ARC_STATUS_WEAK from cutecoin.gui.views.wot import NODE_STATUS_HIGHLIGHTED, NODE_STATUS_OUT, ARC_STATUS_STRONG, ARC_STATUS_WEAK
class Graph(dict): class Graph(object):
def __init__(self, community): def __init__(self, community, graph=None):
""" """
Init Graph instance Init Graph instance
:param cutecoin.core.community.Community community: :param cutecoin.core.community.Community community:
:return: :return:
""" """
self.community = community self.community = community
self.signature_validity = self.community.get_parameters()['sigValidity'] self.signature_validity = self.community.get_parameters()['sigValidity']
# arc considered strong during 75% of signature validity time # arc considered strong during 75% of signature validity time
self.ARC_STATUS_STRONG_time = int(self.signature_validity * 0.75) self.ARC_STATUS_STRONG_time = int(self.signature_validity * 0.75)
# graph empty if None parameter
self._graph = graph or (dict() and (graph is None))
def set(self, graph):
"""
Set the graph from dict
:param dict graph:
:return:
"""
self._graph = graph
def get(self):
"""
Return the graph dict
:return:
"""
return self._graph
def get_shortest_path_between_members(self, from_person, to_person): def get_shortest_path_between_members(self, from_person, to_person):
""" """
...@@ -26,22 +44,21 @@ class Graph(dict): ...@@ -26,22 +44,21 @@ class Graph(dict):
:return: :return:
""" """
path = list() path = list()
graph_tmp = copy.deepcopy(self)
logging.debug("path between %s to %s..." % (from_person.name, to_person.name)) logging.debug("path between %s to %s..." % (from_person.name, to_person.name))
if from_person.pubkey not in graph_tmp.keys(): if from_person.pubkey not in self._graph.keys():
graph_tmp.add_person(from_person) self.add_person(from_person)
certifier_list = from_person.certifiers_of(self.community) certifier_list = from_person.certifiers_of(self.community)
graph_tmp.add_certifier_list(certifier_list, from_person, to_person) self.add_certifier_list(certifier_list, from_person, to_person)
certified_list = from_person.certified_by(self.community) certified_list = from_person.certified_by(self.community)
graph_tmp.add_certified_list(certified_list, from_person, to_person) self.add_certified_list(certified_list, from_person, to_person)
if to_person.pubkey not in graph_tmp.keys(): if to_person.pubkey not in self._graph.keys():
# recursively feed graph searching for account node... # recursively feed graph searching for account node...
graph_tmp.explore_to_find_member(to_person, graph_tmp[from_person.pubkey]['connected'], list()) self.explore_to_find_member(to_person, self._graph[from_person.pubkey]['connected'], list())
if len(graph_tmp[from_person.pubkey]['connected']) > 0: if len(self._graph[from_person.pubkey]['connected']) > 0:
# calculate path of nodes between person and to_person # calculate path of nodes between person and to_person
path = graph_tmp.find_shortest_path(graph_tmp[from_person.pubkey], graph_tmp[to_person.pubkey]) path = self.find_shortest_path(self._graph[from_person.pubkey], self._graph[to_person.pubkey])
if path: if path:
logging.debug([node['text'] for node in path]) logging.debug([node['text'] for node in path])
...@@ -63,27 +80,27 @@ class Graph(dict): ...@@ -63,27 +80,27 @@ class Graph(dict):
connected = connected or (list() and (connected is None)) connected = connected or (list() and (connected is None))
done = done or (list() and (done is None)) done = done or (list() and (done is None))
logging.debug("search %s in " % person.name) logging.debug("search %s in " % person.name)
logging.debug([self[pubkey]['text'] for pubkey in connected]) logging.debug([self._graph[pubkey]['text'] for pubkey in connected])
# for each pubkey connected... # for each pubkey connected...
for pubkey in tuple(connected): for pubkey in tuple(connected):
# capture node connected # capture node connected
node = self[pubkey] node = self._graph[pubkey]
if node['id'] in tuple(done): if node['id'] in tuple(done):
continue continue
person_selected = Person(node['text'], node['id']) person_selected = Person(node['text'], node['id'])
certifier_list = person_selected.certifiers_of(self.community) certifier_list = person_selected.certifiers_of(self.community)
self.add_certifier_list(certifier_list, person_selected, person) self.add_certifier_list(certifier_list, person_selected, person)
if person.pubkey in tuple(self.keys()): if person.pubkey in tuple(self._graph.keys()):
return False return False
certified_list = person_selected.certified_by(self.community) certified_list = person_selected.certified_by(self.community)
self.add_certified_list(certified_list, person_selected, person) self.add_certified_list(certified_list, person_selected, person)
if person.pubkey in tuple(self.keys()): if person.pubkey in tuple(self._graph.keys()):
return False return False
if node['id'] not in tuple(done): if node['id'] not in tuple(done):
done.append(node['id']) done.append(node['id'])
if len(done) >= len(self): if len(done) >= len(self._graph):
return True return True
result = self.explore_to_find_member(person, self[person_selected.pubkey]['connected'], done) result = self.explore_to_find_member(person, self._graph[person_selected.pubkey]['connected'], done)
if not result: if not result:
return False return False
...@@ -101,11 +118,11 @@ class Graph(dict): ...@@ -101,11 +118,11 @@ class Graph(dict):
path = path + [start] path = path + [start]
if start['id'] == end['id']: if start['id'] == end['id']:
return path return path
if start['id'] not in self.keys(): if start['id'] not in self._graph.keys():
return None return None
shortest = None shortest = None
for pubkey in tuple(self[start['id']]['connected']): for pubkey in tuple(self._graph[start['id']]['connected']):
node = self[pubkey] node = self._graph[pubkey]
if node not in path: if node not in path:
newpath = self.find_shortest_path(node, end, path) newpath = self.find_shortest_path(node, end, path)
if newpath: if newpath:
...@@ -127,13 +144,13 @@ class Graph(dict): ...@@ -127,13 +144,13 @@ class Graph(dict):
if (time.time() - certifier['cert_time']['medianTime']) > self.signature_validity: if (time.time() - certifier['cert_time']['medianTime']) > self.signature_validity:
continue continue
# new node # new node
if certifier['pubkey'] not in self.keys(): if certifier['pubkey'] not in self._graph.keys():
node_status = 0 node_status = 0
if certifier['pubkey'] == person_account.pubkey: if certifier['pubkey'] == person_account.pubkey:
node_status += NODE_STATUS_HIGHLIGHTED node_status += NODE_STATUS_HIGHLIGHTED
if certifier['isMember'] is False: if certifier['isMember'] is False:
node_status += NODE_STATUS_OUT node_status += NODE_STATUS_OUT
self[certifier['pubkey']] = { self._graph[certifier['pubkey']] = {
'id': certifier['pubkey'], 'id': certifier['pubkey'],
'arcs': list(), 'arcs': list(),
'text': certifier['uid'], 'text': certifier['uid'],
...@@ -143,8 +160,8 @@ class Graph(dict): ...@@ -143,8 +160,8 @@ class Graph(dict):
} }
# keep only the latest certification # keep only the latest certification
if self[certifier['pubkey']]['arcs']: if self._graph[certifier['pubkey']]['arcs']:
if certifier['cert_time']['medianTime'] < self[certifier['pubkey']]['arcs'][0]['cert_time']: if certifier['cert_time']['medianTime'] < self._graph[certifier['pubkey']]['arcs'][0]['cert_time']:
continue continue
# display validity status # display validity status
if (time.time() - certifier['cert_time']['medianTime']) > self.ARC_STATUS_STRONG_time: if (time.time() - certifier['cert_time']['medianTime']) > self.ARC_STATUS_STRONG_time:
...@@ -160,11 +177,11 @@ class Graph(dict): ...@@ -160,11 +177,11 @@ class Graph(dict):
'cert_time': certifier['cert_time']['medianTime'] 'cert_time': certifier['cert_time']['medianTime']
} }
# add arc to certifier # add arc to certifier
self[certifier['pubkey']]['arcs'].append(arc) self._graph[certifier['pubkey']]['arcs'].append(arc)
# if certifier node not in person nodes # if certifier node not in person nodes
if certifier['pubkey'] not in tuple(self[person.pubkey]['connected']): if certifier['pubkey'] not in tuple(self._graph[person.pubkey]['connected']):
# add certifier node to person node # add certifier node to person node
self[person.pubkey]['connected'].append(certifier['pubkey']) self._graph[person.pubkey]['connected'].append(certifier['pubkey'])
def add_certified_list(self, certified_list, person, person_account): def add_certified_list(self, certified_list, person, person_account):
""" """
...@@ -179,13 +196,13 @@ class Graph(dict): ...@@ -179,13 +196,13 @@ class Graph(dict):
# add only valid certification... # add only valid certification...
if (time.time() - certified['cert_time']['medianTime']) > self.signature_validity: if (time.time() - certified['cert_time']['medianTime']) > self.signature_validity:
continue continue
if certified['pubkey'] not in self.keys(): if certified['pubkey'] not in self._graph.keys():
node_status = 0 node_status = 0
if certified['pubkey'] == person_account.pubkey: if certified['pubkey'] == person_account.pubkey:
node_status += NODE_STATUS_HIGHLIGHTED node_status += NODE_STATUS_HIGHLIGHTED
if certified['isMember'] is False: if certified['isMember'] is False:
node_status += NODE_STATUS_OUT node_status += NODE_STATUS_OUT
self[certified['pubkey']] = { self._graph[certified['pubkey']] = {
'id': certified['pubkey'], 'id': certified['pubkey'],
'arcs': list(), 'arcs': list(),
'text': certified['uid'], 'text': certified['uid'],
...@@ -210,23 +227,23 @@ class Graph(dict): ...@@ -210,23 +227,23 @@ class Graph(dict):
# replace old arc if this one is more recent # replace old arc if this one is more recent
new_arc = True new_arc = True
index = 0 index = 0
for a in self[person.pubkey]['arcs']: for a in self._graph[person.pubkey]['arcs']:
# if same arc already exists... # if same arc already exists...
if a['id'] == arc['id']: if a['id'] == arc['id']:
# if arc more recent, dont keep old one... # if arc more recent, dont keep old one...
if arc['cert_time'] >= a['cert_time']: if arc['cert_time'] >= a['cert_time']:
self[person.pubkey]['arcs'][index] = arc self._graph[person.pubkey]['arcs'][index] = arc
new_arc = False new_arc = False
index += 1 index += 1
# if arc not in graph... # if arc not in graph...
if new_arc: if new_arc:
# add arc in graph # add arc in graph
self[person.pubkey]['arcs'].append(arc) self._graph[person.pubkey]['arcs'].append(arc)
# if certified node not in person nodes # if certified node not in person nodes
if certified['pubkey'] not in tuple(self[person.pubkey]['connected']): if certified['pubkey'] not in tuple(self._graph[person.pubkey]['connected']):
# add certified node to person node # add certified node to person node
self[person.pubkey]['connected'].append(certified['pubkey']) self._graph[person.pubkey]['connected'].append(certified['pubkey'])
def add_person(self, person, status=None, arcs=None, connected=None): def add_person(self, person, status=None, arcs=None, connected=None):
""" """
...@@ -241,7 +258,7 @@ class Graph(dict): ...@@ -241,7 +258,7 @@ class Graph(dict):
status = status or (0 and (status is None)) status = status or (0 and (status is None))
arcs = arcs or (list() and (arcs is None)) arcs = arcs or (list() and (arcs is None))
connected = connected or (list() and (connected is None)) connected = connected or (list() and (connected is None))
self[person.pubkey] = { self._graph[person.pubkey] = {
'id': person.pubkey, 'id': person.pubkey,
'arcs': arcs, 'arcs': arcs,
'text': person.name, 'text': person.name,
......
...@@ -111,7 +111,7 @@ class Scene(QGraphicsScene): ...@@ -111,7 +111,7 @@ class Scene(QGraphicsScene):
""" """
draw community graph draw community graph
:param cutecoin.core.graph.Graph graph: graph to draw :param dict graph: graph to draw
""" """
# clear scene # clear scene
self.clear() self.clear()
......
...@@ -84,7 +84,7 @@ class WotTabWidget(QWidget, Ui_WotTabWidget): ...@@ -84,7 +84,7 @@ class WotTabWidget(QWidget, Ui_WotTabWidget):
graph.add_certified_list(certified_list, person, person_account) graph.add_certified_list(certified_list, person, person_account)
# draw graph in qt scene # draw graph in qt scene
self.graphicsView.scene().update_wot(graph) self.graphicsView.scene().update_wot(graph.get())
# if selected member is not the account member... # if selected member is not the account member...
if person.pubkey != person_account.pubkey: if person.pubkey != person_account.pubkey:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment