Add automatic import for missing entries
This commit is contained in:
parent
32d57c79d3
commit
cb1cfe22ab
1 changed files with 58 additions and 50 deletions
108
papis-marvin
108
papis-marvin
|
@ -7,16 +7,23 @@
|
|||
# resulting file.
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import logging
|
||||
import subprocess
|
||||
from typing import Dict, List
|
||||
import papis.api
|
||||
import papis.pick
|
||||
import papis.format
|
||||
import papis.commands.edit
|
||||
import papis.config
|
||||
import papis.commands.list
|
||||
import papis.commands.add
|
||||
import papis.config
|
||||
import papis.database
|
||||
import re
|
||||
import subprocess
|
||||
import isbnlib
|
||||
import papis.isbn
|
||||
|
||||
logger = logging.getLogger("marvin")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def main(fpath, db):
|
||||
|
@ -33,24 +40,13 @@ def get_all_annotations(db, csv) -> Dict:
|
|||
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."
|
||||
)
|
||||
title_stripped = strip_string(row["Title"])
|
||||
doc = get_document(db, row["Author"], title_stripped)
|
||||
if not doc:
|
||||
continue
|
||||
# TODO warn user/ let him pick with picker if multiple docs found
|
||||
doc = documents[0]
|
||||
note_file = get_notefile(doc)
|
||||
if not note_file:
|
||||
print(
|
||||
f"Found reference entry but no note file for - {row['Author']}: {title_stripped}."
|
||||
)
|
||||
note_file = create_note_file(db, doc)
|
||||
note_file = get_notefile(db, doc)
|
||||
|
||||
text = format_entry(row)
|
||||
|
||||
|
@ -61,36 +57,48 @@ def get_all_annotations(db, csv) -> Dict:
|
|||
return notes
|
||||
|
||||
|
||||
def get_documents(db, author, title) -> List:
|
||||
return db.query(f"author:({author}) title:({title})")
|
||||
# TODO warn user/ let him pick with picker if multiple docs found
|
||||
def get_document(db, author, title):
|
||||
res = db.query(f"author:({author}) title:({title})")
|
||||
if not res:
|
||||
add_to_database(author, title)
|
||||
res = db.query(f"author:({author}) title:({title})")
|
||||
if not res:
|
||||
logger.warning(
|
||||
f"Nothing found for {author}: {title}.\nPlease create manually."
|
||||
)
|
||||
return res[0]
|
||||
return res[0]
|
||||
|
||||
|
||||
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])
|
||||
|
||||
|
||||
def create_note_file(db, document):
|
||||
if input(f"Create note file now? [y/N] ") == "y":
|
||||
if not document.has("notes"):
|
||||
notes_name = papis.config.getstring("notes-name")
|
||||
document["notes"] = papis.format.format(notes_name, document)
|
||||
document.save()
|
||||
db.update(document)
|
||||
|
||||
notes_path = os.path.join(
|
||||
str(document.get_main_folder()),
|
||||
document["notes"]
|
||||
)
|
||||
|
||||
if not os.path.exists(notes_path):
|
||||
# TODO reimplement logger: logger.debug("Creating '%s'", notes_path)
|
||||
papis.commands.edit.create_notes(document, notes_path)
|
||||
return notes_path
|
||||
def add_to_database(author, title, confirm=True, edit=False):
|
||||
print(f"Searching - '{title} {author}'")
|
||||
data = None
|
||||
try:
|
||||
data = papis.isbn.get_data(f"{title}")
|
||||
except isbnlib.ISBNLibException as e:
|
||||
print(f"ERROR: {e}")
|
||||
else:
|
||||
return
|
||||
print(f"Found: {data}")
|
||||
if data:
|
||||
papis_data = papis.isbn.data_to_papis(data[0])
|
||||
papis.commands.add.run([], data=papis_data, confirm=confirm, edit=edit)
|
||||
|
||||
|
||||
def get_notefile(db, document) -> str | None:
|
||||
if not document.has("notes"):
|
||||
notes_name = papis.config.getstring("notes-name")
|
||||
document["notes"] = papis.format.format(notes_name, document)
|
||||
document.save()
|
||||
db.update(document)
|
||||
|
||||
notes_path = os.path.join(str(document.get_main_folder()), document["notes"])
|
||||
print(f"{document['title']}: {document['notes']}")
|
||||
|
||||
if not os.path.exists(notes_path):
|
||||
# TODO reimplement logger: logger.debug("Creating '%s'", notes_path)
|
||||
papis.commands.edit.create_notes(document, notes_path)
|
||||
return notes_path
|
||||
|
||||
|
||||
# TODO implement custom formatting (akin to pubs-extract)
|
||||
|
@ -123,21 +131,21 @@ def write_to_files(notes: Dict):
|
|||
for f, entries in notes.items():
|
||||
if f:
|
||||
with open(f, "a") as note:
|
||||
print(f"Editing {f}...")
|
||||
logger.info(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.")
|
||||
logger.info(f"Added {num_added} entries to it.")
|
||||
|
||||
|
||||
title_strip_pattern = re.compile(r"([^\s\w]|_)+")
|
||||
strip_pattern = re.compile(r"([^\s\w]|_)+")
|
||||
|
||||
|
||||
def strip_book_title(title) -> str:
|
||||
return title_strip_pattern.sub("", title)
|
||||
def strip_string(title) -> str:
|
||||
return strip_pattern.sub("", title)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in a new issue