def writer(obj, fname, mkdir_no_exist, skip_if_exist, write_func_name, unknown_type): fname = make_path(fname) if skip_if_exist and os.path.isfile(fname): # pragma: no cover return False if mkdir_no_exist: dname = os.path.dirname(fname) if dname not in set([".", "..", ""]): mkdir(dname) compression = [] stripped_fname = fname for k, v in EXT_TO_COMPRESSION.items(): if fname.endswith(k): compression.append(v) stripped_fname = stripped_fname[:-(len(k) + 1)] ext = stripped_fname.split(".")[-1] if "." in stripped_fname[-6:] else None if ext not in EXT_TO_MODULE and unknown_type == "RAISE": raise TypeError("just does not yet cover '{}'".format(ext)) writer_module = EXT_TO_MODULE.get(ext, None) or EXT_TO_MODULE[unknown_type] write_fn = getattr(writer_module, write_func_name) if compression: # actually returns a file handler >.< compression = compression[0] with compression(fname, "wb") as f: return write_fn(obj, f) else: return write_fn(obj, fname)
def make_path(name): dir_name = os.path.expanduser("~/nostalgia_data/dfs/") just.mkdir(dir_name, 0o700) return dir_name + slugify(name)