def assertFS(path, remainder=None): """Assert existence of fs. Step up one path segment until the directory exists, create missing directories and return filesystem. Args: path (basestring) remainder (basestring) Returns: fs """ if not os.path.isdir(path): head, tail = os.path.split(path) return assertFS(head, "/".join(filter(bool, [tail, remainder]))) fs = OSFS(path) if remainder is not None: fs.makedirs(unicode(remainder)) fs = fs.opendir(unicode(remainder)) return fs
def reduce_path(carry_fs, value): """ Reduce path by opening or creating each segment and returning the last. Args: carry_fs (fs): Opened filesystem value (str): Next path segment Returns: fs """ if not isinstance(carry_fs, FS): carry_fs = OSFS(carry_fs + separator) if not carry_fs.isdir(value): carry_fs.makedirs(value) # open next carry_fs next_carry_fs = OSFS(carry_fs.getsyspath(value)) # close carry_fs carry_fs.close() return next_carry_fs