From 88f3c17c2726d9c2449043dc8599413892b0325d Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Thu, 15 Aug 2024 18:44:12 +0300 Subject: [PATCH] core.common: move mime-related stuff to my.core.mime no backward compat, unlikely it was used by anyone else --- my/core/common.py | 22 ---------------------- my/core/mime.py | 34 ++++++++++++++++++++++++++++++++++ my/photos/main.py | 3 ++- 3 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 my/core/mime.py diff --git a/my/core/common.py b/my/core/common.py index 926861d..32e277b 100644 --- a/my/core/common.py +++ b/my/core/common.py @@ -2,7 +2,6 @@ from glob import glob as do_glob from pathlib import Path from datetime import datetime from dataclasses import is_dataclass, asdict as dataclasses_asdict -import functools import os from typing import ( Any, @@ -117,27 +116,6 @@ def get_files( return tuple(paths) -@functools.lru_cache(1) -def _magic(): - import magic # type: ignore - return magic.Magic(mime=True) - - -# TODO could reuse in pdf module? -import mimetypes # todo do I need init()? -# todo wtf? fastermime thinks it's mime is application/json even if the extension is xz?? -# whereas magic detects correctly: application/x-zstd and application/x-xz -def fastermime(path: PathIsh) -> str: - paths = str(path) - # mimetypes is faster - (mime, _) = mimetypes.guess_type(paths) - if mime is not None: - return mime - # magic is slower but returns more stuff - # TODO Result type?; it's kinda racey, but perhaps better to let the caller decide? - return _magic().from_file(paths) - - Json = Dict[str, Any] diff --git a/my/core/mime.py b/my/core/mime.py new file mode 100644 index 0000000..c8abc7e --- /dev/null +++ b/my/core/mime.py @@ -0,0 +1,34 @@ +""" +Utils for mime/filetype handling +""" + +from .common import assert_subpackage; assert_subpackage(__name__) + +import functools + +from .common import PathIsh + + +@functools.lru_cache(1) +def _magic(): + import magic # type: ignore + + # TODO also has uncompess=True? could be useful + return magic.Magic(mime=True) + + +# TODO could reuse in pdf module? +import mimetypes # todo do I need init()? + + +# todo wtf? fastermime thinks it's mime is application/json even if the extension is xz?? +# whereas magic detects correctly: application/x-zstd and application/x-xz +def fastermime(path: PathIsh) -> str: + paths = str(path) + # mimetypes is faster, so try it first + (mime, _) = mimetypes.guess_type(paths) + if mime is not None: + return mime + # magic is slower but handles more types + # TODO Result type?; it's kinda racey, but perhaps better to let the caller decide? + return _magic().from_file(paths) diff --git a/my/photos/main.py b/my/photos/main.py index 622d475..6262eac 100644 --- a/my/photos/main.py +++ b/my/photos/main.py @@ -15,9 +15,10 @@ from typing import Optional, NamedTuple, Iterator, Iterable, List from geopy.geocoders import Nominatim # type: ignore -from my.core.common import LazyLogger, fastermime +from my.core import LazyLogger from my.core.error import Res, sort_res_by from my.core.cachew import cache_dir, mcachew +from my.core.mime import fastermime from my.config import photos as config # type: ignore[attr-defined]