From abaae56cc75a0401c80c0853321bec6073d14ade Mon Sep 17 00:00:00 2001 From: Charles Kiene Date: Mon, 27 Feb 2023 16:23:34 -0800 Subject: [PATCH] Added feature to read in multiple config files and iterate over each one to produce separate tag files with corresponding project ID name. --- taguette-export_tags_to_csv.py | 66 +++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/taguette-export_tags_to_csv.py b/taguette-export_tags_to_csv.py index 53a5c48..cf109db 100755 --- a/taguette-export_tags_to_csv.py +++ b/taguette-export_tags_to_csv.py @@ -3,40 +3,50 @@ import re import sqlite3 from configparser import ConfigParser +import csv +import os -config = ConfigParser() -config.read('.taguette_gdocs') +config_files = [f for f in os.listdir() if f.startswith('.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'] +for file_path in config_files: + config = ConfigParser() + config.read(file_path) -## connect to sqlite3 -con = sqlite3.connect(taguette_database_file) -cur = con.cursor() + ## this is project ID from the configuration + project_id = int(config['General']['taguette_project_id']) + taguette_database_file = config['General']['taguette_database_file'] -# Run this if you just want tags and no highlights -sql_stmt_get = "SELECT id, path, description FROM tags WHERE project_id = ?" + # set output file name + output_file_name = f'exported_tags/exported_tags_{project_id}.tsv' -# Run this if you want tags AND highlights -#sql_stmt_get = "SELECT tags.id, tags.path, tags.description, highlights.snippet FROM highlight_tags INNER JOIN tags ON highlight_tags.tag_id = tags.id INNER JOIN highlights ON highlight_tags.highlight_id = highlights.id WHERE project_id = ?" -cur.execute(sql_stmt_get, (project_id,)) + ## connect to sqlite3 + con = sqlite3.connect(taguette_database_file) + cur = con.cursor() -while True: - row = cur.fetchone() - if row == None: - break - - tag_id, path, description = row + # Run this if you just want tags and no highlights + sql_stmt_get = "SELECT id, path, description FROM tags WHERE project_id = ?" - m = re.match(r'^(.+)\_(.*)$', path) - if m: - axial = m.group(1) - tag = m.group(2) - else: - axial = "" - tag = path - - print("\t".join([str(tag_id), axial, tag, description])) + # Run this if you want tags AND highlights + #sql_stmt_get = "SELECT tags.id, tags.path, tags.description, highlights.snippet FROM highlight_tags INNER JOIN tags ON highlight_tags.tag_id = tags.id INNER JOIN highlights ON highlight_tags.highlight_id = highlights.id WHERE project_id = ?" + cur.execute(sql_stmt_get, (project_id,)) + + with open(output_file_name, 'w', newline='') as output_file: + writer = csv.writer(output_file, delimiter='\t') + while True: + row = cur.fetchone() + if row == None: + break + + tag_id, path, description = row + + m = re.match(r'^(.+)\_(.*)$', path) + if m: + axial = m.group(1) + tag = m.group(2) + else: + axial = "" + tag = path + + writer.writerow([str(tag_id), axial, tag, description]) \ No newline at end of file -- 2.39.2