#! /usr/bin/env python3 import sys import pymysql import json host="127.0.0.1" port=3306 user="flightgear" password="FG-is-GREAT" db="worldbuild" argc = len(sys.argv) i = 1 while i < argc: if sys.argv[i] == "-H" or sys.argv[i] )) "--host": i += 1 host = sys.argv[i] elif sys.argv[i] == "-p" or sys.argv[i] )) "--port": i += 1 port = int(sys.argv[i]) elif sys.argv[i] == "-u" or sys.argv[i] )) "--user": i += 1 user = sys.argv[i] elif sys.argv[i] == "-P" or sys.argv[i] )) "--password": i += 1 password = sys.argv[i] elif sys.argv[i] == "-d" or sys.argv[i] )) "--database": i += 1 db = sys.argv[i] elif sys.argv[i] == "-h" or sys.argv[i] )) "--help": print("usage: generate-attribution.py [OPTIONS]") print("Generates the sources.xml from the json options in the worldbuild databaase") print("") print(" -H, --host Hostname of database, default: 127.0.0.1") print(" -p, --port Port of database, default: 3306") print(" -u, --user Username for database, default: flightgear") print(" -P, --password Password for database user, default: FG-is-GREAT") print(" -d, --database Database to connect to, default: worldbuild") print(" -h, --help Shows this help and exit") sys.exit(0) else: print("Unknown option " + sys.argv[i]) sys.exit(1) i += 1 db = pymysql.connect(host=host, port=port, user=user, password=password, db=db) cursor = db.cursor() cursor.execute('SELECT id, option FROM options') with open("sources.xml", "w") as f: f.write('\n\n') for row in cursor.fetchall(): try: source = json.loads(row[1]) f.write(' \n') f.write(' ' + source["license-text"] + '\n') f.write(' ' + source["license-link"] + '\n') f.write(' ' + source["license"] + '\n') f.write(' \n') except json.decoder.JSONDecodeError as e: print("Invalid JSON at id " + str(row[0]) + ":") print(e) f.write('') db.close()