diff options
Diffstat (limited to 'backend/data/addPickedNames.py')
| -rwxr-xr-x | backend/data/addPickedNames.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/backend/data/addPickedNames.py b/backend/data/addPickedNames.py index 3ef099a..d56a0cb 100755 --- a/backend/data/addPickedNames.py +++ b/backend/data/addPickedNames.py @@ -3,12 +3,11 @@ import sys import sqlite3 -usageInfo = f"usage: {sys.argv[0]}\n" -usageInfo += "Reads alt-name data from a file, and adds it to the 'names' table.\n" -usageInfo += "The file is expected to have lines of the form: nodeName|altName|prefAlt\n" -usageInfo += " These correspond to entries in the 'names' table. 'prefAlt' should\n" -usageInfo += " be 1 or 0. A line may specify name1|name1|1, which causes the node\n" -usageInfo += " to have no preferred alt-name.\n" +usageInfo = f""" +Usage: {sys.argv[0]} + +Reads alt-name data from a file, and adds it to the database's 'names' table. +""" if len(sys.argv) > 1: print(usageInfo, file=sys.stderr) sys.exit(1) @@ -16,15 +15,21 @@ if len(sys.argv) > 1: dbFile = "data.db" pickedNamesFile = "pickedNames.txt" -# Open db +print("Opening database") dbCon = sqlite3.connect(dbFile) dbCur = dbCon.cursor() -# Iterate through picked-names file + +print("Iterating through picked-names file") with open(pickedNamesFile) as file: for line in file: # Get record data - (nodeName, altName, prefAlt) = line.lower().rstrip().split("|") + nodeName, altName, prefAlt = line.lower().rstrip().split("|") prefAlt = int(prefAlt) + # Check whether there exists a node with the name + row = dbCur.execute("SELECT name from nodes where name = ?", (nodeName,)).fetchone() + if row == None: + print(f"ERROR: No node with name \"{nodeName}\" exists") + break # Remove any existing preferred-alt status if prefAlt == 1: query = "SELECT name, alt_name FROM names WHERE name = ? AND pref_alt = 1" @@ -46,6 +51,7 @@ with open(pickedNamesFile) as file: print(f"Updating record for alt-name {altName} for {nodeName}") dbCur.execute("UPDATE names SET pref_alt = ?, src = 'picked' WHERE name = ? AND alt_name = ?", (prefAlt, nodeName, altName)) -# Close db + +print("Closing database") dbCon.commit() dbCon.close() |
