61 lines
1.4 KiB
Python
61 lines
1.4 KiB
Python
from typing import *
|
|
import os
|
|
import subprocess
|
|
import re
|
|
import sys
|
|
|
|
|
|
OUTPUT_DIR: str = "._graphics"
|
|
ID_REGEX: re.Pattern = re.compile("rbk_.*")
|
|
|
|
|
|
def get_all_ids(svg_file_name: str) -> List[str]:
|
|
objects: bytes = subprocess.check_output([
|
|
"inkscape",
|
|
"--query-all",
|
|
svg_file_name,
|
|
])
|
|
return [
|
|
obj.split(b",")[0].decode("utf-8")
|
|
for obj in
|
|
objects.splitlines()
|
|
]
|
|
|
|
|
|
def id_filter(svg_id: str) -> bool:
|
|
return ID_REGEX.match(svg_id) is not None
|
|
|
|
|
|
def export_objects(svg_file: str, object_ids: Iterable[str]) -> None:
|
|
|
|
for obj in object_ids:
|
|
command = [
|
|
"inkscape",
|
|
"--export-type=svg",
|
|
"--export-plain-svg",
|
|
"--vacuum-defs",
|
|
"--export-id-only",
|
|
"--export-id",
|
|
obj,
|
|
"-o",
|
|
OUTPUT_DIR + "/" + obj[4:],
|
|
svg_file,
|
|
]
|
|
print(f"exporting {obj[4:]}...", end="", flush=True)
|
|
res = subprocess.check_output(command)
|
|
if res:
|
|
print(f" An error might have occurred:\n{res}")
|
|
else:
|
|
print(" done")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if not os.path.isdir(OUTPUT_DIR):
|
|
os.mkdir(OUTPUT_DIR)
|
|
|
|
for graphics_file in sys.argv[1:]:
|
|
export_objects(
|
|
graphics_file,
|
|
filter(id_filter, get_all_ids(graphics_file)),
|
|
)
|