Skip to content

Commit

Permalink
general: work around the issue when warnings module sometimes logs th…
Browse files Browse the repository at this point in the history
…e same warning multiple times

see python/cpython#73858 (comment)
  • Loading branch information
karlicoss committed Aug 25, 2023
1 parent c8056d9 commit 78e7d73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/promnesia/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'):
Expand Down
10 changes: 6 additions & 4 deletions src/promnesia/sources/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/promnesia/sources/plaintext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78e7d73

Please sign in to comment.