aboutsummaryrefslogtreecommitdiff
path: root/backend/tolData/addPickedNames.py
diff options
context:
space:
mode:
authorTerry Truong <terry06890@gmail.com>2022-07-11 01:54:08 +1000
committerTerry Truong <terry06890@gmail.com>2022-07-11 01:54:08 +1000
commit5fe71ea7b9d9a5d2dc6e8e5ce5b9193629eed74d (patch)
tree3b8b9d7299540a812ec93e224f8fc71249a98860 /backend/tolData/addPickedNames.py
parenta8f80a02b88055cfcb45664ce3a3d24c2b2da98c (diff)
Make backend dev server script serve the image files
Previously, image files in backend/data/img were moved to, or symlinked from, public/. This needed to be changed before each build, otherwise vite would end up copying gigabytes of images.
Diffstat (limited to 'backend/tolData/addPickedNames.py')
-rwxr-xr-xbackend/tolData/addPickedNames.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/backend/tolData/addPickedNames.py b/backend/tolData/addPickedNames.py
new file mode 100755
index 0000000..d56a0cb
--- /dev/null
+++ b/backend/tolData/addPickedNames.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python3
+
+import sys
+import sqlite3
+
+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)
+
+dbFile = "data.db"
+pickedNamesFile = "pickedNames.txt"
+
+print("Opening database")
+dbCon = sqlite3.connect(dbFile)
+dbCur = dbCon.cursor()
+
+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("|")
+ 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"
+ row = dbCur.execute(query, (nodeName,)).fetchone()
+ if row != None and row[1] != altName:
+ print(f"Removing pref-alt status from alt-name {row[1]} for {nodeName}")
+ dbCur.execute("UPDATE names SET pref_alt = 0 WHERE name = ? AND alt_name = ?", row)
+ # Check for an existing record
+ if nodeName == altName:
+ continue
+ query = "SELECT name, alt_name, pref_alt FROM names WHERE name = ? AND alt_name = ?"
+ row = dbCur.execute(query, (nodeName, altName)).fetchone()
+ if row == None:
+ print(f"Adding record for alt-name {altName} for {nodeName}")
+ dbCur.execute("INSERT INTO names VALUES (?, ?, ?, 'picked')", (nodeName, altName, prefAlt))
+ else:
+ # Update existing record
+ if row[2] != prefAlt:
+ 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))
+
+print("Closing database")
+dbCon.commit()
+dbCon.close()