Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
102 lines (78 loc) · 2.83 KB
/
Copy pathutils.py
File metadata and controls
102 lines (78 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import functools
import os
import pathlib
import shutil
import subprocess
from collections.abc import Callable
from anki.utils import no_bundled_libs
from aqt.qt import pyqtBoundSignal, pyqtSignal
def ui_translate(key: str) -> str:
"""Turn a configuration key into a label while preserving common technical abbreviations."""
return (
key.capitalize()
.replace("_", " ")
.replace("Html", "HTML")
.replace("Svg", "SVG")
.replace("Url", "URL")
.replace("Css", "CSS")
)
HARDCODED_PATHS = (
"/usr/bin",
"/opt/homebrew/bin",
"/usr/local/bin",
"/bin",
os.path.join(os.getenv("HOME", "/home/user"), ".local", "bin"),
)
def find_executable_hardcoded(name: str) -> str | None:
"""Return the first existing fallback executable path, or None when no candidate exists."""
for path_to_dir in HARDCODED_PATHS:
if os.path.isfile(path_to_exe := os.path.join(path_to_dir, name)):
return path_to_exe
return None
@functools.cache
def find_executable(name: str) -> str | None:
"""
If possible, use the executable installed in the system.
Otherwise, try fallback paths.
"""
return shutil.which(name) or find_executable_hardcoded(name)
def clamp(min_val: int, val: int, max_val: int) -> int:
return max(min_val, min(val, max_val))
MISSING = object()
def q_emit(signal: Callable | pyqtSignal | pyqtBoundSignal, value=MISSING) -> None:
"""Helper to work around type checking not working with signal.emit(func)."""
if value is not MISSING:
signal.emit(value) # type: ignore
else:
signal.emit() # type: ignore
def open_file(path: str | pathlib.Path) -> None:
"""
Select file in lf, the preferred terminal file manager, or open it with xdg-open.
"""
from aqt.qt import QDesktopServices, QUrl
terminal = os.getenv("TERMINAL") or find_executable("i3-sensible-terminal")
lf = os.getenv("FILE") or find_executable("lf")
if terminal and lf:
subprocess.Popen(
[terminal, "-e", lf, path],
shell=False,
start_new_session=True,
)
elif opener := find_executable("xdg-open"):
subprocess.Popen(
[opener, f"file://{path}"],
shell=False,
start_new_session=True,
)
else:
with no_bundled_libs():
QDesktopServices.openUrl(QUrl(f"file://{path}"))
def main() -> None:
"""Print executable-discovery results for the standalone diagnostic entry point."""
print("distutils", shutil.which("anki"))
print("hardcoded", find_executable_hardcoded("anki"))
print("all", find_executable("anki"))
if __name__ == "__main__":
main()