Move logic to main function
This commit is contained in:
parent
ae495af1bc
commit
d78c5dd64a
1 changed files with 73 additions and 62 deletions
63
papis-marvin
63
papis-marvin
|
@ -6,7 +6,6 @@
|
|||
# annotations as 'csv' format and then point the script to the
|
||||
# resulting file.
|
||||
import sys
|
||||
import csv
|
||||
import papis.api
|
||||
import papis.commands.list
|
||||
import papis.database
|
||||
|
@ -14,15 +13,24 @@ import re
|
|||
import subprocess
|
||||
|
||||
# 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:
|
||||
|
||||
def format_entry(row) -> str:
|
||||
text = f"> {row['HighlightText']}"
|
||||
if row["EntryText"]:
|
||||
if text:
|
||||
text += "\n"
|
||||
else:
|
||||
text = "> "
|
||||
text += f"{row['EntryText']}"
|
||||
return text
|
||||
|
||||
|
||||
def main(fpath, db):
|
||||
notes = {}
|
||||
with open(fpath) as f:
|
||||
import csv
|
||||
|
||||
csv = csv.DictReader(f)
|
||||
|
||||
title_strip_pattern = re.compile(r"([^\s\w]|_)+")
|
||||
|
@ -35,7 +43,9 @@ with open(fpath) as f:
|
|||
if old_title != row["Title"]:
|
||||
old_title = row["Title"]
|
||||
|
||||
documents = db.query(f"author:({row['Author']}) title:({title_stripped})")
|
||||
documents = db.query(
|
||||
f"author:({row['Author']}) title:({title_stripped})"
|
||||
)
|
||||
if not documents:
|
||||
print(
|
||||
f"No papis entry found for Marvin entry - {row['Author']}: {row['Title']}.\nPlease manually create."
|
||||
|
@ -43,13 +53,10 @@ with open(fpath) as f:
|
|||
continue
|
||||
note_file = papis.commands.list.run(notes=True, documents=documents)
|
||||
if not note_file:
|
||||
print(f"Found reference entry but no note file for - {row['Author']}: {row['Title']}.")
|
||||
if (
|
||||
input(
|
||||
f"Create note file now? [y/N] "
|
||||
print(
|
||||
f"Found reference entry but no note file for - {row['Author']}: {row['Title']}."
|
||||
)
|
||||
== "y"
|
||||
):
|
||||
if input(f"Create note file now? [y/N] ") == "y":
|
||||
output = subprocess.run(
|
||||
["papis", "edit", "-n"], capture_output=True, shell=True
|
||||
)
|
||||
|
@ -60,22 +67,15 @@ with open(fpath) as f:
|
|||
continue
|
||||
note_file = str(note_file[0])
|
||||
|
||||
text = f"> {row['HighlightText']}"
|
||||
if row["EntryText"]:
|
||||
if text:
|
||||
text += "\n"
|
||||
else:
|
||||
text = "> "
|
||||
text += f"{row['EntryText']}"
|
||||
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 notes
|
||||
for f, entries in notes.items():
|
||||
# write to notes
|
||||
for f, entries in notes.items():
|
||||
if f:
|
||||
with open(f, "a") as note:
|
||||
print(f"Editing {f}...")
|
||||
|
@ -86,3 +86,14 @@ for f, entries in notes.items():
|
|||
note.write(f"{entry}\n\n")
|
||||
num_added += 1
|
||||
print(f"Added {num_added} entries to it.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
fpath = (
|
||||
sys.argv[1]
|
||||
if len(sys.argv) > 1
|
||||
else "/home/marty/Nextcloud/Personal/Backups/Journal.csv"
|
||||
)
|
||||
db = papis.database.get()
|
||||
|
||||
main(fpath, db)
|
||||
|
|
Loading…
Reference in a new issue