]> code.communitydata.science - taguette_google_sheet_integration.git/commitdiff
add support for multiple projects to the update code
authorBenjamin Mako Hill <mako@atdot.cc>
Tue, 28 Feb 2023 01:21:15 +0000 (17:21 -0800)
committerBenjamin Mako Hill <mako@atdot.cc>
Tue, 28 Feb 2023 01:21:15 +0000 (17:21 -0800)
taguette-update_tags_from_sheet.py

index 565a145fba76d4f9e5fe4a93a3676e0d1d22cb63..1c3c735fd81a07597379ff2748b4a37dd3be0a7c 100755 (executable)
@@ -6,72 +6,78 @@ import sqlite3
 from configparser import ConfigParser
 import re
 import json
+import os
 
-config = ConfigParser()
-config.read('.taguette_gdocs')
-
-## this is project ID from the configuration
-project_id = int(config['General']['taguette_project_id'])
-taguette_database_file = config['General']['taguette_database_file']
-
-## load the googgle sheet ID from the configuration
-gsheet_id = config['General']['gsheet_id']
-gsheet_gid = config['General']['gsheet_gid']
-
-## get the spreadsheet data
-axial_url = f"https://docs.google.com/spreadsheets/d/{gsheet_id}/export?format=csv&id={gsheet_id}&gid={gsheet_gid}"
-rv = requests.get(axial_url)
-csv_text = rv.content.decode('utf-8')
-
-## connect to sqlite3
-con = sqlite3.connect(taguette_database_file)
-cur = con.cursor()
-
-## import taguette.database as tagdb
-## db = tagdb.connect("sqlite:////home/mako/taguette-snapshot-20210422-1.sqlite3")
-
-for row in DictReader(csv_text.splitlines(), delimiter=","):
-    #print(row)
-    tag_id = row['id']
-    new_name = row['tag']
-    axial_code = row['axial codes']
-    category = row['category']
-    description = row['description']
-
-    sql_stmt_get = "SELECT id, path, description from TAGS where id = ? AND project_id = ?"
-    cur.execute(sql_stmt_get, (tag_id, project_id))
-    tag_info = cur.fetchall()
-
-    if len(tag_info) > 1:
-        print(f"ERROR: '{id}' is not unique, SKIPPING")
-    elif len(tag_info) == 0:
-        print(f"ERROR: 'tag with ID {id}' does not exist, SKIPPING")
-    else:
-        oldname = tag_info[0][1]
-        old_description = tag_info[0][2]
-
-        if axial_code: 
-            newname = axial_code.lower() + "_" + new_name.lower()
+config_files = [f for f in os.listdir() if f.startswith('.taguette_gdocs')]
+
+for file_path in config_files:
+
+    config = ConfigParser()
+    config.read(file_path)
+
+    # this is project ID from the configuration
+    project_id = int(config['General']['taguette_project_id'])
+    print(project_id)
+    taguette_database_file = config['General']['taguette_database_file']
+
+    ## connect to sqlite3
+    con = sqlite3.connect(taguette_database_file)
+    cur = con.cursor()
+
+    # load the googgle sheet ID from the configuration
+    gsheet_id = config['General']['gsheet_id']
+    gsheet_gid = config['General']['gsheet_gid']
+
+    # get the spreadsheet data
+    axial_url = f"https://docs.google.com/spreadsheets/d/{gsheet_id}/export?format=csv&id={gsheet_id}&gid={gsheet_gid}"
+    rv = requests.get(axial_url)
+    csv_text = rv.content.decode('utf-8')
+
+    # import taguette.database as tagdb
+    # db = tagdb.connect("sqlite:////home/mako/taguette-snapshot-20210422-1.sqlite3")
+
+    for row in DictReader(csv_text.splitlines(), delimiter=","):
+        #print(row)
+        tag_id = row['id']
+        new_name = row['tag']
+        axial_code = row['axial codes']
+        category = row['category']
+        description = row['description']
+
+        sql_stmt_get = "SELECT id, path, description from TAGS where id = ? AND project_id = ?"
+        cur.execute(sql_stmt_get, (tag_id, project_id))
+        tag_info = cur.fetchall()
+
+        if len(tag_info) > 1:
+            print(f"ERROR: '{id}' is not unique, SKIPPING")
+        elif len(tag_info) == 0:
+            print(f"ERROR: 'tag with ID {id}' does not exist, SKIPPING")
         else:
-            newname = new_name.lower()
-
-        new_description = description
-        if description and category:
-            new_description += " "
-        if category:
-            new_description += json.dumps({'category' : category})
-
-        if not oldname == newname:
-            sql_stmt_update = "UPDATE tags SET path = ? WHERE project_id = ? AND id = ?"
-            cur.execute(sql_stmt_update, (newname, project_id, tag_id))
-            print(f"UPDATE TAG: {oldname} → {newname}")
-            
-        if new_description.strip() != old_description.strip():
-            sql_stmt_update = "UPDATE tags SET description = ? WHERE project_id = ? AND id = ?"
-            cur.execute(sql_stmt_update, (new_description, project_id, tag_id))
-            print(f"UPDATE DESC: {old_description} → {new_description}")
-
-con.commit()
-con.close()
+            oldname = tag_info[0][1]
+            old_description = tag_info[0][2]
+
+            if axial_code: 
+                newname = axial_code.lower() + "_" + new_name.lower()
+            else:
+                newname = new_name.lower()
+
+            new_description = description
+            if description and category:
+                new_description += " "
+            if category:
+                new_description += json.dumps({'category' : category})
+
+            if not oldname == newname:
+                sql_stmt_update = "UPDATE tags SET path = ? WHERE project_id = ? AND id = ?"
+                cur.execute(sql_stmt_update, (newname, project_id, tag_id))
+                print(f"UPDATE TAG for {project_id}: {oldname} → {newname}")
+                
+            if new_description.strip() != old_description.strip():
+                sql_stmt_update = "UPDATE tags SET description = ? WHERE project_id = ? AND id = ?"
+                cur.execute(sql_stmt_update, (new_description, project_id, tag_id))
+                print(f"UPDATE DESC for {project_id}:: {old_description} → {new_description}")
+
+    con.commit()
+    con.close()
 
 

Community Data Science Collective || Want to submit a patch?