Skip to content

Commit

Permalink
use bundled find_executable
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Apr 22, 2024
1 parent 944ecea commit 52ee491
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ajt_common
Submodule ajt_common updated 1 files
+56 −0 utils.py
17 changes: 14 additions & 3 deletions helpers/file_ops.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# 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]


Expand Down Expand Up @@ -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__):
Expand All @@ -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(
Expand All @@ -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()
8 changes: 6 additions & 2 deletions helpers/goldendict_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 52ee491

Please sign in to comment.