Extract annotation extraction to function
This commit is contained in:
parent
571bf09c54
commit
721e308207
1 changed files with 50 additions and 39 deletions
89
papis-marvin
89
papis-marvin
|
@ -15,55 +15,67 @@ import subprocess
|
||||||
|
|
||||||
|
|
||||||
def main(fpath, db):
|
def main(fpath, db):
|
||||||
notes = {}
|
|
||||||
with open(fpath) as f:
|
with open(fpath) as f:
|
||||||
import csv
|
import csv
|
||||||
|
|
||||||
csv = csv.DictReader(f)
|
csv = csv.DictReader(f)
|
||||||
|
notes = get_all_annotations(db, csv)
|
||||||
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:
|
|
||||||
continue
|
|
||||||
|
|
||||||
text = format_entry(row)
|
|
||||||
|
|
||||||
if note_file and text:
|
|
||||||
if not note_file in notes.keys():
|
|
||||||
notes[note_file] = []
|
|
||||||
notes[note_file].append(text)
|
|
||||||
|
|
||||||
write_to_files(notes)
|
write_to_files(notes)
|
||||||
|
|
||||||
|
|
||||||
def get_documents(author, title) -> List:
|
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"]):
|
||||||
|
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)
|
||||||
|
|
||||||
|
if note_file and text:
|
||||||
|
if not note_file in notes.keys():
|
||||||
|
notes[note_file] = []
|
||||||
|
notes[note_file].append(text)
|
||||||
|
return notes
|
||||||
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
)
|
||||||
|
print(output)
|
||||||
|
print("NOT IMPLEMENTED: Please create note file manually.")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def get_documents(db, author, title) -> List:
|
||||||
return db.query(f"author:({author}) title:({title})")
|
return db.query(f"author:({author}) title:({title})")
|
||||||
|
|
||||||
|
|
||||||
def get_notefile(author, title) -> str | None:
|
def get_notefile(document) -> str | None:
|
||||||
documents = get_documents(author, title)
|
note_file = papis.commands.list.run(notes=True, documents=document)
|
||||||
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:
|
if not note_file:
|
||||||
print(f"Found reference entry but no note file for - {author}: {title}.")
|
return
|
||||||
if input(f"Create note file now? [y/N] ") == "y":
|
|
||||||
output = subprocess.run(
|
|
||||||
["papis", "edit", "-n"], capture_output=True, shell=True
|
|
||||||
)
|
|
||||||
print(output)
|
|
||||||
print("NOT IMPLEMENTED: Please create note file manually.")
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
return str(note_file[0])
|
return str(note_file[0])
|
||||||
|
|
||||||
|
|
||||||
|
@ -121,6 +133,5 @@ if __name__ == "__main__":
|
||||||
if len(sys.argv) > 1
|
if len(sys.argv) > 1
|
||||||
else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
|
else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
|
||||||
)
|
)
|
||||||
db = papis.database.get()
|
|
||||||
|
|
||||||
main(fpath, db)
|
main(fpath, papis.database.get())
|
||||||
|
|
Loading…
Reference in a new issue