4 from csv import DictReader
6 from configparser import ConfigParser
10 config = ConfigParser()
11 config.read('.taguette_gdocs')
13 ## this is project ID from the configuration
14 project_id = int(config['General']['taguette_project_id'])
15 taguette_database_file = config['General']['taguette_database_file']
17 ## load the googgle sheet ID from the configuration
18 gsheet_id = config['General']['gsheet_id']
19 gsheet_gid = config['General']['gsheet_gid']
21 ## get the spreadsheet data
22 axial_url = f"https://docs.google.com/spreadsheets/d/{gsheet_id}/export?format=csv&id={gsheet_id}&gid={gsheet_gid}"
23 rv = requests.get(axial_url)
24 csv_text = rv.content.decode('utf-8')
27 con = sqlite3.connect(taguette_database_file)
30 ## import taguette.database as tagdb
31 ## db = tagdb.connect("sqlite:////home/mako/taguette-snapshot-20210422-1.sqlite3")
33 for row in DictReader(csv_text.splitlines(), delimiter=","):
37 axial_code = row['axial codes']
38 category = row['category']
39 description = row['description']
41 sql_stmt_get = "SELECT id, path, description from TAGS where id = ? AND project_id = ?"
42 cur.execute(sql_stmt_get, (tag_id, project_id))
43 tag_info = cur.fetchall()
46 print(f"ERROR: '{id}' is not unique, SKIPPING")
47 elif len(tag_info) == 0:
48 print(f"ERROR: 'tag with ID {id}' does not exist, SKIPPING")
50 oldname = tag_info[0][1]
51 old_description = tag_info[0][2]
54 newname = axial_code.lower() + "_" + new_name.lower()
56 newname = new_name.lower()
58 new_description = description
59 if description and category:
60 new_description += " "
62 new_description += json.dumps({'category' : category})
64 if not oldname == newname:
65 sql_stmt_update = "UPDATE tags SET path = ? WHERE project_id = ? AND id = ?"
66 cur.execute(sql_stmt_update, (newname, project_id, tag_id))
67 print(f"UPDATE TAG: {oldname} → {newname}")
69 if new_description.strip() != old_description.strip():
70 sql_stmt_update = "UPDATE tags SET description = ? WHERE project_id = ? AND id = ?"
71 cur.execute(sql_stmt_update, (new_description, project_id, tag_id))
72 print(f"UPDATE DESC: {old_description} → {new_description}")