def filtre(self, tree, line): # Don't use very complex trees because they are usually misanalysed. if len(tree) > 30: return False # Find the 'give' nodes. gives = findall_by_attr(tree[0], value=u'give', name='lemma') # For simplicity's sake, only use trees with one give. if len(gives) > 1: return False # Search 'pobj' nodes below 'give' node pobjs = findall_by_attr(gives[0], value=u'pobj', name='relation', maxlevel=3) if len(pobjs) < 1: return False # Finally, check whether the parent of any of the pobj nodes is 'to'. return (any( [True if x.parent.lemma == u'to' else False for x in pobjs]))
def print_dependency_tree(): root = Node("/") for k, v in missing_dependencies.items(): # insert keys as root children if they are not alredy in tree if len(findall_by_attr(root, k)) == 0: Node(k, parent=root) for child in v: # get parent existing_parent = findall_by_attr(root, k) # add child Node(child, parent=existing_parent[0]) # get all childs with same name existing_child = findall_by_attr(root, child) # there never should be more than 2 of them if len(existing_child) > 1: # assign new parent to the older child (one with whole dependency tree) existing_child[0].parent = existing_parent[0] # remove the other duplicate child existing_child[1].parent = None for pre, _, node in RenderTree(root): print(f"{pre}{node.name}") most_common = Counter({k: len(v) for k, v in missing_dependencies.items() }).most_common(10) print( "Top 10 of blockers (match_failed contains packages that could not be parsed):", file=sys.stderr) for pkg, count in most_common: print(pkg + ": " + str(count), file=sys.stderr)
def test_findall_by_attr(): f = Node("f") b = Node("b", parent=f) Node("a", parent=b) d = Node("d", parent=b) Node("c", parent=d) Node("e", parent=d) eq_(findall_by_attr(f, "d"), (d, )) with assert_raises(CountError, ("Expecting at least 1 elements, but found 0.")): findall_by_attr(f, "z", mincount=1)
def get_gate(self, gate_id, gate_path=None): """ Retrieve a gate instance from its gate ID. reference gates in their parent gating strategy. :param gate_id: text string of a gate ID :param gate_path: complete list of gate IDs for unique set of gate ancestors. Required if gate_id is ambiguous :return: Subclass of a Gate object :raises KeyError: if gate ID is not found in gating strategy """ # It's not safe to just look at the gates dictionary as # QuadrantGate IDs cannot be parents themselves, only their component # Quadrant IDs can be parents. node_matches = anytree.findall_by_attr(self._gate_tree, gate_id) node_match_count = len(node_matches) if node_match_count == 1: node = node_matches[0] elif node_match_count > 1: # need to match on full gate path if gate_path is None: raise ValueError( "Found multiple gates with ID %s. Provide full 'gate_path' to disambiguate." % gate_id) gate_matches = [] gate_path_length = len(gate_path) for n in node_matches: if len(n.ancestors) != gate_path_length: continue ancestor_matches = [ a.name for a in n.ancestors if a.name in gate_path ] if ancestor_matches == gate_path: gate_matches.append(n) if len(gate_matches) == 1: node = gate_matches[0] elif len(gate_matches) > 1: raise ValueError( "Report as bug: Found multiple gates with ID %s and given gate path." % gate_id) else: node = None else: node = None if node is None: # may be in a Quadrant gate for d in self._gate_tree.descendants: if isinstance(d.gate, fk_gates.QuadrantGate): if gate_id in d.gate.quadrants: node = d continue if node is None: raise ValueError("Gate ID %s was not found in gating strategy" % gate_id) return node.gate
def csttoast(cst): # from anytree import Node, RenderTree from anytree import findall_by_attr, RenderTree Par = findall_by_attr(cst, '(', 'val') + findall_by_attr(cst, ')', 'val') for i in range(len(Par)): Par[i].parent = None # if cst.name[-3:]='Com': if cst.name[-4:] == 'Aexp': if len(cst.children) == 1: cst = cst.children[0] else: i = 0 while i <= (len(cst.children) - 1): #print(root.children[i].name[-1:]) #Recursively call CST parser for additional expressions in tree if cst.children[i].kw == 'RESERVED': if i == 0: cst.children[i].children = cst.children[1:] cst = cst.children[i] if cst.children[i].kw == 'INT' or cst.children[ i].kw == 'FLOAT' or cst.children[i].kw == 'ID': pass if cst.children[i].name[-4:] == 'Aexp': if i == 0: cst.children = [ csttoast(cst.children[i]), cst.children[i + 1] ] if i == 1: cst.children = [ cst.children[i - 1], csttoast(cst.children[i]) ] i = i + 1 # print('Exit Function') return cst
def _find(self, template, reference_name): while template.parent: result = findall_by_attr(template.parent, reference_name) if result: return result[0] template = template.parent raise RuntimeError( 'Unable to find referenced template "' + reference_name + '".' )
def p_mms_tree(ptype, packet, debug): global i_invokeid raw_value = packet.pres.fully_encoded_data.raw_value bytesraw = bytes.fromhex(raw_value) root = extractor.extract_mms_structur(bytesraw, len(bytesraw)) if debug: print_tree(root) listblatt = findall_by_attr(root, 'blatt') if len(listblatt) < 5: raise ValueError('at least 1 Data leaf Element needet') presentation_context_identifier = listblatt[ 0] # 1 (mms-abstract-syntax-version1(1)) invokeid = listblatt[1] domainid = listblatt[2] itemid = listblatt[3] for subvalues in listblatt[4:]: change_value_node(subvalues) if i_invokeid % 32767 == 0: i_invokeid = 128 i_invokeid += 1 invokeid.payload = (i_invokeid).to_bytes(2, byteorder='big') rebuild_tree(invokeid) #domainid.payload.decode('utf-8') if random.random() < 0.01: #1% chance mutated_domainId = mms.mutate_mms_domainId( domainid.payload.decode('utf-8')) if not mutated_domainId is None: domainid.payload = mutated_domainId domainid.ilength = len(mutated_domainId) domainid.blength = (len(mutated_domainId)).to_bytes( 1, byteorder='big') #rebuild_tree(domainid) if random.random() < 0.05: #1% chance mutated_itemid = mms.mutate_mms_itemid(itemid.payload.decode('utf-8')) itemid.payload = mutated_itemid itemid.ilength = len(mutated_itemid) itemid.blength = (len(itemid.payload)).to_bytes(1, byteorder='big') #komentar hier einfügen mms_valid_types = [ b'\x80', b'\x81', b'\x83', b'\x84', b'\x85' b'\x86', b'\x87', b'\x88,' b'\x89', b'\x8a', b'\x91', b'\xa0', b'\x1a', b'\xa5', b'\x02' ] #if random.random() < 0.01: #1% chance # itemid.mmstype = random.choice(mms_valid_types) #if random.random() < 0.02: #1% chance # lenght = random.randint(0,100) # itemid.ilength = lenght # itemid.blength = (lenght).to_bytes(1, byteorder='big') rebuild_tree(itemid) if debug: print_tree(root) return root.payload
def add_participant(tree_root, participant, changed_root_keys=None): empty_node = findall_by_attr(tree_root, "empty")[0] # participant.add_topic(topic) # include code for user-permissions # or better move this to top new_leaf_node = LeafNode(empty_node.leaf_node.node_id, participant) added_participant = Node(participant.participant_id, parent=empty_node.parent, leaf_node=new_leaf_node) # dis-allocate the old empty node after attaching the new one to the tree empty_node.parent = None empty_node.leaf_node = None # find ancestors of the added participant and change their keys ancestor_list = added_participant.ancestors for ancestor in ancestor_list: if ancestor.is_root and changed_root_keys is not None: ancestor.tree_node.root_node_keys = changed_root_keys.copy() else: ancestor.tree_node.reset_key() # change the keys of root node here # code to add details about the messages to be sent # first construct messages for participant and its siblings message_details_dict_list = [] # adding message for the newly added participant to be decided based on other implementations # todo """message_detail = {"message_name": str(added_participant.parent.tree_node.node_id) + "/" + str(added_participant.leaf_node.node_id), "encryption_key": added_participant.leaf_node.participant.pairwise_key, "changed_parent_key": added_participant.parent.tree_node.node_key} message_details_dict_list.append(message_detail)""" siblings = added_participant.siblings for sibling in siblings: if sibling.leaf_node.participant is not None: message_detail = {# "message_name": str(sibling.parent.tree_node.node_id) + "/" + str(sibling.leaf_node.node_id), "message_name": str(sibling.parent.tree_node.node_id) + "/" + str(sibling.leaf_node.participant.participant_id) + "__changeParent__" + str(sibling.parent.tree_node.node_id), "encryption_key": sibling.leaf_node.participant.pairwise_key, "changed_parent_key": sibling.parent.tree_node.node_key} message_details_dict_list.append(message_detail) # construct messages for ancestors and their siblings for ancestor in range(len(ancestor_list)-2, -1, -1): children = ancestor_list[ancestor].children for child in children: message_detail = { "message_name": str(child.parent.tree_node.node_id) + "/" + str(child.tree_node.node_id) + "__changeParent__" + str(child.parent.tree_node.node_id), "encryption_key": child.tree_node.node_key} if child.parent.is_root and changed_root_keys is not None: message_detail["changed_parent_key"] = child.parent.tree_node.root_node_keys else: message_detail["changed_parent_key"] = child.parent.tree_node.node_key # "changed_parent_key": child.parent.tree_node.node_key} # if last i.e. root node then encryption keys is the list of changed pub-sub keys # add that condition for the last one. when ancestor = 0 basically. message_details_dict_list.append(message_detail) return tree_root, added_participant, message_details_dict_list
def tree(self, selected_refs, synonyms, hyponyms): #Adding lists to class: self.selected_refs = selected_refs self.synonyms = synonyms self.hyponyms = hyponyms participants = Node('participants') ##hyponyms for hyp in self.hyponyms: if not findall_by_attr(participants, hyp): hypo = Node(hyp, parent=participants) for hyper in self.hyponyms[hyp]: hyper = Node(hyper, parent=hypo) else: for hyper in self.hyponyms[hyp]: hypo = find_by_attr(participants, hyp) hyper = Node(hyper, parent=hypo) ##synonyms for syn in self.synonyms: overlap = findall_by_attr(participants, syn) if overlap: #The synonym already exists in the tree for n in overlap: syno = find_by_attr(n, syn) for s in self.synonyms[syn]: s0 = Node(s, parent=syno) #Creating node s1 = Node(syno.name, parent=s0) #Creating additional node for doubling and reversing the edge. else: #If the synonym does not exist in the tree syno = Node(syn, parent=participants) #Creating node for s in self.synonyms[syn]: s0 = Node(s, parent=syno) #Creating synonym s1 = Node(syno.name, parent=s0) #Reverse node ##remaining participants for n in self.selected_refs: if not findall_by_attr(participants, n): node = Node(n, parent=participants) self.tree = participants return participants
def get_message_names_topic(self, group): message_names_to_sub = [] # add messages for ancestors participant_node = findall_by_attr(group.root_tree, str(self.participant_id))[0] message_name = str(participant_node.parent.tree_node.node_id) + "/" + str(participant_node.leaf_node.node_id) message_names_to_sub.append(message_name) ancestors = participant_node.ancestors for ancestor in range(len(ancestors)-1, 0, -1): message_name = str(ancestors[ancestor].parent.tree_node.node_id) + "/" + str( ancestors[ancestor].tree_node.node_id) message_names_to_sub.append(message_name) return message_names_to_sub
def delete_participant(tree_root, participant, changed_root_keys=None): # find the node participant_to_be_removed = findall_by_attr(tree_root, participant.participant_id)[0] # find all ancestors of this participant and change keys ancestor_list = participant_to_be_removed.ancestors for ancestor in ancestor_list: if ancestor.is_root and changed_root_keys is not None: ancestor.tree_node.root_node_keys = changed_root_keys.copy() else: ancestor.tree_node.reset_key() # code to add details about the messages to be sent # first construct messages for to-be-deleted participant's siblings message_details_dict_list = [] siblings = participant_to_be_removed.siblings for sibling in siblings: if sibling.leaf_node.participant is not None: message_detail = { # "message_name": str(sibling.parent.tree_node.node_id) + "/" + str(sibling.leaf_node.node_id), "message_name": str(sibling.parent.tree_node.node_id) + "/" + str( sibling.leaf_node.participant.participant_id) + "__changeParent__" + str(sibling.parent.tree_node.node_id), "encryption_key": sibling.leaf_node.participant.pairwise_key, "changed_parent_key": sibling.parent.tree_node.node_key} message_details_dict_list.append(message_detail) # construct messages for ancestors and their siblings for ancestor in range(len(ancestor_list) - 2, -1, -1): children = ancestor_list[ancestor].children for child in children: message_detail = { "message_name": str(child.parent.tree_node.node_id) + "/" + str(child.tree_node.node_id) + "__changeParent__" + str(child.parent.tree_node.node_id), "encryption_key": child.tree_node.node_key} if child.parent.is_root and changed_root_keys is not None: message_detail["changed_parent_key"] = child.parent.tree_node.root_node_keys else: message_detail["changed_parent_key"] = child.parent.tree_node.node_key # "changed_parent_key": child.parent.tree_node.node_key} message_details_dict_list.append(message_detail) # delete the participant and add empty node there # moved this to manager class # participant.delete_topic(topic) new_leaf_node = LeafNode(participant_to_be_removed.leaf_node.node_id) new_leaf_node.participant = None new_empty_node = Node("empty", parent=participant_to_be_removed.parent, leaf_node=new_leaf_node) # dis-allocate the participant node after attaching the new empty node to the tree participant_to_be_removed.parent = None participant_to_be_removed.leaf_node = None return tree_root, new_empty_node, message_details_dict_list
def test_enum(): class Animals(IntEnum): Mammal = 1 Cat = 2 Dog = 3 root = Node("ANIMAL") mammal = Node(Animals.Mammal, parent=root) cat = Node(Animals.Cat, parent=mammal) dog = Node(Animals.Dog, parent=mammal) eq_(findall(root), (root, mammal, cat, dog)) eq_(findall_by_attr(root, Animals.Cat), (cat, ))
def getleafcheckers(self, checkerstr): checkerstrs = anytree.findall_by_attr(self.tree, name="name", value=checkerstr.strip()) if not checkerstr: return [] checker = checkerstrs[0] if checker.is_leaf: return [checker.name] descendants = checker.descendants result = [] for check in descendants: if check.is_leaf: result.append(check.name) return result
def _get_gate_node(self, gate_name, gate_path=None): node_matches = anytree.findall_by_attr(self._gate_tree, gate_name) node_match_count = len(node_matches) if node_match_count == 1: node = node_matches[0] elif node_match_count > 1: # need to match on full gate path # TODO: what if QuadrantGate is re-used, does this still work for that case if gate_path is None: raise ValueError( "Found multiple gates with name %s. Provide full 'gate_path' to disambiguate." % gate_name) gate_matches = [] gate_path_length = len(gate_path) for n in node_matches: if len(n.ancestors) != gate_path_length: continue ancestor_matches = tuple( (a.name for a in n.ancestors if a.name in gate_path)) if ancestor_matches == gate_path: gate_matches.append(n) if len(gate_matches) == 1: node = gate_matches[0] elif len(gate_matches) > 1: raise ValueError( "Report as bug: Found multiple gates with ID %s and given gate path." % gate_name) else: node = None else: node = None if node is None: raise ValueError("Gate name %s was not found in gating strategy" % gate_name) return node
def _insert_one_elem(root, path, size): cur = None node = None parent = root for part in path.parts: if cur is None: cur = part else: cur = str(Path(cur, part)) results = findall_by_attr(root, cur, name="identifier") if results: item = results[0] item.size += size parent = item else: if node: parent = node node = TreeNode(name=str(part), identifier=cur, size=size, parent=parent)
def resultRefs(self): result_dict = defaultdict(list) for actor in self.AllRefs: if actor in self.selected_refs: ##Synonyms if actor in [x for v in self.synonyms.values() for x in v]: for syn in self.synonyms: if actor in self.synonyms[syn]: result_dict[syn] += self.AllRefs[actor] ##Remaining actors else: result_dict[actor] += self.AllRefs[actor] for actor in self.AllRefs: ##Hyponyms if actor in self.hyponyms: for hypo in self.hyponyms[actor]: hypers = findall_by_attr(self.tree, hypo) #Finding all hypernyms of the given actor using the tree. for hyper in hypers: #Looping over all possible hypernyms for parent in hyper.ancestors: #Looping over all ancestors and extracting each particular parent. #The hyponym is compared with the hypernym. If equal, they are synonyms and thus ignored. #If un-equal, it is indeed a hyponym and the references of the hypernym is transferred to #the hyponym. if parent.name != 'participants' and hypo != parent.name and parent.name not in [x for v in self.synonyms.values() for x in v]: #If the references are not already stored for the given actor, they are added to the dict. if not result_dict[parent.name][0] in result_dict[hypo]: result_dict[hypo] += result_dict[parent.name] self.result_dict = result_dict return result_dict
def tree(self, mobile, iiuv): rc = current_app.redis_cli # 获取新用户的id member_id = f"SELECT id FROM {MEMBERS_TABLE} WHERE mobile='{mobile}'" member = MysqlSearch().get_one(member_id) # 获取父级用户id parent_id = f"SELECT id FROM {MEMBERS_TABLE} WHERE my_code='{iiuv}'" parent = MysqlSearch().get_one(parent_id) if iiuv is None: # 设置当前用户为顶级节点 parent_id = Node(member['id']) child_id = Node(None) exporter = DictExporter() data = exporter.export(parent_id) rc.hsetnx(f"drp_relation_member_{member['id']}", 0, json.dumps(data)) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id,levels) VALUE ('{member['id']}','{member['id']}' ,1)" MysqlWrite().write(sql_iiuv) return True # TODO 查询当前用户的parent_id是否顶级节点 try: root_data = rc.hget(f"drp_relation_member_{parent['id']}", 0) except Exception as e: rc.lpush('error_not_parent_id', member['id']) return False if root_data: try: json_data = json.loads(root_data) importer = DictImporter() root = importer.import_(json_data) level_2 = Node(member['id'], parent=root) exporter = DictExporter() data = exporter.export(root) rc.hset(f"drp_relation_member_{parent['id']}", 0, json.dumps(data)) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id) VALUE ('{member['id']}','{parent['id']}')" MysqlWrite().write(sql_iiuv) # 更新数据库levels字段数据 sql = f"UPDATE {ET_MEMBER_RELATIONS} SET levels=2,parent_id='{parent['id']}',top_parent_id='{parent['id']}' WHERE member_id='{member['id']}'" MysqlWrite().write(sql) # 判断当前用户是否已经关联了父级,如关联了就删除顶级用户树 gl = MysqlSearch().get_one( f"SELECT parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if gl['parent_id'] != member['id']: # 删除树 rc.delete(f"drp_relation_member_{member['id']}") # 删除缓存 rc.delete(f"user_apprentice_:{member['id']}") rc.delete(f"user_apprentice_:{parent['id']}") return True except Exception as e: current_app.logger.error(e) return False else: # TODO 如果当前parent_id不是顶级节点,查询当前parent_id的上级并进入树结构查询 top_parent = MysqlSearch().get_one( f"SELECT top_parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent['id']}'" ) try: root_data = rc.hget( f"drp_relation_member_{top_parent['top_parent_id']}", 0) except Exception as e: json_data = { 'member_id': member['id'], 'parent_id': parent['id'] } rc.lpush('error_not_top_parent_id', json.dumps(json_data)) if root_data: json_data = json.loads(root_data) importer = DictImporter() root = importer.import_(json_data) level_1 = findall_by_attr(root, top_parent['top_parent_id'])[0] level_2 = findall_by_attr(root, parent['id'])[0] level_3 = Node(member['id'], parent=level_2) exporter = DictExporter() data = exporter.export(root) rc.hset(f"drp_relation_member_{top_parent['top_parent_id']}", 0, json.dumps(data)) # 查询当前parent的等级 lv = MysqlSearch().get_one( f"SELECT levels FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent['id']}'" ) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id) VALUE ('{member['id']}','{parent['id']}')" MysqlWrite().write(sql_iiuv) # 更新数据库levels字段数据 sql = f"UPDATE {ET_MEMBER_RELATIONS} SET levels='{lv['levels'] + 1}',parent_id='{parent['id']}',top_parent_id='{top_parent['top_parent_id']}' WHERE member_id='{member['id']}'" MysqlWrite().write(sql) # 判断当前用户是否已经关联了父级,如关联了就删除顶级用户树 gl = MysqlSearch().get_one( f"SELECT parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if gl['parent_id'] != member['id']: # 删除树 rc.delete(f"drp_relation_member_{member['id']}") # 删除缓存 rc.delete(f"user_apprentice_:{member['id']}") rc.delete(f"user_apprentice_:{parent['id']}") return True # todo 除了第一第二步这里全跑 # class MemberRelationTreeCache(object): # def tree(self, member_id, parent_id): # rc = current_app.redis_cli # # 获取新用户的id # # TODO 查询当前用户的parent_id是否顶级节点 # try: # root_data = rc.hget(f"drp_relation_member_{parent_id}", 0) # except Exception as e: # rc.lpush('error_not_parent_id_2', member_id) # return False # if root_data: # try: # json_data = json.loads(root_data) # importer = DictImporter() # root = importer.import_(json_data) # level_2 = Node(member_id, parent=root) # exporter = DictExporter() # data = exporter.export(root) # rc.hset(f"drp_relation_member_{parent_id}", 0, json.dumps(data)) # # todo 查询当前用户关系是否入库 # fx = MysqlSearch().get_one( # f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member_id}'") # if not fx: # # 新增当前用户分销关系 # sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id) VALUE ('{member_id}','{parent_id}')" # MysqlWrite().write(sql_iiuv) # # 更新数据库levels字段数据 # sql = f"UPDATE {ET_MEMBER_RELATIONS} SET levels=2,parent_id='{parent_id}',top_parent_id='{parent_id}' WHERE member_id='{member_id}'" # MysqlWrite().write(sql) # # 判断当前用户是否已经关联了父级,如关联了就删除顶级用户树 # gl = MysqlSearch().get_one( # f"SELECT parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member_id}'") # if gl['parent_id'] != member_id: # # 删除树 # rc.delete(f"drp_relation_member_{member_id}") # # 删除缓存 # rc.delete(f"user_apprentice_:{member_id}") # rc.delete(f"user_apprentice_:{parent_id}") # return True # except Exception as e: # current_app.logger.error(e) # return False # else: # # TODO 如果当前parent_id不是顶级节点,查询当前parent_id的上级并进入树结构查询 # top_parent = MysqlSearch().get_one(f"SELECT top_parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent_id}'") # try: # root_data = rc.hget(f"drp_relation_member_{top_parent['top_parent_id']}", 0) # except Exception as e: # json_data = { # 'member_id': member_id, # 'parent_id': parent_id # } # rc.lpush('error_not_top_parent_id_33', json.dumps(json_data)) # return False # if root_data: # json_data = json.loads(root_data) # importer = DictImporter() # root = importer.import_(json_data) # level_1 = findall_by_attr(root, top_parent['top_parent_id'])[0] # level_2 = findall_by_attr(root, parent_id)[0] # level_3 = Node(member_id,parent=level_2) # exporter = DictExporter() # data = exporter.export(root) # rc.hset(f"drp_relation_member_{top_parent['top_parent_id']}", 0, json.dumps(data)) # # 查询当前parent的等级 # lv = MysqlSearch().get_one(f"SELECT levels FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent_id}'") # # todo 查询当前用户关系是否入库 # fx = MysqlSearch().get_one( # f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member_id}'") # if not fx: # # 新增当前用户分销关系 # sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id) VALUE ('{member_id}','{parent_id}')" # MysqlWrite().write(sql_iiuv) # # 更新数据库levels字段数据 # sql = f"UPDATE {ET_MEMBER_RELATIONS} SET levels='{lv['levels'] + 1}',parent_id='{parent_id}',top_parent_id='{top_parent['top_parent_id']}' WHERE member_id='{member_id}'" # MysqlWrite().write(sql) # # 判断当前用户是否已经关联了父级,如关联了就删除顶级用户树 # gl = MysqlSearch().get_one( # f"SELECT parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member_id}'") # if gl['parent_id'] != member_id: # # 删除树 # rc.delete(f"drp_relation_member_{member_id}") # # 删除缓存 # rc.delete(f"user_apprentice_:{member_id}") # rc.delete(f"user_apprentice_:{parent_id}") # return True # class MemberRelationTreeCache(object): # def tree(self, member, iiuv): # rc = current_app.redis_cli # if iiuv is None: # # 设置当前用户为顶级节点 # parent_id = Node(iiuv) # child_id = Node(member, parent=parent_id) # exporter = DictExporter() # data = exporter.export(parent_id) # rc.hsetnx(f"drp_relation_member_{iiuv}", 0, json.dumps(data)) # # todo 查询当前用户关系是否入库 # fx = MysqlSearch().get_one(f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member}'") # if not fx: # # 新增当前用户分销关系 # sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id,levels) VALUE ('{member}','{parent_id}' ,1)" # MysqlWrite().write(sql_iiuv) # return True
def add_client(self, client, permission, initial_topic, initial_topic_anc): # add the participant # then send updated keys to all others -- subscribe specific ancestors # later, send the initial message the participant # initial message = all ancestors and keys and what topics to subscribe to receive updated keys # this way all have updated keys # time.sleep(5) # for syncing result = KeyManager.add_or_delete_participant(self.topic, client, permission, add_participant=True) # print(RenderTree(self.topic.root_tree_pub_sub)) broker_address = "iot.eclipse.org" # use external broker publisher_GCKS = mqtt.Client("P3") # create new instance publisher_GCKS.connect(broker_address) # print("result") # print(result) # sending messages to participants affected by added participant for message in result['add_participant'][0]: update_msg_topic_name = self.topic.topicName + result['add_participant'][1]['tree_type'] + message['message_name'] # print(update_msg_topic_name) # todo -- encrypt here # msg_updated_key = str(message['changed_parent_key'])+" "+str(message['encryption_key']) # encrypting message # if key type is dictionary, then convert to byte-type for enryption if type(message['changed_parent_key']) is dict: # major nacl changes # message_to_bytes = json.dumps(message['changed_parent_key']) message_to_bytes = json.dumps(message['changed_parent_key']).encode('utf-8') else: message_to_bytes = message['changed_parent_key'] encrypted_message = cryptography.encrypt_secret_key(message['encryption_key'], message_to_bytes) publisher_GCKS.publish(update_msg_topic_name, encrypted_message) for tree in result['update_tree']: for message in tree[0]: update_msg_topic_name = self.topic.topicName + tree[1]['tree_type'] + message['message_name'] # todo -- encrypt here # print(update_msg_topic_name) msg_updated_key = str(message['changed_parent_key'])+" "+str(message['encryption_key']) # encrypting message # if key type is dictionary, then convert to byte-type for enryption if type(message['changed_parent_key']) is dict: # nacl change # message_to_bytes = json.dumps(message['changed_parent_key']) message_to_bytes = json.dumps(message['changed_parent_key']).encode('utf-8') else: message_to_bytes = message['changed_parent_key'] encrypted_message = cryptography.encrypt_secret_key(message['encryption_key'], message_to_bytes) publisher_GCKS.publish(update_msg_topic_name, encrypted_message) # initial subscribing topics for the client newly added # get all ancestors of participant1 and publish them encrypting with pairwise keys tree = Node(self.topic.topicName) tree_type_name = '' if permission is 1: tree = copy.deepcopy(self.topic.root_tree_publishers) tree_type_name = 'pub' if permission is 2: tree = copy.deepcopy(self.topic.root_tree_subscribers) tree_type_name = 'sub' if permission is 3: tree = copy.deepcopy(self.topic.root_tree_pub_sub) tree_type_name = 'pub_sub' # recheck ancestor_list = (findall_by_attr(tree, client.participant_id))[0].ancestors # # print(ancestor_list) # make a dictionary/json of tree node names and key possessed by it # since participant would need keys of all its ancestors ancestor_keys = [] topic_to_sub_enc_keys = [] i = 0 while i < len(ancestor_list): if ancestor_list[i].is_root is True: ancestor_keys.append({'name': str(ancestor_list[i].tree_node.node_id), # problem with json byte encoding #'key': ancestor_list[i].tree_node.root_node_keys}) 'key': ancestor_list[i].tree_node.root_node_keys}) else: ancestor_keys.append({'name': str(ancestor_list[i].tree_node.node_id), # nacl change 'key': ancestor_list[i].tree_node.node_key.hex()}) if i != len(ancestor_list) - 1: topic_to_sub_enc_keys.append({'topic_to_sub': self.topic.topicName + tree_type_name + str(ancestor_list[i].tree_node.node_id) + '/' + str(ancestor_list[i + 1].tree_node.node_id) + "__changeParent__" + str(ancestor_list[i].tree_node.node_id ), # 'enc_key': ancestor_list[i + 1].tree_node.node_key}) # problem with json byte encoding # nacl change 'enc_key': ancestor_list[i + 1].tree_node.node_key.hex()}) else: topic_to_sub_enc_keys.append({'topic_to_sub': self.topic.topicName + tree_type_name + str(ancestor_list[i].tree_node.node_id) + '/' + client.participant_id + "__changeParent__" + str(ancestor_list[i].tree_node.node_id), # 'enc_key': client.pairwise_key}) # problem with json byte encoding # nacl change 'enc_key': client.pairwise_key.hex()}) i = i + 1 # print ("topic to sub ") # print (topic_to_sub_enc_keys) # nacl change # mqtt_msg = json.dumps(topic_to_sub_enc_keys) # todo -- encrypt with participant1.pairwise_key mqtt_msg = json.dumps(topic_to_sub_enc_keys).encode('utf-8') # encrypt message # nacl change if type(client.pairwise_key) is str: encrypted_message = cryptography.encrypt_secret_key(bytes.fromhex(client.pairwise_key), mqtt_msg) else: encrypted_message = cryptography.encrypt_secret_key(client.pairwise_key, mqtt_msg) time.sleep(5) # print(encrypted_message) publisher_GCKS.publish(initial_topic, encrypted_message) # nacl change # mqtt_msg = json.dumps(ancestor_keys) # todo -- encrypt with participant1.pairwise_key mqtt_msg = json.dumps(ancestor_keys).encode('utf-8') # nacl change if type(client.pairwise_key) is str: encrypted_message = cryptography.encrypt_secret_key(bytes.fromhex(client.pairwise_key), mqtt_msg) else: encrypted_message = cryptography.encrypt_secret_key(client.pairwise_key, mqtt_msg) time.sleep(5) publisher_GCKS.publish(initial_topic_anc, encrypted_message)
def mineFrequentPattern(dataset, tree, min_support, itemCounter): root = tree print dataset #MENGURUTKAN ITEM DARI PALING DIKIT KARENA AKAN DI BOTTOM UP, INTINYA NGEDAPETIN NODE TERBAWAH print itemCounter itemReverse = [] lengthItems = len(itemCounter) for i in range(1, lengthItems + 1): itemReverse.append(itemCounter.most_common()[-i][0]) #TRACE DARI ITEM PALING BAWAH #DECLARE DICTIONARIES condPattern = {} frequentPattern = [] for item in itemReverse: #MENCARI SEMUA NODE YANG BERATRIBUT ITEM TERSEBUT nodes = findall_by_attr(root, item) #print item #UNTUK SETIAP NODE #DICARI PARENT #PARENT AKAN DISIMPAN DI DALAM ARRAY condPatternParents = [] for node in nodes: parent = str(node) parent = parent.replace("/root/", "") parent = parent.split("'")[1] for i in range(0, node.support): condPatternParents.append(parent) condPattern[item] = condPatternParents #print condPattern for item in itemReverse: print '' print str(item) + " >>>> " + str(condPattern[item]) #MEMASUKKAN SEMUA ITEM UNTUK DIHITUNG itemsInPattern = [] for pattern in condPattern[item]: #print pattern temp_pattern = pattern.split('/') for temp_item in temp_pattern: itemsInPattern.append(temp_item) print itemsInPattern item_counter = Counter(itemsInPattern) print item_counter temp_item_counter = Counter(item_counter) #dia gabisa di delete waktu di iterasi, jadi ya iterasi data copyannya baru di delete #di delete agar array yang ga masuk threshold langsung ilang, nanti diilangin pake lamda for temp_item in temp_item_counter: if (temp_item_counter[temp_item] < min_support): del item_counter[temp_item] print item_counter willBeCombinatedItem = [] willBeCombinatedItem = filter(lambda v: v in item_counter, itemReverse) print willBeCombinatedItem print '' for L in range(1, len(willBeCombinatedItem) + 1): for resultcombo in combinations(willBeCombinatedItem, L): resultcombo = list(resultcombo) if item in resultcombo: #print resultcombo #setiap kombo yang terbuat #harus di cek, berapa kali dia muncul diantara condition pattern yang telah ada support = 0 for pattern in condPattern[item]: pattern_array = pattern.split('/') flag_support_increment = 1 for combo in resultcombo: if combo not in pattern_array: flag_support_increment = 0 if flag_support_increment == 1: support += 1 if support >= min_support: print ','.join(resultcombo) + ' >>> ' + str(support) frequentPattern.append( FrequentPattern(item, ','.join(resultcombo), str(support))) """ flag = 0 #untuk setiap item pada array combo #akan dicari minimal support yang ada for combo in resultcombo: current_support = item_counter[combo] if flag == 0: support = current_support flag = 1 elif(current_support < support): support = current_support SALAH KARENA HARUSNYA GA NGITUNG SINGLE PER ITEM AJA """ print '' print '' for fp in frequentPattern: print fp.suffix + ' -- ' + fp.pattern + ' >>> ' + fp.support return frequentPattern
def lookup(self, key: str, value: Any) -> Union[Tuple[PulumiResource, ...], Tuple[()]]: """ lookup resources by searching using a key (ie. id, urn, provider, type) and value """ return findall_by_attr(self.items[0], name=key, value=value)
pub_tree_size = {'no_of_children': 2, 'depth': 3, 'tree_type': 'pub'} sub_tree_size = {'no_of_children': 2, 'depth': 3, 'tree_type': 'sub'} pub_sub_tree_size = {'no_of_children': 2, 'depth': 3, 'tree_type': 'pub_sub'} tree_sizes = [pub_tree_size, sub_tree_size, pub_sub_tree_size] participants_permissions2 = [(participant1, 1), (participant2, 2), (participant3, 3), (participant1_sibling, 1)] KeyManager.setup_topic_trees(topic, participants_permissions2, tree_sizes) # trees generated successfully here # print(RenderTree(topic.root_tree_publishers)) # get all ancestors of participant1 and publish them encrypting with pairwise keys ancestor_list = (findall_by_attr(topic.root_tree_publishers, "001"))[0].ancestors # print(ancestor_list) # make a dictionary/json of tree node names and key possessed by it # since participant would need keys of all its ancestors ancestor_keys = [] topic_to_sub_enc_keys = [] i = 0 while i < len(ancestor_list): ancestor_keys.append({ 'name': ancestor_list[i].name, 'key': ancestor_list[i].tree_node.node_key }) if i != len(ancestor_list) - 1: topic_to_sub_enc_keys.append({
def tree(self, mobile, iiuv): rc = current_app.redis_cli # 获取新用户的id member_id = f"SELECT id FROM {MEMBERS_TABLE} WHERE mobile='{mobile}'" member = MysqlSearch().get_one(member_id) # 获取父级用户id parent_id = f"SELECT id FROM {MEMBERS_TABLE} WHERE IIUV='{iiuv}'" parent = MysqlSearch().get_one(parent_id) if iiuv is None: # 设置当前用户为顶级节点 parent_id = Node(member['id']) child_id = Node(None) exporter = DictExporter() data = exporter.export(parent_id) rc.hsetnx(f"drp_relation_member_{member['id']}", 0, json.dumps(data)) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id,levels) VALUE ('{member['id']}','{member['id']}' ,1)" MysqlWrite().write(sql_iiuv) return True # TODO 查询当前用户的parent_id是否顶级节点 root_data = rc.hget(f"drp_relation_member_{parent['id']}", 0) if root_data: try: json_data = json.loads(root_data) importer = DictImporter() root = importer.import_(json_data) level_2 = Node(member['id'], parent=root) exporter = DictExporter() data = exporter.export(root) print(data) rc.hset(f"drp_relation_member_{parent['id']}", 0, json.dumps(data)) # 新增拉新redis rc.hsetnx("is_new_member", member['id'], parent['id']) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id) VALUE ('{member['id']}','{parent['id']}')" MysqlWrite().write(sql_iiuv) # 更新数据库levels字段数据 sql = f"UPDATE {ET_MEMBER_RELATIONS} SET levels=2,parent_id='{parent['id']}',top_parent_id='{parent['id']}' WHERE member_id='{member['id']}'" MysqlWrite().write(sql) # 判断当前用户是否已经关联了父级,如关联了就删除顶级用户树 gl = MysqlSearch().get_one( f"SELECT parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if gl['parent_id'] != member['id']: # 删除树 rc.delete(f"drp_relation_member_{member['id']}") # 删除缓存 rc.delete(f"user_apprentice_:{member['id']}") rc.delete(f"user_apprentice_:{parent['id']}") return True except Exception as e: current_app.logger.error(e) else: # TODO 如果当前parent_id不是顶级节点,查询当前parent_id的上级并进入树结构查询 top_parent = MysqlSearch().get_one( f"SELECT top_parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent['id']}'" ) try: root_data = rc.hget( f"drp_relation_member_{top_parent['top_parent_id']}", 0) except Exception as e: json_data = { 'member_id': member['id'], 'parent_id': parent['id'] } rc.lpush('error_not_top_parent_id', json.dumps(json_data)) if root_data: json_data = json.loads(root_data) importer = DictImporter() root = importer.import_(json_data) level_1 = findall_by_attr(root, top_parent['top_parent_id'])[0] level_2 = findall_by_attr(root, parent['id'])[0] level_3 = Node(member['id'], parent=level_2) exporter = DictExporter() data = exporter.export(root) rc.hset(f"drp_relation_member_{top_parent['top_parent_id']}", 0, json.dumps(data)) # 新增拉新redis rc.hsetnx("is_new_member", member['id'], parent['id']) # 查询当前parent的等级 lv = MysqlSearch().get_one( f"SELECT levels FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent['id']}'" ) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id) VALUE ('{member['id']}','{parent['id']}')" MysqlWrite().write(sql_iiuv) # 更新数据库levels字段数据 sql = f"UPDATE {ET_MEMBER_RELATIONS} SET levels='{lv['levels'] + 1}',parent_id='{parent['id']}',top_parent_id='{top_parent['top_parent_id']}' WHERE member_id='{member['id']}'" MysqlWrite().write(sql) # 判断当前用户是否已经关联了父级,如关联了就删除顶级用户树 gl = MysqlSearch().get_one( f"SELECT parent_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{member['id']}'" ) if gl['parent_id'] != member['id']: # 删除树 rc.delete(f"drp_relation_member_{member['id']}") # 删除缓存 rc.delete(f"user_apprentice_:{member['id']}") rc.delete(f"user_apprentice_:{parent['id']}") return True else: # 设置当前用户为顶级节点 parent_id_ = Node(parent['id']) level_2 = Node(member['id'], parent=parent_id_) exporter = DictExporter() data = exporter.export(parent_id_) # 新增树 rc.hset(f"drp_relation_member_{parent['id']}", 0, json.dumps(data)) # 新增拉新redis rc.hsetnx("is_new_member", member['id'], parent['id']) # todo 查询当前用户关系是否入库 fx = MysqlSearch().get_one( f"SELECt member_id FROM {ET_MEMBER_RELATIONS} WHERE member_id='{parent['id']}'" ) if not fx: # 新增当前用户分销关系 sql_iiuv = f"INSERT INTO {ET_MEMBER_RELATIONS} (member_id,parent_id,levels) VALUE ('{member['id']}','{parent['id']}' ,2)" MysqlWrite().write(sql_iiuv) return True
def add_participant(tree_root, participant, changed_root_keys=None): empty_nodes = findall_by_attr(tree_root, "empty") if len(empty_nodes) is 0: # tree structure change -- # get all the leaf nodes and maximum node id leaf_nodes = tree_root.leaves last_node_id = int(leaf_nodes[len(leaf_nodes) - 1].leaf_node.node_id) participant_counter = 0 added_participant = None message_details_dict_list = [] # change tree structure # children_node = TreeNode(node_count) # children_tree_node = Node(str(node_count), parent=parent, tree_node=children_node) for leaf in leaf_nodes: new_parent_tree_node = TreeNode(leaf.leaf_node.node_id) new_parent = Node(leaf.leaf_node.node_id, parent=leaf.parent, tree_node=new_parent_tree_node) # two children for this node -- or todo no of children of this root_tree -- # 1st child has the same participant and the second child is empty last_node_id += 1 leaf.leaf_node.name = str(last_node_id) leaf.parent = new_parent # send the changed structure message only to the participants affected # newly added participant messages will be handled by registration protocol not here message_detail = { "message_name": "change_tree_structure" + "/" + leaf.name, "encryption_key": leaf.leaf_node.participant.pairwise_key, "new_ancestor_key": new_parent.tree_node.node_key } message_details_dict_list.append(message_detail) # first check if the participant is already added if participant_counter is 0: # add new participant here last_node_id += 1 new_leaf_node = LeafNode(str(last_node_id), participant) added_participant = Node(participant.participant_id, parent=new_parent, leaf_node=new_leaf_node) participant_counter = 1 else: # add empty node else # if participant is already added 2nd child empty # Node("empty", parent=leaf_nodes[p].parent, leaf_node=LeafNode(leaf_nodes[p].tree_node.node_id)) last_node_id += 1 Node("empty", parent=new_parent, leaf_node=LeafNode(str(last_node_id))) return tree_root, added_participant, message_details_dict_list else: empty_node = empty_nodes[0] # participant.add_topic(topic) # include code for user-permissions # or better move this to top new_leaf_node = LeafNode(empty_node.leaf_node.node_id, participant) added_participant = Node(participant.participant_id, parent=empty_node.parent, leaf_node=new_leaf_node) # dis-allocate the old empty node after attaching the new one to the tree empty_node.parent = None empty_node.leaf_node = None # find ancestors of the added participant and change their keys ancestor_list = added_participant.ancestors for ancestor in ancestor_list: if ancestor.is_root and changed_root_keys is not None: ancestor.tree_node.root_node_keys = changed_root_keys.copy( ) else: ancestor.tree_node.reset_key() # change the keys of root node here # code to add details about the messages to be sent # first construct messages for participant and its siblings message_details_dict_list = [] # adding message for the newly added participant to be decided based on other implementations # todo """message_detail = {"message_name": str(added_participant.parent.tree_node.node_id) + "/" + str(added_participant.leaf_node.node_id), "encryption_key": added_participant.leaf_node.participant.pairwise_key, "changed_parent_key": added_participant.parent.tree_node.node_key} message_details_dict_list.append(message_detail)""" siblings = added_participant.siblings for sibling in siblings: if sibling.leaf_node.participant is not None: message_detail = { # "message_name": str(sibling.parent.tree_node.node_id) + "/" + str(sibling.leaf_node.node_id), "message_name": str(sibling.parent.tree_node.node_id) + "/" + str(sibling.leaf_node.participant.participant_id) + "__changeParent__" + str(sibling.parent.tree_node.node_id), "encryption_key": sibling.leaf_node.participant.pairwise_key, "changed_parent_key": sibling.parent.tree_node.node_key } message_details_dict_list.append(message_detail) # construct messages for ancestors and their siblings for ancestor in range(len(ancestor_list) - 2, -1, -1): children = ancestor_list[ancestor].children for child in children: message_detail = { "message_name": str(child.parent.tree_node.node_id) + "/" + str(child.tree_node.node_id) + "__changeParent__" + str(child.parent.tree_node.node_id), "encryption_key": child.tree_node.node_key } if child.parent.is_root and changed_root_keys is not None: message_detail[ "changed_parent_key"] = child.parent.tree_node.root_node_keys else: message_detail[ "changed_parent_key"] = child.parent.tree_node.node_key # "changed_parent_key": child.parent.tree_node.node_key} # if last i.e. root node then encryption keys is the list of changed pub-sub keys # add that condition for the last one. when ancestor = 0 basically. message_details_dict_list.append(message_detail) return tree_root, added_participant, message_details_dict_list
depthCurrent = 0 featuresAvailable = [] featuresList = [] for x in range(len(data[0]) - 1): featuresAvailable.append(names[x]) featuresList.append(names[x]) iteration = 0 decisionTree = tree.id3(newData, featuresAvailable, featuresList) print(RenderTree(decisionTree)) # Find accuracy totalData = len(newData) correctData = 0 correctOneLabels = [x for x in data if x[len(data[0]) - 1] == 1] correctZeroLabels = [x for x in data if x[len(data[0]) - 1] == 0] leaves = findall_by_attr(decisionTree, "Leaf") # Collect all nodes with predictions # Compare predictions of leaf tuple to label lists from data -- unsure of implementation # Add to correctData if leaf assignment matches correct label lists if file == 'synthetic-1.csv': # Known from manual decision boundary correctData = totalData accuracy = correctData / totalData print("\nAccuracy=") print(accuracy) # Visualize classifiers (matplotlib, numpy, sklearn.tree) # References: https://scikit-learn.org/stable/auto_examples/tree/plot_iris_dtc.html # https://medium.com/cascade-bio-blog/creating-visualizations-to-better-understand-your-data-and-models-part-2-28d5c46e956 """y = findall_by_attr(decisionTree, value="Leaf") X = [x for x in data if x[len(data[0]) - 1] == 1] clf = decisionTree.fit(X, y) # Can't fit() the root node from decision tree plt.subplot(2, 3, 3)
def get_ancestor_list(self, topic): participant_node = findall_by_attr(topic.root_tree, str(self.participant_id))[0] return participant_node.ancestors
list_var = list() print(participants) f = Node("f") b = Node("b", parent=f) c = Node("c", parent=b) d = Node("d", parent=b) e = Node("e", parent=f) h = Node("h", parent=e) g = Node("g", parent=e) #print(f.height) res = findall_by_attr(f, "b") res1 = res[0] b.name = "z" f.leaves #print (b.name) participant4 = Participant("12345", "004") participant5 = Participant("12345", "004") part_perm = dict() part_perm[participant4] = PermissionTypesEnum.PUBLISH.value part_perm[participant5] = PermissionTypesEnum.PUBLISH_AND_SUBSCRIBE.value del part_perm[participant5] for part_perm_key, part_perm_value in part_perm.items():
def add_participant_lkh(group, participant, permissions): group.current_key_sequence_number = random.randint(1, 100000) result = KeyManager.add_or_delete_participant(group, participant, permissions, True, False) group_tree_map = [ x for x in KeyManager.group_tree_mapping_list if x.group.id == group.id ][0] # keeping unique nonce -fix nonce_prefix_size = None nonce_prefix_value = None if group.type_of_pub_sub_group is TypeOfPubSubGroupEnum.ALL_PUBSUB.value: nonce_prefix_size = 0 nonce_prefix_value = -1 no_of_leaves = len(group_tree_map.root_tree_common.leaves) nonce_prefix_size = math.ceil(math.log2(no_of_leaves)) tree = Node(group.group_name) tree_type_name = '' if permissions is 1: tree = group_tree_map.root_tree_publishers tree_type_name = 'pub' if permissions is 2: tree = group_tree_map.root_tree_subscribers tree_type_name = 'sub' if permissions is 3: if group.type_of_pub_sub_group is TypeOfPubSubGroupEnum.ALL_PUBSUB.value: tree = group_tree_map.root_tree_common tree_type_name = 'common' else: tree = group_tree_map.root_tree_pub_sub tree_type_name = 'pub_sub' if result['tree_structure_change'] is False: for message in result['add_participant'][0]: # for sequenctial nonce #if group.type_of_pub_sub_group is TypeOfPubSubGroupEnum.ALL_PUBSUB.value: #nonce_prefix_value += 1 update_msg_topic_name = str( group.id) + "__" + result['add_participant'][1][ 'tree_type'] + message['message_name'] # todo - nonce range logic rekey_sa = RekeySa(3) '''if type(message['changed_parent_key']) is dict: # major nacl changes # message_to_bytes = json.dumps(message['changed_parent_key']) # no bytes here encoding problem # message_to_bytes = json.dumps(message['changed_parent_key']).encode('utf-8') rekey_sa.changed_keys = message['changed_parent_key'] else: message_to_bytes = message['changed_parent_key']''' rekey_sa.changed_keys = ['changed_parent_key'] GroupControllerMqttTopicsListner.send_rekey_message( message['changed_parent_key'], update_msg_topic_name, message['encryption_key'], group.current_key_sequence_number, group_eval=group) # time.sleep(2) # rekey_message = MqttMesssageData(rekey_sa, update_msg_topic_name, message['encryption_key']) # rekey_message.send_message() else: # send strucutre change message # check participants in the group # send all the new ancestors and changed keys, except for the participant # or just the new ancestor and all keys leaves = tree.leaves for leaf in leaves: if leaf.leaf_node.participant is not None and leaf.leaf_node.participant.participant_id is not participant.participant_id: if group.type_of_pub_sub_group is TypeOfPubSubGroupEnum.ALL_PUBSUB.value: nonce_prefix_value += 1 ancestor_list = (findall_by_attr( tree, leaf.leaf_node.participant.participant_id) )[0].ancestors ancestor_keys, topic_to_sub_enc_keys = GroupController.__get_ancestors_and_keys( ancestor_list, group, tree_type_name, leaf.leaf_node.participant) GroupControllerMqttTopicsListner.change_structure_message( ancestor_keys, topic_to_sub_enc_keys, leaf.leaf_node.participant.pairwise_key, leaf.leaf_node.participant.participant_id, group.id, group.current_key_sequence_number, nonce_prefix_size, nonce_prefix_value, group_eval=group) # update the nonce prefix for the participant leaf.leaf_node.participant.nonce_prefix_value = nonce_prefix_value # update other trees where group keys changed for trees in result['update_tree']: for message in trees[0]: # update_msg_topic_name = str(group.id)+"__" + trees[1]['tree_type'] + message['message_name'] update_msg_topic_name = str( group.id ) + "__" + trees[1]['tree_type'] + message['message_name'] # # print(str(group.id)+"__" + trees[1]['tree_type'] + message['message_name']) # msg_updated_key = str(message['changed_parent_key'])+" "+str(message['encryption_key']) rekey_sa = RekeySa(3) # print(update_msg_topic_name) rekey_sa.changed_keys = message['changed_parent_key'] GroupControllerMqttTopicsListner.send_rekey_message( message['changed_parent_key'], update_msg_topic_name, message['encryption_key'], group.current_key_sequence_number, group_eval=group) # time.sleep(2) if group.type_of_pub_sub_group is TypeOfPubSubGroupEnum.ALL_PUBSUB.value: if len(group.nonce_free_prefix_values) == 0: leaves = tree.leaves number_of_participants = -1 for leaf in leaves: if leaf.leaf_node.participant is not None: number_of_participants += 1 nonce_prefix_value = number_of_participants else: nonce_prefix_value = group.nonce_free_prefix_values[0] del group.nonce_free_prefix_values[0] participant.nonce_prefix_value = nonce_prefix_value # now return to registration DATA SA data_sa = DataSA(permissions, participant.pairwise_key, "LKH") # recheck ancestor_list = (findall_by_attr( tree, participant.participant_id))[0].ancestors # # # print(ancestor_list) # make a dictionary/json of tree node names and key possessed by it # since participant would need keys of all its ancestors ancestor_keys, topic_to_sub_enc_keys = GroupController.__get_ancestors_and_keys( ancestor_list, group, tree_type_name, participant) data_sa.key_management_type = KeyManagementProtocols.LKH.value data_sa.set_ancestor_keys(ancestor_keys) data_sa.set_group_keys(ancestor_keys[0]['key']) data_sa.set_rekey_topics(topic_to_sub_enc_keys) data_sa.set_subscriptions(group.topics) data_sa.set_request_rekey_topic("request_group_keys_change/" + group.id + "/" + participant.participant_id) data_sa.set_pairwise_key(participant.pairwise_key) data_sa.set_participant_id(participant.participant_id) data_sa.set_group_topics(group.topics) data_sa.set_group_id(group.id) data_sa.set_type_of_group(group.type_of_pub_sub_group) data_sa.set_change_tree_structure_topic(group.id, participant.participant_id) data_sa.leave_group_topic = "client_leave_group/" + group.id + "/" + participant.participant_id data_sa.leave_group_confirmation_topic = "client_leave_group_confirmation/" + group.id + "/" + participant.participant_id data_sa.key_sequence_number = group.current_key_sequence_number data_sa.nonce_prefix_value = nonce_prefix_value data_sa.nonce_prefix_size = nonce_prefix_size return data_sa