From 0c10e5e8ae039e83cc91f8a4aab9240ba9fe19ae Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sun, 12 May 2024 14:38:20 +0800 Subject: [PATCH] refactor: use more-itertools Signed-off-by: Jack Cherng --- plugin/constants.py | 5 +++++ plugin/listener.py | 7 +++---- plugin/utils.py | 36 ------------------------------------ 3 files changed, 8 insertions(+), 40 deletions(-) create mode 100644 plugin/constants.py delete mode 100644 plugin/utils.py diff --git a/plugin/constants.py b/plugin/constants.py new file mode 100644 index 0000000..aa5ae3e --- /dev/null +++ b/plugin/constants.py @@ -0,0 +1,5 @@ +from __future__ import annotations + +assert __package__ + +PLUGIN_NAME = __package__.partition(".")[0] diff --git a/plugin/listener.py b/plugin/listener.py index 3e92c67..76c42fa 100644 --- a/plugin/listener.py +++ b/plugin/listener.py @@ -6,12 +6,11 @@ import sublime import sublime_plugin +from more_itertools import first_true +from .constants import PLUGIN_NAME from .libs.LnkParse3.extra.metadata import Metadata, SerializedPropertyStorage, SerializedPropertyValueIntegerName from .libs.LnkParse3.lnk_file import LnkFile -from .utils import first_true - -PACKAGE_NAME = __package__.partition(".")[0] class FollowLnkViewEventListener(sublime_plugin.ViewEventListener): @@ -42,7 +41,7 @@ def on_load(self) -> None: self.view.close() return - raise RuntimeError(f"[{PACKAGE_NAME}] Uh, what's this: {path = }; {target_path = }") + raise RuntimeError(f"[{PLUGIN_NAME}] Uh, what's this: {path = }; {target_path = }") @classmethod def _resolve_lnk(cls, path: str) -> str | None: diff --git a/plugin/utils.py b/plugin/utils.py deleted file mode 100644 index b14205c..0000000 --- a/plugin/utils.py +++ /dev/null @@ -1,36 +0,0 @@ -# This file is more self-sustained and shouldn't use things from other higher-level modules. -from __future__ import annotations - -from collections.abc import Iterable -from typing import Callable, TypeVar, overload - -_T = TypeVar("_T") -_U = TypeVar("_U") - - -@overload -def first_true( - items: Iterable[_T], - default: _U, - pred: Callable[[_T], bool] | None = None, -) -> _T | _U: ... - - -@overload -def first_true( - items: Iterable[_T], - *, - pred: Callable[[_T], bool] | None = None, -) -> _T | None: ... - - -def first_true( - items: Iterable[_T], - default: _U | None = None, - pred: Callable[[_T], bool] | None = None, -) -> _T | _U | None: - """ - Gets the first item which satisfies the `pred`. Otherwise, `default`. - If `pred` is not given or `None`, the first truthy item will be returned. - """ - return next(filter(pred, items), default)