Extract annotation extraction to function

This commit is contained in:
Marty Oehme 2023-02-22 12:00:25 +01:00
parent 571bf09c54
commit 721e308207
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -15,21 +15,35 @@ import subprocess
def main(fpath, db):
notes = {}
with open(fpath) as f:
import csv
csv = csv.DictReader(f)
notes = get_all_annotations(db, csv)
write_to_files(notes)
def get_all_annotations(db, csv):
notes = {}
note_file = ""
for row in csv:
title_stripped = strip_book_title(row["Title"])
# switch to next book
if not is_same_book(row["Title"]):
note_file = get_notefile(row["Author"], title_stripped)
if not note_file:
documents = get_documents(db, row["Author"], title_stripped)
if not documents:
print(
f"No papis entry found for Marvin entry - {row['Author']}: {title_stripped}.\nPlease manually create."
)
continue
note_file = get_notefile(documents)
if not note_file:
print(
f"Found reference entry but no note file for - {row['Author']}: {title_stripped}."
)
manual_papis_entry()
text = format_entry(row)
@ -37,25 +51,13 @@ def main(fpath, db):
if not note_file in notes.keys():
notes[note_file] = []
notes[note_file].append(text)
write_to_files(notes)
return notes
def get_documents(author, title) -> List:
return db.query(f"author:({author}) title:({title})")
def get_notefile(author, title) -> str | None:
documents = get_documents(author, title)
if not documents:
print(
f"No papis entry found for Marvin entry - {author}: {title}.\nPlease manually create."
)
return
note_file = papis.commands.list.run(notes=True, documents=documents)
if not note_file:
print(f"Found reference entry but no note file for - {author}: {title}.")
# TODO Implement manual note creation
def manual_papis_entry():
if input(f"Create note file now? [y/N] ") == "y":
# i think instead we need to run the papis.edit cmd with note bool true and maybe editor set to none?
output = subprocess.run(
["papis", "edit", "-n"], capture_output=True, shell=True
)
@ -64,6 +66,16 @@ def get_notefile(author, title) -> str | None:
return
else:
return
def get_documents(db, author, title) -> List:
return db.query(f"author:({author}) title:({title})")
def get_notefile(document) -> str | None:
note_file = papis.commands.list.run(notes=True, documents=document)
if not note_file:
return
return str(note_file[0])
@ -121,6 +133,5 @@ if __name__ == "__main__":
if len(sys.argv) > 1
else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
)
db = papis.database.get()
main(fpath, db)
main(fpath, papis.database.get())