This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-xml.py
100 lines (77 loc) · 3.32 KB
/
build-xml.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
#!/usr/bin/env python3
import os
import sys
import csv
from lxml import etree as ET
if sys.version_info[0] != 3:
print("# This script requires Python version 3.x")
sys.exit(1)
def xml_escape(value):
value = "" if value == None else str(value)
value = value.replace("&", "&").replace("<", "<").replace(">",">").replace("\"", """)
return value
def main(argv):
if len(argv) < 3:
print("usage: ./run.py </path/to/genre.csv> <output_dir>")
sys.exit(1)
genre_csv = argv[1]
out_dir = argv[2]
filter_dir = argv[3]
if not os.path.isfile(genre_csv):
print("Not a file: " + str(genre_csv))
sys.exit(1)
if not os.path.isdir(out_dir):
print("Not a directory: " + str(out_dir))
sys.exit(1)
filter_books = os.listdir(filter_dir)
genres = {}
genre_id = 1
with open(genre_csv, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
genre = row[0]
identifier = row[1]
title = row[2]
available = row[3]
if genre is None or genre == "":
continue
if identifier not in filter_books:
continue
if genre not in genres:
genres[genre] = {
"id": str(genre_id),
"books": {}
}
genre_id += 1
genres[genre]["books"][identifier] = {"title": title, "available": available}
sorted_genres = sorted([genre for genre in genres], key=lambda genre: len(genres[genre]["books"]), reverse=True)
genre_list_csv = os.path.join(out_dir, "genres.csv")
with open(genre_list_csv, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for genre in sorted_genres:
csvwriter.writerow([genre, genres[genre]["id"] + ".xml", str(len(genres[genre]["books"]))])
for genre in sorted_genres:
genre_xml = os.path.join(out_dir, genres[genre]["id"] + ".xml")
# <active_loans>...</active_loans>
active_loans = ET.Element("active_loans")
# <result><success>true</success><message/></result>
result = ET.SubElement(active_loans, "result")
success = ET.SubElement(result, "success")
success.text = "true"
message = ET.SubElement(result, "message")
# <data><books>...</books></data>
data = ET.SubElement(active_loans, "data")
data.append(ET.Comment(" " + genre + " "))
books = ET.SubElement(data, "books")
for identifier in genres[genre]["books"]:
# <book><id>...</id><title>...</title></book>
book = ET.SubElement(books, "book")
id_element = ET.SubElement(book, "id")
id_element.text = identifier
title = ET.SubElement(book, "title")
title.text = genres[genre]["books"][identifier]["title"]
book.append(ET.Comment(" " + genres[genre]["books"][identifier]["available"] + " "))
tree = ET.ElementTree(active_loans)
tree.write(genre_xml, xml_declaration=True, encoding='UTF-8', pretty_print=True)
if __name__ == "__main__":
main(sys.argv)