#!/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 updateData(): 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 book in collection.iter(ns + "entry"): bookout = {} for dName in fields.keys(): datum = book.find(ns + dName) if datum != None: t = int(fields[dName]["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[dName]["flags"]) & 0x01 & 0xff) != 0: bookout[dName] = [] for sub in datum: bookout[dName].append(sub.text) else: bookout[dName] = datum.text #date elif t == 12: bookout[dName] = "" if datum.find(ns + "year") != None: bookout[dName] += datum.find(ns + "year").text if datum.find(ns + "month") != None: bookout[dName] += "-" + datum.find(ns + "month").text if datum.find(ns + "day") != None: bookout[dName] += "-" + datum.find(ns + "day").text elif t == 6: #Number bookout[dName] = datum.text else: print("Did not parse:" + dName + ", type:" + str(t)) bookout[dName] = "" else: bookout[dName] = "" out.append(bookout.copy()) data = out.copy() return "success" @app.route("/") def main(): if data == []: updateData() return render_template("main.html", books = data, attributes = attributes) @app.route("/book/") 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) if __name__ == "__main__": app.run(debug=True, host='0.0.0.0')