tellicoViewer/test.py
Adam Goldsmith 6dd4161339 fix some linting errors
mostly variable/function names and spacing
2015-08-17 12:49:27 -04:00

87 lines
2.9 KiB
Python

#!/usr/bin/env python3
from xml.etree import ElementTree
import zipfile
from flask import Flask, render_template
app = Flask(__name__)
data = []
attributes = ["title",
"authors",
"isbn"]
fields = {}
@app.route("/r")
def update_data():
global data
out = []
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
for entry in collection.iter(ns + "entry"):
bookout = {}
for datum_name in fields.keys():
datum = entry.find(ns + datum_name)
if datum != None:
datum_type = int(fields[datum_name]["type"])
#simple text, paragraph, multiple choice, image
if datum_type == 1 or datum_type == 2 or datum_type == 3 or datum_type == 10:
#check if multiple entries are allowed
#if so, put entries in a list
if (int(fields[datum_name]["flags"]) & 0x01 & 0xff) != 0:
bookout[datum_name] = []
for sub in datum:
bookout[datum_name].append(sub.text)
else:
bookout[datum_name] = datum.text
#date
elif datum_type == 12:
bookout[datum_name] = ""
if datum.find(ns + "year") != None:
bookout[datum_name] += datum.find(ns + "year").text
if datum.find(ns + "month") != None:
bookout[datum_name] += "-" + datum.find(ns + "month").text
if datum.find(ns + "day") != None:
bookout[datum_name] += "-" + datum.find(ns + "day").text
elif datum_type == 6: #Number
bookout[datum_name] = datum.text
else:
print("Did not parse:" + datum_name + ", type:" + str(datum_type))
bookout[datum_name] = ""
else:
bookout[datum_name] = ""
out.append(bookout.copy())
data = out.copy()
return "success"
@app.route("/")
def main():
if data == []:
update_data()
return render_template("main.html", books=data, attributes=attributes)
@app.route("/book/<int:id_num>")
def book(id_num):
if data == []:
update_data()
entry = next((item for item in data if int(item["id"]) == id_num), {})
return render_template("book.html", book=entry, fields=fields)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')