def make_path(target, desired_base_of_target='.', actual_base_of_target='/', desired_base_path_to_result='', relpath=relpath, target_join=os.path.join, result_join=os.path.join): """ makes a path to target from desired_base_of_result, assuming that target is a (relative) path based in actual_base_of_target, and that desired_base_path_to_result is equivalent to actual_base_of_target This can be used, for example, to make a url out of a local path. """ full_path = target_join(actual_base_of_target, target) relative_path = relpath(full_path, desired_base_of_target) return result_join(desired_base_path_to_result, relative_path)
def get_list(alphabet_id=None, id_=None, from_path=default_from_path): if not use_dict: def make_dict(): use_dict = {} for base, dirs, files in os.walk(base_dir): print(base) for file_name in sorted(files): match = reg.match(file_name) if match: name, number, cur_id = match.groups() name = name if name not in use_dict: use_dict[name] = {} if cur_id not in use_dict[name]: use_dict[name][cur_id] = [] use_dict[name][cur_id].append(make_path(file_name, desired_base_of_target=default_from_path, actual_base_of_target=base)) return use_dict use_dict.update(objectstorage.get_object(name, make_dict, timestamp_dir=default_from_path)) if default_from_path == from_path: def fix_path(path): return path else: def fix_path(path): return make_path(path, desired_base_of_target=from_path, actual_base_of_target=default_from_path) if alphabet_id is None: rtn = {} if id_ is None: for alphabet_id in use_dict: rtn[alphabet_id] = {} for id_ in use_dict[alphabet_id]: rtn[alphabet_id][id_] = maplist(fix_path, use_dict[alphabet_id][id_]) else: rtn = {} for alphabet_id in use_dict: if id_ in use_dict[alphabet_id]: rtn[alphabet_id] = maplist(fix_path, use_dict[alphabet_id][id_]) return rtn else: if id_ is None: if alphabet_id not in use_dict: return {} rtn = {} for cur_id in use_dict[alphabet_id]: rtn[cur_id] = maplist(fix_path, use_dict[alphabet_id][cur_id]) return rtn else: if alphabet_id not in use_dict: return [] return maplist((lambda i: relpath(i, from_path)), use_dict[alphabet_id][id_])