Update generator script

This commit is contained in:
Dominic Zimmer 2019-11-13 14:53:24 +01:00
parent 0cbca5bde3
commit b45f4d3b37

View File

@ -1,46 +1,104 @@
import xml.etree.ElementTree as ET
import argparse
import re
pattern = re.compile("table_(.)(.*)")
table_groups = "abcdefghi"
table_group_sizes = [
15, # a: a-o
20, # b: a-t
29, # c: a-z ä-ü
15, # d: a-o
19, # e: a-s
13, # f: a-m
19, # g: a-s
8, # h: a-h
6 # i: a-f
]
table_num_to_char = list("abcdefghijklmnopqrstuvwxyz") + ["ae", "oe", "ue"]
table_char_to_num = dict([(x, y) for (y, x) in list(enumerate("abcdefghijklmnopqrstuvwxyz"))] + [("ae", 26), ("oe", 27), ("ue", 28)])
list("abcdefghijklmnopqrstuvwxyz") + ["ae", "oe", "ue"]
parser = argparse.ArgumentParser(description="Generate the static images of MensaarMapBot. The resulting map will include all tables specified by command flags (default: none).")
parser.add_argument("--input", default="mensa.svg", help="Specify the output filename (default: mensa.svg)")
parser.add_argument("--output", default="out.svg", help="Specify the output filename (default: out.svg)")
parser_singles = parser.add_argument_group("Single Table Renders")
parser_groups = parser.add_argument_group("Group Table Renders")
parser_extra = parser.add_argument_group("Extra Table Renders")
parser_extra.add_argument(f"--all", action = "store_true", help = f"Highlight all tables. This argument implies all other highlighting arguments.")
for j in range(len(table_groups)):
group = table_groups[j]
parser_groups.add_argument(f"-{group.upper()}", action = "store_true", help = f"Highlight all tables of group {group}. This implies all highlighting arguments that match -{group}*.")
for i in range(table_group_sizes[j]):
parser_singles.add_argument(f"--{group}{table_num_to_char[i]}", action = "store_true", help = f"Highlight the table {group}{table_num_to_char[i]}.")
# make parsed to namespace dictionary
parsed = vars(parser.parse_args())
# The group arguments tell us which tables to ignore as they should be displayed
disable_tables = [
list(range(size)) for size in table_group_sizes
]
if parsed["all"]:
disable_tables = []
else:
# enable groups that got enabled by -A -B ...
# and enable tables themselves
for i in range(len(table_groups)):
lower = table_groups[i].lower()
upper = table_groups[i].upper()
if parsed[upper]:
disable_tables[i] = []
# we have disabled this entire group. no need to check the singletons
continue
for j in range(table_group_sizes[i]):
thechar = table_num_to_char[j]
tableid = f"{lower}{thechar}"
if parsed[tableid]:
disable_tables[i].remove(j)
print(disable_tables)
pattern_rect = re.compile("table_(.)(.*)")
pattern_text = re.compile("label_(.)")
tree = ET.parse(parsed["input"])
root = tree.getroot()
def letters_to_numbers(group_string, number_string):
out1 = ord(group_string) - ord('a')
if len(number_string) == 1:
# only one letter
out2 = ord(number_string) - ord('a')
elif len(number_string) == 2:
# its an umlaut
out2 = {"ae": 26, "oe": 27, "ue": 28}[number_string]
return out1, out2
# parse XML for the table rects
rects = root.findall(".//{http://www.w3.org/2000/svg}rect")
rects = list(filter(lambda rect: pattern_rect.match(rect.attrib["id"]), rects))
def draw_only_group(group_id):
tree = ET.parse("mensa.svg")
root = tree.getroot()
# parse XML for the text containers
texts = root.findall(".//{http://www.w3.org/2000/svg}text")
texts = list(filter(lambda text: pattern_text.match(text.attrib["id"]), texts))
rects = root.findall(".//{http://www.w3.org/2000/svg}rect")
rects = list(filter(lambda rect: pattern.match(rect.attrib["id"]), rects))
for text in texts:
# parse group of text
match = pattern_text.match(text.attrib["id"])
group = ord(match.group(1)) - ord('a')
texts = root.findall(".//{http://www.w3.org/2000/svg}text")
texts = list(filter(lambda text: pattern_text.match(text.attrib["id"]), texts))
for text in texts:
match = pattern_text.match(text.attrib["id"])
group = ord(match.group(1)) - ord('a')
if group != group_id:
# color the child spans text white
span = list(text)[0]
style_span="font-size:3.17499995px;fill:#ffffff;fill-opacity:1;stroke-width:0.26458332;"
span.attrib["style"] = style_span
# disable text if all of its childs are disabled
all_childs_disabled = len(disable_tables[group]) == table_group_sizes[group] # no element removed
if all_childs_disabled:
print("disable group", group)
span = list(text)[0]
style_span="font-size:3.17499995px;fill:#ffffff;fill-opacity:1;stroke-width:0.26458332;"
span.attrib["style"] = style_span
else:
print("dont disable group", group)
# do nothing, we start with a full canvas
for rect in rects:
match = pattern.match(rect.attrib["id"])
group, number = letters_to_numbers(match.group(1), match.group(2))
if group != group_id:
# other group, color grey
rect.attrib["style"] = "fill:#808080;stroke-width:0.26458332"
groupstring = chr(ord('a') + group_id)
tree.write(f"mensamap_highlight_group_{groupstring}.svg")
for i in range(9):
draw_only_group(i)
for rect in rects:
match = pattern_rect.match(rect.attrib["id"])
group, number = ord(match.group(1)) - ord('a'), table_char_to_num[match.group(2)]
print(group, number)
if number in disable_tables[group]:
#other group, color grey
rect.attrib["style"] = "fill:#808080;stroke-width:0.26458332"
#groupstring = chr(ord('a') + group_id)
tree.write(parsed["output"])