4 from csv import DictReader
6 from configparser import ConfigParser
11 config_files = [f for f in os.listdir() if f.startswith('.taguette_gdocs')]
13 for file_path in config_files:
15 config = ConfigParser()
16 config.read(file_path)
18 # this is project ID from the configuration
19 project_id = int(config['General']['taguette_project_id'])
20 taguette_database_file = config['General']['taguette_database_file']
23 con = sqlite3.connect(taguette_database_file)
26 # load the googgle sheet ID from the configuration
27 gsheet_id = config['General']['gsheet_id']
28 gsheet_gid = config['General']['gsheet_gid']
30 # get the spreadsheet data
31 axial_url = f"https://docs.google.com/spreadsheets/d/{gsheet_id}/export?format=csv&id={gsheet_id}&gid={gsheet_gid}"
32 rv = requests.get(axial_url)
33 csv_text = rv.content.decode('utf-8')
35 # import taguette.database as tagdb
36 # db = tagdb.connect("sqlite:////home/mako/taguette-snapshot-20210422-1.sqlite3")
38 # ditionary to ensure that we don't have duplicate tags and to handle merges
40 for row in DictReader(csv_text.splitlines(), delimiter=","):
44 axial_code = row['axial codes']
45 category = row['category']
46 description = row['description']
48 sql_stmt_get = "SELECT id, path, description from TAGS where id = ? AND project_id = ?"
49 cur.execute(sql_stmt_get, (tag_id, project_id))
50 tag_info = cur.fetchall()
53 print(f"ERROR: '{tag_id}' is not unique, SKIPPING")
54 elif len(tag_info) == 0:
55 print(f"ERROR: 'tag with ID {tag_id}' does not exist, SKIPPING")
57 oldname = tag_info[0][1]
58 old_description = tag_info[0][2]
60 # delete any code and associated snipits
61 if new_name == "DELETE":
62 # delete all highlights
63 sql_stmt_update = "DELETE FROM highlight_tags WHERE tag_id = ?"
64 cur.execute(sql_stmt_update, (tag_id,))
66 # delete the tag itself
67 sql_stmt_update = "DELETE FROM tags WHERE project_id = ? AND id = ?"
68 cur.execute(sql_stmt_update, (project_id, tag_id))
70 print(f"DELETE TAG FROM {project_id}: {tag_id}")
74 new_name = axial_code.lower() + "_" + new_name.lower()
76 new_name = new_name.lower()
78 new_description = description
79 if description and category:
80 new_description += " "
82 new_description += json.dumps({'category' : category})
84 # merge tags if we have two with the same name
85 if new_name in seen_paths:
86 # identify what we're merging into
87 merge_keep_id = seen_paths[new_name]
88 merge_delete_id = tag_id
90 # reassociate all the highlight associated with tag 2 so they are associated with tag 1 intsead
91 sql_stmt_update = "UPDATE highlight_tags SET tag_id = ? WHERE tag_id = ?"
92 cur.execute(sql_stmt_update, (merge_keep_id, merge_delete_id))
94 # reassociate all the highlight associated with tag 2 so they are associated with tag 1 intsead
95 sql_stmt_update = "DELETE FROM tags WHERE project_id = ? AND id = ?"
96 cur.execute(sql_stmt_update, (project_id, merge_delete_id))
98 print(f"MERGE DUPLICATE TAGS for {project_id}: {new_name}")
102 seen_paths[new_name] = tag_id
104 if not oldname == new_name:
105 sql_stmt_update = "UPDATE tags SET path = ? WHERE project_id = ? AND id = ?"
106 print(f"UPDATE TAG for {project_id}: {oldname} → {new_name}")
107 cur.execute(sql_stmt_update, (new_name, project_id, tag_id))
109 if new_description.strip() != old_description.strip():
110 sql_stmt_update = "UPDATE tags SET description = ? WHERE project_id = ? AND id = ?"
111 print(f"UPDATE DESC for {project_id}: {old_description} → {new_description}")
112 cur.execute(sql_stmt_update, (new_description, project_id, tag_id))