def test_add_node_at_root(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': []} path = '/' d.add_node(path, n) tree = d.get_tree() node = self.__get_list_item_by_id(tree['children'], 'a1') self.assertEqual(n, node)
def test_init_with_missing_optional_field_in_children(self): t1 = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'a1', 'key': b'1234'}]} d = DataTree(t1) t2 = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': [{'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': []}]} tree = d.get_tree() self.assertEqual(self.__add_to_root(t2), tree)
def test_add_n_level_hierarchy(self): d = DataTree() path = '' for l in range(100): level_id = 'a' + str(l) n = {'id': level_id, 'key': b'1234', 'children': []} path += '/' d.add_node(path, n) path += level_id tree = d.get_tree() t = tree for l in range(100): level_id = 'a' + str(l) n = self.__get_list_item_by_id(t['children'], level_id) self.assertEqual(n.get('id'), level_id) t = n
def test_add_n_children(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'b1', 'key': b'1234', 'children': []}]} path = '/' d.add_node(path, n) path = '/a1/b1' for l in range(100): child_id = 'c' + str(l) n = {'id': child_id, 'key': b'1234', 'children': []} d.add_node(path, n) tree = d.get_tree() t = self.__get_list_item_by_id(tree['children'], 'a1') self.assertNotEqual(t, None) t = self.__get_list_item_by_id(t['children'], 'b1') self.assertNotEqual(t, None) for l in range(100): child_id = 'c' + str(l) n = self.__get_list_item_by_id(t['children'], child_id) self.assertEqual(n.get('id'), child_id)
def test_add_tree_in_constructor(self): t = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': []} d = DataTree(t) tree = d.get_tree() self.assertEqual(self.__add_to_root(t), tree)
class KeyManager(): __key_tree = None __storage = None def __init__(self, storage): if storage is not None: try: if storage.is_initialized(): self.__storage = storage else: raise ValueError('Storage must be initilized.') except BaseException as e: raise ValueError('Invalid Storage object type') else: raise ValueError( 'Must provide storage for initializing KeyManager') if storage.is_valid(): # decode yaml s = storage.load() py_objs = None try: print(s) py_objs = yaml2py(s) except Exception as e: raise ValueError('Invalid Yaml read from storage') if py_objs is not None: # Import objects to data tree self.__key_tree = DataTree(py_objs) else: raise ValueError( 'Data read from file do not conform with DataTree format') else: self.__key_tree = DataTree() self.__save_key_tree() def __save_key_tree(self): data = self.__key_tree.get_tree() yaml = '' try: yaml = py2yaml(data) except BaseException as e: raise ValueError('Failed to encode key tree to yaml') try: file_data = bytes(yaml, 'utf-8') self.__storage.save(file_data) except BaseException as e: raise ValueError('Error saving key tree to storage') def add_key(self, path, key_id, key, attrib=None): try: key_node = {'id': key_id, 'key': key, 'attrib': attrib} self.__key_tree.add_node(path, key_node) self.__save_key_tree() except BaseException as e: raise ValueError('Error adding key.') def get_key(self, path): return self.__key_tree.get_node(path) def del_key(self, path): pass def update_key(self, path, key=None, attrib=None): pass def export_key(self, path, out_path): pass def reseal_storage(self, new_key): pass