From 649537deca8a99bc5f730648a1278e0164e6bbaa Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 10 Oct 2020 23:43:12 +0100 Subject: [PATCH] my.notes.orgmode: make a bit more iterative --- my/notes/orgmode.py | 14 +++++++++----- tests/orgmode.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/my/notes/orgmode.py b/my/notes/orgmode.py index 117f462..dd33fe7 100644 --- a/my/notes/orgmode.py +++ b/my/notes/orgmode.py @@ -35,20 +35,24 @@ def org_files(roots=config.roots, archived: bool=False) -> Iterator[Path]: class PorgAll: # TODO *roots? def __init__(self, roots: Sequence[PathIsh]) -> None: - self.files = org_files(roots=roots) + self.roots = roots + + @property + def files(self): + yield from org_files(roots=self.roots) def xpath_all(self, query: str): return self.query_all(lambda x: x.xpath_all(query)) + # TODO very confusing names... + # TODO careful... maybe use orgparse iterate instead?? ugh. def get_all(self): - return self.xpath_all('/') + return self.xpath_all('//org') def query_all(self, query): - res: List[Org] = [] for of in self.files: org = Org.from_file(str(of)) - res.extend(query(org)) - return res + yield from query(org) def query(): diff --git a/tests/orgmode.py b/tests/orgmode.py index f628211..2675d0d 100644 --- a/tests/orgmode.py +++ b/tests/orgmode.py @@ -3,5 +3,5 @@ import my.notes.orgmode as orgmode def test(): # meh - results = orgmode.query().query_all(lambda x: x.with_tag('python')) + results = list(orgmode.query().query_all(lambda x: x.with_tag('python'))) assert len(results) > 5