Refactor annotations to list of paper,annotations tuples

This commit is contained in:
Marty Oehme 2022-12-22 18:32:11 +01:00
parent c5a9c7dc53
commit 6569d0b05d
Signed by: Marty
GPG key ID: 73BA40D5AFAF49C9

View file

@ -14,7 +14,7 @@ from ...events import PaperChangeEvent, PostCommandEvent
from ... import repo from ... import repo
from ...utils import resolve_citekey_list from ...utils import resolve_citekey_list
from ...filebroker import DocBroker from ...content import write_file
class ExtractPlugin(PapersPlugin): class ExtractPlugin(PapersPlugin):
@ -33,9 +33,9 @@ class ExtractPlugin(PapersPlugin):
def __init__(self, conf, ui): def __init__(self, conf, ui):
pass pass
self.ui = ui self.ui = ui
self.pubsdir = os.path.expanduser(conf["main"]["pubsdir"])
self.broker = DocBroker(self.pubsdir)
self.repository = repo.Repository(conf) self.repository = repo.Repository(conf)
self.pubsdir = os.path.expanduser(conf["main"]["pubsdir"])
self.broker = self.repository.databroker
self.quiet = conf["plugins"].get("extract", {}).get("quiet", False) self.quiet = conf["plugins"].get("extract", {}).get("quiet", False)
# self.manual = conf['plugins'].get('git', {}).get('manual', False) # self.manual = conf['plugins'].get('git', {}).get('manual', False)
# self.force_color = conf['plugins'].get('git', {}).get('force_color', True) # self.force_color = conf['plugins'].get('git', {}).get('force_color', True)
@ -66,16 +66,13 @@ class ExtractPlugin(PapersPlugin):
self.to_stdout(all_annotations) self.to_stdout(all_annotations)
def extract(self, papers): def extract(self, papers):
papers_annotated: Dict = {} papers_annotated = []
for paper in papers: for paper in papers:
file = self.get_file(paper) file = self.get_file(paper)
try: try:
papers_annotated[paper.citekey] = self.get_annotations(file) papers_annotated.append((paper, self.get_annotations(file)))
except fitz.FileDataError as e: except fitz.FileDataError as e:
print(f"ERROR: Document {file} is broken: {e}") print(f"ERROR: Document {file} is broken: {e}")
if not self.quiet:
# TODO find pretty print functionality
self.ui.info(f"Extracted annotations from {paper.citekey}")
return papers_annotated return papers_annotated
def gather_papers(self, citekeys): def gather_papers(self, citekeys):
@ -103,11 +100,13 @@ class ExtractPlugin(PapersPlugin):
return annotations return annotations
def to_stdout(self, annotated_papers): def to_stdout(self, annotated_papers):
for citekey, annotations in annotated_papers.items(): for contents in annotated_papers:
paper = contents[0]
annotations = contents[1]
if annotations: if annotations:
print(f"{citekey}") print(f"{paper.citekey}")
for annot in annotations: for annot in annotations:
print(f"> \"{annot}\"") print(f'> "{annot}"')
print("") print("")