#! /usr/bin/python3 # Copyright (C) 2018-2020 Merspieler, merspieler _at_ airmail.cc # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import sys import json import os import pymysql from common import norm verbose = False dbuser = "" dbpw = "" fname = "" argc = len(sys.argv) i = 1 while i < argc: if sys.argv[i] == "--port": i += 1 port = int(sys.argv[i]) elif sys.argv[i] == "--host": i += 1 host = sys.argv[i] elif sys.argv[i] == "-f" or sys.argv[i] == "--file": i += 1 fname = sys.argv[i] elif sys.argv[i] == "-u" or sys.argv[i] == "--user": i += 1 dbuser = sys.argv[i] elif sys.argv[i] == "-p" or sys.argv[i] == "--password": i += 1 dbpw = sys.argv[i] elif sys.argv[i] == "-v": verbose = True elif sys.argv[i] == "-h" or sys.argv[i] == "--help": print("usage: import-progress.py [OPTIONS]") print("Imports the procgress from a json file") print("") print("OPTIONS") print(" -f, --file Input file") print(" -u, --user Database user") print(" -p, --password Database password") print(" -v, --verbose Verbose printouts") print(" -h, --help Shows this help and exit") sys.exit(0) else: print("Unknown option " + sys.argv[i]) sys.exit(1) i += 1 if dbuser == "": print("ERROR: No database user given") sys.exit(1) if dbpw == "": print("ERROR: No database password given") sys.exit(1) if fname == "": print("ERROR: No file name given") sys.exit(1) db = pymysql.connect("localhost", dbuser, dbpw, "worldbuild") cursor = db.cursor() try: with open(fname) as json_data: status = json.load(json_data) except ValueError: print("ERROR: Invalid status file") sys.exit(1) except IOError: print("ERROR: Unable to read file") sys.exit(1) # Drop triggers cause they slow down things massively cursor.execute("DROP TRIGGER IF EXISTS update_second_level") for top in status: if isinstance(status[top], dict): for second in status[top]: if isinstance(status[top][second], dict) and "status" in status[top][second]: sql = "UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = '" + status[top][second]["status"] + "') WHERE name = '" + second + "'" cursor.execute(sql) sql = "SELECT id FROM secondLevel WHERE name = '" + second + "'" cursor.execute(sql) row = cursor.fetchone() sql = "UPDATE tile SET status_id = (SELECT id FROM status WHERE name = '" + status[top][second]["status"] + "') WHERE parent_id = '" + str(row[0]) + "'" cursor.execute(sql) if verbose: print("Imported " + second + " as " + status[top][second]["status"]) # Readd trigger # Trigger for updating secondLevel on tile update sql = ('CREATE OR REPLACE TRIGGER update_second_level ' 'AFTER UPDATE ON tile ' 'FOR EACH ROW ' 'BEGIN ' 'IF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id) = (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "packaged")) THEN ' 'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "packaged") WHERE id = NEW.parent_id; ' 'ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id) = (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "done")) THEN ' 'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "done") WHERE id = NEW.parent_id; ' 'ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "started")) > 0 THEN ' 'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "started") WHERE id = NEW.parent_id; ' 'ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "rebuild")) > 0 THEN ' 'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "rebuild") WHERE id = NEW.parent_id; ' 'ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "skip")) > 0 THEN ' 'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "skip") WHERE id = NEW.parent_id; ' 'ELSE ' 'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "pending") WHERE id = NEW.parent_id; ' 'END IF; ' 'END; ') cursor.execute(sql) db.commit()