#!/usr/bin/env python3 """ merge_prefs.py — Merge Illuscape config patch into Inkscape preferences.xml Usage: python3 merge_prefs.py --prefs ~/.config/inkscape/preferences.xml \ --patch config/preferences-patch.xml \ --preset cc|cs6 """ import argparse import shutil from pathlib import Path from xml.etree import ElementTree as ET PRESET_OVERRIDES = { "cc": { ("options", "keyboard"): {"file": "illustrator-cc"}, ("options", "units"): {"doc": "px", "font": "px"}, }, "cs6": { ("options", "keyboard"): {"file": "illustrator-cs6"}, ("options", "units"): {"doc": "pt", "font": "pt"}, }, } def _find_or_create(parent: ET.Element, id_val: str) -> ET.Element: """Return child with matching id, or create and append a new one.""" for child in parent: if child.get("id") == id_val: return child child = ET.SubElement(parent, "group") child.set("id", id_val) return child def _merge_nodes(live_parent: ET.Element, patch_parent: ET.Element) -> None: """Recursively upsert patch_parent's children into live_parent.""" for patch_child in patch_parent: child_id = patch_child.get("id") if child_id is None: continue live_child = _find_or_create(live_parent, child_id) # Merge attributes: only set attrs present in patch; preserve unknown attrs for attr, value in patch_child.attrib.items(): if attr != "id": live_child.set(attr, value) # Recurse into children _merge_nodes(live_child, patch_child) def _apply_preset(root: ET.Element, preset: str) -> None: """Apply preset-specific overrides (active keys file, document units).""" for id_path, attrs in PRESET_OVERRIDES[preset].items(): node = root for id_val in id_path: node = _find_or_create(node, id_val) for attr, value in attrs.items(): node.set(attr, value) def merge(prefs_path: Path, patch_path: Path, preset: str) -> None: """Merge patch into prefs, applying preset overrides. Lenient: creates prefs if missing.""" patch_tree = ET.parse(patch_path) # raises FileNotFoundError or ParseError patch_root = patch_tree.getroot() if not prefs_path.exists(): # Lenient: seed from patch, let Inkscape fill in the rest on first launch prefs_path.parent.mkdir(parents=True, exist_ok=True) shutil.copy(patch_path, prefs_path) live_tree = ET.parse(prefs_path) _apply_preset(live_tree.getroot(), preset) live_tree.write(str(prefs_path), encoding="unicode", xml_declaration=True) return live_tree = ET.parse(prefs_path) # raises ParseError on malformed XML live_root = live_tree.getroot() _merge_nodes(live_root, patch_root) _apply_preset(live_root, preset) live_tree.write(str(prefs_path), encoding="unicode", xml_declaration=True) def main() -> None: parser = argparse.ArgumentParser( description="Merge Illuscape preferences patch into Inkscape preferences.xml" ) parser.add_argument("--prefs", required=True, type=Path) parser.add_argument("--patch", required=True, type=Path) parser.add_argument("--preset", required=True, choices=["cc", "cs6"]) args = parser.parse_args() merge(args.prefs, args.patch, args.preset) print(f"Illuscape preferences merged ({args.preset} preset)") if __name__ == "__main__": main()