From 52ee491a271d6281fa7c59e57a9bd65a09ce9c71 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Mon, 22 Apr 2024 11:37:29 +0300 Subject: [PATCH] use bundled find_executable --- ajt_common | 2 +- helpers/file_ops.py | 17 ++++++++++++++--- helpers/goldendict_lookups.py | 8 ++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ajt_common b/ajt_common index 9b070f8..3dd9dee 160000 --- a/ajt_common +++ b/ajt_common @@ -1 +1 @@ -Subproject commit 9b070f8ec8bfaabe582738e1199d2f726558d674 +Subproject commit 3dd9deefeb1c12639134d71e1975b02269963dfe diff --git a/helpers/file_ops.py b/helpers/file_ops.py index 46d98d0..d92d188 100644 --- a/helpers/file_ops.py +++ b/helpers/file_ops.py @@ -1,12 +1,18 @@ # Copyright: Ren Tatsumoto # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +import functools import os import subprocess from collections.abc import Iterable from anki.utils import no_bundled_libs +try: + from ..ajt_common.utils import find_executable +except ImportError: + from ajt_common.utils import find_executable + THIS_ADDON_MODULE = __name__.split(".")[0] @@ -35,6 +41,7 @@ def find_config_json() -> str: return path +@functools.cache def user_files_dir() -> str: """Return path to the user files directory.""" for parent_dir in walk_parents(__file__): @@ -47,7 +54,6 @@ def open_file(path: str) -> None: Select file in lf, the preferred terminal file manager, or open it with xdg-open. """ from aqt.qt import QDesktopServices, QUrl - from distutils.spawn import find_executable if (terminal := os.getenv("TERMINAL")) and (lf := (os.getenv("FILE") or find_executable("lf"))): subprocess.Popen( @@ -66,6 +72,11 @@ def open_file(path: str) -> None: QDesktopServices.openUrl(QUrl(f"file://{path}")) +def main(): + print("config", find_config_json()) + print("user files", user_files_dir()) + print("open file", open_file("/etc/hosts")) + + if __name__ == "__main__": - print(user_files_dir()) - print(open_file("/etc/hosts")) + main() diff --git a/helpers/goldendict_lookups.py b/helpers/goldendict_lookups.py index 839ea52..b989f0b 100644 --- a/helpers/goldendict_lookups.py +++ b/helpers/goldendict_lookups.py @@ -4,17 +4,21 @@ import functools import os import subprocess -from distutils.spawn import find_executable from typing import Optional from anki.utils import is_mac +try: + from ..ajt_common.utils import find_executable +except ImportError: + from ajt_common.utils import find_executable + GD_PROGRAM_NAME = "GoldenDict-NG" GD_MACOS_PATH = "/Applications/GoldenDict.app/Contents/MacOS/GoldenDict" def find_goldendict_fallback() -> Optional[str]: - return is_mac and os.path.isfile(GD_MACOS_PATH) and GD_MACOS_PATH or None + return GD_MACOS_PATH if (is_mac and os.path.isfile(GD_MACOS_PATH)) else None @functools.cache