From e5165246e0a9a76c5e334778dd9a0b29a903c1c7 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Thu, 11 Apr 2019 17:34:00 +0100 Subject: [PATCH] better org notes retrieval --- notes/__init__.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/notes/__init__.py b/notes/__init__.py index e67dc9e..f05c894 100644 --- a/notes/__init__.py +++ b/notes/__init__.py @@ -1,8 +1,12 @@ from glob import glob -from typing import List +from typing import List, Sequence +from pathlib import Path + +from kython.ktyping import PathIsh from porg import Org # type: ignore + # TODO enc stuff? def get_org_paths(): return [ @@ -10,7 +14,14 @@ def get_org_paths(): '***REMOVED***', ] -def _get_org_files_in(path, archived: bool=False) -> List[str]: +def _get_org_files_in(path, archived: bool=False) -> List[PathIsh]: + ppp = Path(path) + assert ppp.exists() + # TODO try/catch?? + if ppp.is_file(): + return [ppp] + + path = str(path) # TODO FIXME use pathlib res = [] res.extend(glob(path + '/**/*.org', recursive=True)) if archived: @@ -18,7 +29,7 @@ def _get_org_files_in(path, archived: bool=False) -> List[str]: return res -def get_org_files(archived: bool = False) -> List[str]: +def get_org_files(archived: bool = False) -> List[PathIsh]: res = [] for p in get_org_paths(): res.extend(_get_org_files_in(p, archived=archived)) @@ -27,11 +38,14 @@ def get_org_files(archived: bool = False) -> List[str]: # TODO move to porg? class PorgAll: - def __init__(self, paths: List[str]) -> None: - self.paths = paths + def __init__(self, paths: Sequence[PathIsh]) -> None: + self.paths = [Path(p) for p in paths] + + def xpath_all(self, query: str): + return self.query_all(lambda x: x.xpath_all(query)) def get_all(self): - return self.query_all(lambda x: x.xpath_all('//*')) + return self.xpath_all('//*') def query_all(self, query): res: List[Org] = []