#!/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 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] = "" 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')