diff --git a/src/promnesia/common.py b/src/promnesia/common.py index 721ea7c8..6532333b 100644 --- a/src/promnesia/common.py +++ b/src/promnesia/common.py @@ -76,13 +76,26 @@ def file(cls, path: PathIsh, line: Optional[int]=None, relative_to: Optional[Pat # but generally, it will be # (url|file)(linenumber|json_path|anchor) + +@lru_cache(None) +def warn_once(message: str) -> None: + # you'd think that warnings module already logs warnings only once per line.. + # but sadly it's not the case + # see https://github.com/karlicoss/python_duplicate_warnings_investigation/blob/master/test.py + warnings.warn(message, stacklevel=2) + + +def _warn_no_xdg_mime() -> None: + warn_once("No xdg-mime on your OS! If you're on OSX, perhaps you can help me! https://github.com/karlicoss/open-in-editor/issues/1") + + @lru_cache(1) def _detect_mime_handler() -> str: def exists(what: str) -> bool: try: r = run(f'xdg-mime query default x-scheme-handler/{what}'.split(), stdout=PIPE) except (FileNotFoundError, NotADirectoryError): # ugh seems that osx might throw NotADirectory for some reason - warnings.warn("No xdg-mime on your OS! If you're on OSX, perhaps you can help me! https://github.com/karlicoss/open-in-editor/issues/1") + _warn_no_xdg_mime() return False if r.returncode > 0: warnings.warn('xdg-mime failed') # hopefully rest is in stderr @@ -102,6 +115,7 @@ def exists(what: str) -> bool: result = 'emacs:' # 2. now try to use newer editor:// thing + # TODO flip order here? should rely on editor:// first? # TODO would be nice to collect warnings and display at the end if not exists('editor'): diff --git a/src/promnesia/sources/auto.py b/src/promnesia/sources/auto.py index 81c019dd..81b90f4b 100644 --- a/src/promnesia/sources/auto.py +++ b/src/promnesia/sources/auto.py @@ -22,6 +22,7 @@ import pytz from ..common import Visit, Url, PathIsh, get_logger, Loc, get_tmpdir, extract_urls, Extraction, Result, Results, mime, traverse, file_mtime, echain, logger +from ..common import warn_once from ..config import use_cores @@ -167,7 +168,7 @@ def _org(path: Path) -> Results: Replacer = Optional[Callable[[str, str], str]] def index( - *paths: Union[PathIsh], + *paths: PathIsh, ignored: Union[Sequence[str], str]=(), follow: bool=True, replacer: Replacer=None, @@ -282,6 +283,8 @@ def by_path(pp: Path) -> Tuple[Optional[Ex], Optional[Mime]]: def _index_file(pp: Path, opts: Options) -> Results: logger = get_logger() + # TODO need to keep debug logs here... + # logger.info(f"indexing {pp}") # TODO use kompress? # TODO not even sure if it's used... suf = pp.suffix.lower() @@ -307,10 +310,9 @@ def _index_file(pp: Path, opts: Options) -> Results: ip, pm = by_path(pp) if ip is None: - # TODO use warning (with mime/ext as key?) - # TODO only log once? # hmm.. + # todo not really sure about using warnings vs yielding error here? msg = f'No extractor for suffix {suf}, mime {pm}' - warnings.warn(msg) + warn_once(msg) yield echain(ex, RuntimeError(msg)) return diff --git a/src/promnesia/sources/plaintext.py b/src/promnesia/sources/plaintext.py index 54f1594b..99a4c21f 100644 --- a/src/promnesia/sources/plaintext.py +++ b/src/promnesia/sources/plaintext.py @@ -98,8 +98,10 @@ def extract_from_path(path: PathIsh) -> Command: '.gz', '.zip', )): - logger.info(f"Extracting from compressed file {path}") + # todo should be debug? + # or should delete it completely, feels like unpacking archives here is a bit too much raise RuntimeError(f"Archives aren't supported yet: {path}") + logger.info(f"Extracting from compressed file {path}") import lzma from tempfile import NamedTemporaryFile # TODO hopefully, no collisions