-
Notifications
You must be signed in to change notification settings - Fork 1
/
scriptus.py
134 lines (105 loc) · 3.99 KB
/
scriptus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
import os
import csv
import xml.dom.minidom
#X_TRANSLATE = (23.233, 78.333334, 133.433)
#Y_TRANSLATE = (26.110997, 108.21131, 190.311)
X_TRANSLATE = (22.104, 78.237, 134.380)
Y_TRANSLATE = (19.117, 106.277 , 193.429)
CARD_TEMPLATE_FILE = str(os.getcwd()) + "/sniðmát/56x87.svg"
DATABASE_FILE = str(os.getcwd()) + "/gagnasafn.csv"
IMAGE_DIR = str(os.getcwd()) + "/myndir"
PRINTABLES_DIR = str(os.getcwd()) + "/printables"
A4_TEMPLATE = """
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="210mm"
height="297mm"
viewBox="0 0 210 297"
version="1.1">
{content}
</svg>
"""
def get_card_template():
DOMTree = xml.dom.minidom.parse(CARD_TEMPLATE_FILE)
g = DOMTree.documentElement.getElementsByTagName("g")[0]
return g.toxml()
def open_database():
with open(DATABASE_FILE) as f:
database = csv.DictReader(f)
rows = list(database)
return rows
def get_image_url(number):
files = os.listdir(IMAGE_DIR)
image = [img for img in files if img.startswith(f"{number:03}")]
if not image:
return ""
image_url = f"file://{IMAGE_DIR}/{image[0]}"
return image_url
def fill_template(template, row):
number = int(row["Númer Dýrs"])
output = template
if row["Afkvæmi avg"] != "":
offsprings = row["Afkvæmi avg"]
else:
offsprings = row["Afkvæmi min"] + " - " + row["Afkvæmi max"]
if row["Líftími avg"] != "":
longevity = row["Líftími avg"]
else:
longevity = row["Líftími min"] + " - " + row["Líftími max"]
if row["Þyngd avg"] != "":
weight = row["Þyngd avg"]
else:
weight = row["Þyngd min"] + " - " + row["Þyngd max"]
if row["Lengd avg"] != "":
length = row["Lengd avg"]
else:
length = row["Lengd min"] + " - " + row["Lengd max"]
scientific_name_length = str(len(row["Fræðiheiti"].strip().replace(" ", "")))
if (len(row['Nafn Dýrs']) > 32 and "-" in row['Nafn Dýrs']):
hyphen_name = row['Nafn Dýrs']
output = output.replace("#NAME", hyphen_name[0:hyphen_name.index("-")+1])
output = output.replace("#LONGER_NAME", hyphen_name[hyphen_name.index("-")+1:])
else:
output = output.replace("#NAME", row["Nafn Dýrs"])
output = output.replace("#LONGER_NAME", "")
output = output.replace("#CARD_NUMBER", str(number))
output = output.replace("#SCIENTIFIC_NAME_LENGTH", scientific_name_length)
output = output.replace("#SCIENTIFIC_NAME", row["Fræðiheiti"])
output = output.replace("#OFFSPRINGS", offsprings)
output = output.replace("#LONGEVITY", longevity)
output = output.replace("#FEET", row["Fætur"])
output = output.replace("#WEIGHT", weight)
output = output.replace("#LENGTH", length)
#output = output.replace("#IMAGE_REFERENCE", row["Vísun í mynd á spjaldi"])
output = output.replace("#IMAGE_URL", get_image_url(number))
return output
def main():
card_template = get_card_template()
rows = open_database()
groups = [fill_template(card_template, row) for row in rows]
printable = 1
start = 0
end = 9
while start < len(groups):
page = groups[start:end]
content = []
for i, card in enumerate(page):
t_x = X_TRANSLATE[i % 3]
t_y = Y_TRANSLATE[i // 3]
content.append(f'<g transform="translate({t_x}, {t_y})">{card}</g>')
a4_svg = A4_TEMPLATE.format(content="\n".join(content))
with open(f"{PRINTABLES_DIR}/page_{printable:02}.svg", "w") as f:
f.write(a4_svg)
printable += 1
start += 9
end += 9
main()