tellicoViewer/test.py

86 lines
2.8 KiB
Python
Raw Normal View History

2015-08-07 11:55:21 -04:00
#!/usr/bin/env python3
from xml.etree import ElementTree
import zipfile
from flask import Flask, render_template
app = Flask(__name__)
2015-08-08 19:26:24 -04:00
data = []
attributes= ["title",
"authors",
2015-08-08 19:26:24 -04:00
"isbn"]
fields = {}
@app.route("/r")
2015-08-08 19:26:24 -04:00
def updateData():
global data
2015-08-08 19:26:24 -04:00
out = []
2015-08-07 11:55:21 -04:00
f = zipfile.ZipFile("library.tc")
tree = ElementTree.parse(f.open("tellico.xml"))
collection = tree.getroot()[0]
ns = "{http://periapsis.org/tellico/}"
for field in collection.find(ns + "fields"):
#check if multiple entries are allowed
if (int(field.attrib["flags"]) & 0x01 & 0xff) != 0:
#change name to match
name = field.attrib["name"] + "s"
else:
name = field.attrib["name"]
fields[name] = field.attrib
2015-08-07 11:55:21 -04:00
for book in collection.iter(ns + "entry"):
bookout = {}
for datum in fields.keys():
if book.find(ns + datum) != None:
t = int(fields[datum]["type"])
#simple text, paragraph, multiple choice, image
if t == 1 or t == 2 or t == 3 or t == 10:
#check if multiple entries are allowed
#if so, put entries in a list
if (int(fields[datum]["flags"]) & 0x01 & 0xff) != 0:
bookout[datum] = []
for sub in book.find(ns + datum):
bookout[datum].append(sub.text)
else:
bookout[datum] = book.find(ns + datum).text
#date
elif t == 12:
bookout[datum] = ""
if book.find(ns + "year") != None:
bookout[datum] += book.find(ns + "year").text
if book.find(ns + "month") != None:
bookout[datum] += "-" + book.find(ns + "month").text
if book.find(ns + "day") != None:
bookout[datum] += "-" + book.find(ns + "day").text
elif t == 6: #Number
bookout[datum] = book.find(ns + datum).text
else:
print(str(t) + ": " + datum)
bookout[datum] = ""
else:
bookout[datum] = ""
2015-08-07 11:55:21 -04:00
out.append(bookout.copy())
data = out.copy()
return "success"
2015-08-08 19:26:24 -04:00
@app.route("/")
def main():
if data == []:
updateData()
2015-08-08 19:26:24 -04:00
return render_template("main.html", books = data, attributes = attributes)
2015-08-08 19:31:48 -04:00
@app.route("/book/<int:id>")
def book(id):
if data == []:
updateData()
book = next((item for item in data if int(item["id"]) == id), {})
return render_template("book.html", book = book, fields = fields)
2015-08-07 11:55:21 -04:00
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')