def get(self, path, root=None): if isinstance(path, str) or isinstance(path, unicode): path = PurePosixPath(path) if isinstance(path, PurePath): path = list(path.parts[1:] if path.is_absolute() else path.parts) if root is None: root = self.root if not isinstance(root, dict): root = {'?file': True, '?content': root} if not path: # Check if reached the final location return root next_node = path[0] path.pop(0) if '?' in next_node: return None elif next_node in root: if not isinstance(root[next_node], dict): root[next_node] = {'?file': True, '?content': root[next_node]} root[next_node]['?parent'] = root return self.get(path, root[next_node]) else: return None
def resolveTraversal(self, path): path = PurePosixPath(path) if not path.is_absolute(): return path new_path = [] for node in path.parts: if node == '.': pass elif node == '..': if len(new_path) > 1: new_path.pop() else: new_path.append(node) return PurePosixPath(*new_path)
def resolve(self, path_cur, path_next, path_home): path_cur = PurePosixPath(path_cur) path_next = PurePosixPath(path_next) path_next_parts = path_next.parts if not path_next_parts: return None path_new = None if path_next.is_absolute(): path_new = path_next elif path_next_parts[0] == '~': path_new = PurePosixPath(path_home) / PurePosixPath( *path_next_parts[1:]) else: path_new = path_cur / path_next path_new = self.resolveTraversal(path_new) if not self.get(path_new) is None: return str(path_new) else: return None