talk-v4ov6-ietf108/imgs/split_layers.py

94 lines
2.8 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"],
"01": ["Key"],
"02": ["Key", "Routing table", "Routing table v4"],
"03": ["Key", "Routing table", "Routing table v4", "Routing table - select"],
"04": ["Key", "Yellow router"],
"05": ["Key", "Yellow router", "ARP query", "ARP query v4"],
"06": [
"Key",
"Yellow router",
"ARP query",
"ARP query v4",
"ARP answer",
"ARP answer v4",
],
"07": ["Key", "Yellow router", "Outbound packet"],
"08": ["Key", "Yellow router", "Outbound packet", "NHonlyMAC"],
"09": ["Key", "Yellow router", "Outbound packet", "NHonlyMAC", "Waitbutthen"],
"10": ["Key v6", "Routing table", "Routing table v6", "Routing table - select"],
"11": [
"Key v6",
"Yellow router",
"ARP query",
"ARP query v6",
"ARP answer",
"ARP answer v6",
],
"12": ["Key v6", "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()