""" resources.py provides useful tools for resources processing. There are 2 commands available. - clean: clean and unify the resources file names with some rules. - round: generate the rounded images from the original squared images. """ import os import subprocess import sys import config as cfg from . import resource_dir _usage = "Usage: resources.py " def cleaner_aws(f): f = f.replace("_", "-") f = f.replace("@4x", "") for p in cfg.FILE_PREFIXES["aws"]: if f.startswith(p): f = f[len(p):] break return f.lower() def cleaner_azure(f): f = f.replace("_", "-") f = f.replace("(", "").replace(")", "") f = "-".join(f.split()) for p in cfg.FILE_PREFIXES["azure"]: if f.startswith(p): f = f[len(p):] break return f.lower() def cleaner_gcp(f): f = f.replace("_", "-") f = "-".join(f.split()) for p in cfg.FILE_PREFIXES["gcp"]: if f.startswith(p): f = f[len(p):] break return f.lower() cleaners = { "aws": cleaner_aws, "azure": cleaner_azure, "gcp": cleaner_gcp, } def clean_png(pvd: str) -> None: """Refine the resources files names.""" def _rename(base: str, png: str): new = cleaners[pvd](png) old_path = os.path.join(base, png) new_path = os.path.join(base, new) os.rename(old_path, new_path) for root, _, files in os.walk(resource_dir(pvd)): pngs = filter(lambda f: f.endswith(".png"), files) [_rename(root, png) for png in pngs] def round_png(pvd: str) -> None: """Round the images.""" def _round(base: str, path: str): path = os.path.join(base, path) subprocess.call([cfg.CMD_ROUND, *cfg.CMD_ROUND_OPTS, path]) for root, _, files in os.walk(resource_dir(pvd)): pngs = filter(lambda f: f.endswith(".png"), files) paths = filter(lambda f: "rounded" not in f, pngs) [_round(root, path) for path in paths] def svg2png(pvd: str) -> None: """Convert the svg into png""" def _convert(base: str, path: str): path = os.path.join(base, path) subprocess.call([cfg.CMD_SVG2PNG, *cfg.CMD_SVG2PNG_OPTS, path]) subprocess.call(['rm', path]) for root, _, files in os.walk(resource_dir(pvd)): svgs = filter(lambda f: f.endswith(".svg"), files) [_convert(root, path) for path in svgs] # fmt: off commands = { "clean": clean_png, "round": round_png, "svg2png": svg2png, } # fmt: on if __name__ == "__main__": if len(sys.argv) < 3: print(_usage) sys.exit() cmd = sys.argv[1] pvd = sys.argv[2] if cmd not in commands: sys.exit() if pvd not in cfg.PROVIDERS: sys.exit() commands[cmd](pvd)