def test_immutable_responses(self): t1 = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': [{ 'id': 'a1', 'key': b'1234', 'attrib': { 'attrib1': 123, 'attrib2': '1234', 'attrib3': {'abc': 123}, 'attrib4': [1236], 'attrib5': b'1273' }, 'children': [] }, { 'id': 'a2', 'key': b'1234', 'attrib': {}, 'children': [] }]} d = DataTree(t1) t1['id'] = 'b1' self.assertRaises(TMSError, d.get_node, '/b1') n1 = d.get_node('/a1/a1') n1['key'] = b'xyz' n2 = d.get_node('/a1/a1') self.assertEqual(n2['key'], b'1234') a1 = d.get_node_attrib_list('/a1/a1') a1['attrib3'] = b'xyz' a2 = d.get_node_attrib_list('/a1/a1') self.assertEqual(a2['attrib3'], {'abc': 123})
def test_del_node_invalid_path(self): t1 = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': [{ 'id': 'a1', 'key': b'1234', 'attrib': { 'attrib1': 123, 'attrib2': '1234', 'attrib3': {'abc': 123}, 'attrib4': [1236], 'attrib5': b'1273' }, 'children': [] }, { 'id': 'a2', 'key': b'1234', 'attrib': {}, 'children': [] }]} d = DataTree(t1) node = d.get_node('/a1/a1') self.assertEqual(node, t1['children'][0]) node = d.del_node('/a1/a1') self.assertRaises(TMSError, d.get_node, '/a1/a1') self.assertRaises(TMSError, d.del_node, '/a1/a1/a1') self.assertRaises(TMSError, d.del_node, '/a1/a5') self.assertRaises(TMSError, d.del_node, '/')
def test_get_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 path = '' for l in range(100): level_id = 'a' + str(l) n = {'id': level_id, 'key': b'1234', 'children': []} path += '/' + level_id d.get_node(path) self.assertEqual(n.get('id'), level_id)
def test_addnode_without_children(self): d = DataTree() n = {'id': 'a1', 'key': b'1234'} path = '/' d.add_node(path, n) n['children'] = [] n['attrib'] = {} self.assertEqual(d.get_node('/a1'), n)
def test_add_node_with_missing_optional_field_in_children(self): d = DataTree() n1 = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'a1', 'key': b'1234'}]} d.add_node('/', n1) n2 = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': [{'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': []}]} node = d.get_node('/a1') self.assertEqual(n2, node)
def test_get_n_level_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) for l in range(100): path = '/a1/b1/' child_id = 'c' + str(l) path += child_id n = d.get_node(path) self.assertEqual(n.get('id'), child_id)
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