Move logic to main function

This commit is contained in:
Marty Oehme 2023-02-21 09:03:22 +01:00
parent ae495af1bc
commit d78c5dd64a
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -6,7 +6,6 @@
# annotations as 'csv' format and then point the script to the # annotations as 'csv' format and then point the script to the
# resulting file. # resulting file.
import sys import sys
import csv
import papis.api import papis.api
import papis.commands.list import papis.commands.list
import papis.database import papis.database
@ -14,75 +13,87 @@ import re
import subprocess import subprocess
# use argument passed to command as file or default file here # use argument passed to command as file or default file here
fpath = (
sys.argv[1]
if len(sys.argv) > 1
else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
)
db = papis.database.get()
notes = {}
with open(fpath) as f:
csv = csv.DictReader(f)
title_strip_pattern = re.compile(r"([^\s\w]|_)+") def format_entry(row) -> str:
old_title = "" text = f"> {row['HighlightText']}"
note_file = "" if row["EntryText"]:
for row in csv: if text:
title_stripped = title_strip_pattern.sub("", row["Title"]) text += "\n"
else:
text = "> "
text += f"{row['EntryText']}"
return text
# switch to next book
if old_title != row["Title"]:
old_title = row["Title"]
documents = db.query(f"author:({row['Author']}) title:({title_stripped})") def main(fpath, db):
if not documents: notes = {}
print( with open(fpath) as f:
f"No papis entry found for Marvin entry - {row['Author']}: {row['Title']}.\nPlease manually create." import csv
csv = csv.DictReader(f)
title_strip_pattern = re.compile(r"([^\s\w]|_)+")
old_title = ""
note_file = ""
for row in csv:
title_stripped = title_strip_pattern.sub("", row["Title"])
# switch to next book
if old_title != row["Title"]:
old_title = row["Title"]
documents = db.query(
f"author:({row['Author']}) title:({title_stripped})"
) )
continue if not documents:
note_file = papis.commands.list.run(notes=True, documents=documents) print(
if not note_file: f"No papis entry found for Marvin entry - {row['Author']}: {row['Title']}.\nPlease manually create."
print(f"Found reference entry but no note file for - {row['Author']}: {row['Title']}.")
if (
input(
f"Create note file now? [y/N] "
) )
== "y" continue
): note_file = papis.commands.list.run(notes=True, documents=documents)
output = subprocess.run( if not note_file:
["papis", "edit", "-n"], capture_output=True, shell=True print(
f"Found reference entry but no note file for - {row['Author']}: {row['Title']}."
) )
print(output) if input(f"Create note file now? [y/N] ") == "y":
# print("NOT IMPLEMENTED: Please create note file manually.") output = subprocess.run(
continue ["papis", "edit", "-n"], capture_output=True, shell=True
else: )
continue print(output)
note_file = str(note_file[0]) # print("NOT IMPLEMENTED: Please create note file manually.")
continue
else:
continue
note_file = str(note_file[0])
text = f"> {row['HighlightText']}" text = format_entry(row)
if row["EntryText"]:
if text:
text += "\n"
else:
text = "> "
text += f"{row['EntryText']}"
if note_file and text: if note_file and text:
if not note_file in notes.keys(): if not note_file in notes.keys():
notes[note_file] = [] notes[note_file] = []
notes[note_file].append(text) notes[note_file].append(text)
# write to notes
for f, entries in notes.items():
if f:
with open(f, "a") as note:
print(f"Editing {f}...")
num_added = 0
for entry in entries:
with open(f) as noteread:
if entry not in noteread.read():
note.write(f"{entry}\n\n")
num_added += 1
print(f"Added {num_added} entries to it.")
# write to notes if __name__ == "__main__":
for f, entries in notes.items(): fpath = (
if f: sys.argv[1]
with open(f, "a") as note: if len(sys.argv) > 1
print(f"Editing {f}...") else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
num_added = 0 )
for entry in entries: db = papis.database.get()
with open(f) as noteread:
if entry not in noteread.read(): main(fpath, db)
note.write(f"{entry}\n\n")
num_added += 1
print(f"Added {num_added} entries to it.")