from pathlib import Path

from lxml import etree


def normalize_value(value: str) -> str:
    try:
        return f"{float(value):.2f}"
    except ValueError:
        pass
    return value


def icon_optimizator(file: Path):
    source_size = file.stat().st_size
    tree = etree.parse(file)
    for node in tree.findall(".//{*}path"):
        d = node.attrib["d"]
        node.attrib["d"] = " ".join(map(normalize_value, d.split(" ")))
    #file = file.parent.parent / "icons.tmp" / file.name
    #file.parent.mkdir(parents=True, exist_ok=True)
    tree.write(file)
    target_size = file.stat().st_size
    print(file.name, source_size, target_size)


def icons_optimizator():
    for file in (Path(__file__).parent / "icons").glob("*.svg"):
        icon_optimizator(file)


if __name__ == "__main__":
    icons_optimizator()
