worldbuild/generate-attribution.py

65 lines
2 KiB
Python
Raw Normal View History

#! /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 <host> Hostname of database, default: 127.0.0.1")
print(" -p, --port <port> Port of database, default: 3306")
print(" -u, --user <user> Username for database, default: flightgear")
print(" -P, --password <pw> Password for database user, default: FG-is-GREAT")
print(" -d, --database <db> 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('<?xml version="1.0"?>\n<PropertyList>\n')
for row in cursor.fetchall():
try:
source = json.loads(row[1])
f.write(' <source>\n')
f.write(' <name>' + source["license-text"] + '</name>\n')
f.write(' <link>' + source["license-link"] + '</link>\n')
f.write(' <license>' + source["license"] + '</license>\n')
f.write(' </source>\n')
except json.decoder.JSONDecodeError as e:
print("Invalid JSON at id " + str(row[0]) + ":")
print(e)
f.write('</PropertyList>')
db.close()