talk-v4ov6-ietf108/imgs/split_layers.py

75 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
from xml.etree import ElementTree as ET
import sys
from pathlib import Path
IN_DIR = Path(__file__).parent
OUT_DIR = IN_DIR / "_autogen"
namespaces = {
"svg": "http://www.w3.org/2000/svg",
"inkscape": "http://www.inkscape.org/namespaces/inkscape",
}
splits = {
"mac.svg": {
"_always": ["Base", "Key"],
"0": [],
"1": ["Routing table"],
"2": ["Routing table", "Routing table - select"],
"3": ["Yellow router"],
"4": ["Yellow router", "ARP query"],
"5": ["Yellow router", "ARP query", "ARP answer"],
"6": ["Yellow router", "Outbound packet"],
}
}
def process_svg(svg_splits, path, out_path):
out_path.mkdir(parents=True, exist_ok=True)
always = svg_splits.get("_always", [])
svg = ET.parse(path.as_posix())
for keyframe in svg_splits:
if keyframe.startswith("_"): # special value -- eg `_always`
continue
layers = always + svg_splits[keyframe]
for layer_node in svg.findall(
".//svg:g[@inkscape:groupmode='layer']", namespaces=namespaces
):
layer_node.attrib["style"] = "display:none"
for layer_name in layers:
layer = svg.find(
".//svg:g[@inkscape:groupmode='layer'][@inkscape:label='{}']".format(
layer_name
),
namespaces=namespaces,
)
if not layer:
raise Exception("Layer {} not found".format(layer_name))
layer.attrib["style"] = "display:inline"
keyframe_outpath = out_path / "{}.svg".format(keyframe)
svg.write(keyframe_outpath.as_posix(), encoding="utf-8")
print("Written {}".format(keyframe_outpath.as_posix()))
def main():
processlist = []
if len(sys.argv) < 2:
processlist = splits.keys()
else:
processlist = [sys.argv[1]]
for svg_path in processlist:
process_svg(
splits[svg_path], IN_DIR / svg_path, OUT_DIR / (Path(svg_path).stem)
)
if __name__ == "__main__":
main()