def test_add(self): tree1 = CDirTree("a/b/c") etree = tree1.add(CDir('a/b/c/d/e')) self.assertEqual(etree.path, 'a/b/c/d/e/') ddir = tree1.get(CPath("a/b/c/d/")) self.assertEqual(('a', 'b', 'c', 'd'), ddir.names) ddir2 = tree1.get(CPath("a/b/c/d")) self.assertIsNotNone(ddir2) ddir4 = tree1.get(CDir("a/b/c/d")) self.assertIsNotNone(ddir4) ddir3 = tree1.get(CFile("a/b/c/d", 1, 1)) self.assertIsNone(ddir3)
def test_get(self): tree1 = CDirTree("a/b/c") ftree = tree1.add(CDir('a/b/c/d/e/f')) self.assertEqual(('a', 'b', 'c', 'd', 'e', 'f'), ftree.names) ddir = tree1.get(CPath('a/b/c/d/')) self.assertEqual('a/b/c/d/', ddir.path) # path type aware/unaware self.assertIsNotNone( tree1.get(CPath('a/b/c/d')) ) self.assertIsNotNone( tree1.get(CFile('a/b/c/d', 1, 1), path_type_aware=False) ) self.assertIsNone( tree1.get(CFile('a/b/c/d', 1, 1)) )
class DictMetaFileSystemBackend(BaseMetaFsBackendContract): def __init__(self, cpath_tree_dict: dict): self.__cpath_tree_dict = cpath_tree_dict self.__dict_list = self.__cpath_tree_dict['cpaths'] self.__cdir_tree = CDirTree() self.__base_path = None # build the cdir tree def json_visitor_callable(path_dict): if path_dict['type'] == 'DIR': cpath = CDir(path_dict['names']) else: assert path_dict['type'] == 'FILE' if path_dict.get('hash', None) is None: cpath = CFile(path_dict['names'], path_dict['mtime'], path_dict['size']) else: cpath = CFileHashed(path_dict['names'], path_dict['mtime'], path_dict['size'], path_dict['hash']) self.__cdir_tree.add(cpath) visit_fs_dictz(self.__dict_list, json_visitor_callable) def _get_dict_list(self): """ Currently used for testing purpose only. """ return tuple(self.__dict_list) def set_base_path(self, base_path: str): # Should I restrict to setting only once??? self.__base_path = base_path return self @property def base_path(self): bp = self.__base_path assert bp is not None return bp def _full_path(self, cpath: CPath): return os.path.join(self.base_path, cpath.path) def exists(self, cpath: CPath): """Does not care what you pass, cdir or cfile - it does not check the type of the path""" inside_cpath = self.__cdir_tree.get(cpath, path_type_aware=False) return inside_cpath is not None def is_file(self, cpath: CPath): inside_cpath = self.__cdir_tree.get(cpath) if inside_cpath is None: raise CFSPathDoesNotExistException( f'Meta File System Error (occurred during checking if the cpath is a file cpath {cpath}):\n' f'X path does not exist') else: if inside_cpath.is_file(): return True else: return False def is_dir(self, cpath: CPath): return not self.is_file(cpath) def listdir(self, cpath: CPath) -> List[str]: if cpath.names_count > 0: sub_tree = self.__cdir_tree.get_sub_tree(cpath) else: sub_tree = self.__cdir_tree if sub_tree is None: return [] else: children_cpaths = sub_tree.get_children_cpaths() return list(cpath.name for cpath in children_cpaths) # TODO: throw exceptions accordingly # if inside_cpath is None: # raise CFSException( # f'File System Error (occurred during listing cpath: {cpath}):\n' # f'X Path does not exist' # ) # if inside_cpath.is_file(): # raise CFSException( # f'File System Error (occurred during listing cpath: {cpath}):\n' # f'X Cannot list on file' # ) def getmtime(self, cpath: CPath): inside_cpath = self.__cdir_tree.get(cpath) if inside_cpath is None: raise CFSException( f'File System Error (occurred during getmtime on cpath: {cpath}):\n' f'X Path does not exist') if inside_cpath.is_dir() or cpath.is_dir(): raise CFSException( f'File System Error (occurred during getmtime on cpath: {cpath}):\n' f'X gmtime on dir') return inside_cpath.mtime def getsize(self, cpath: CPath): inside_cpath = self.__cdir_tree.get(cpath) if inside_cpath is None: raise CFSException( f'File System Error (occurred during gethash on cpath: {cpath}):\n' f'X Path not found') if cpath.is_dir() or inside_cpath.is_dir(): raise CFSException( f'File System Error (occurred during getsize on cpath: {cpath}):\n' ) return inside_cpath.size def gethash(self, cpath: CPath): inside_cpath: CFileHashed = self.__cdir_tree.get(cpath) if inside_cpath is None: raise CFSException( f'File System Error (occurred during gethash on cpath: {cpath}):\n' f'X Path not found') if not cpath.is_file() or not inside_cpath.is_file(): raise CFSException( f'File System Error (occurred during gethash on cpath: {cpath}):\n' f'get hash can only be used on Files') if not isinstance(inside_cpath, CFileHashed): raise CFSException( f'File System Error (occurred during gethash on cpath: {cpath}):\n' f'X file was not hashed') return cpath.hash def is_real_fs(self): # this is not a real fs return False