-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
63 lines (43 loc) · 1.65 KB
/
utils.py
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
# 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 shutil
from typing import Callable, Optional, Union
from aqt.qt import pyqtBoundSignal, pyqtSignal
def ui_translate(key: str) -> str:
return key.capitalize().replace("_", " ").replace("Html", "HTML").replace("Svg", "SVG").replace("Url", "URL")
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) -> Optional[str]:
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) -> Optional[str]:
"""
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: Union[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 main():
print("distutils", shutil.which("anki"))
print("hardcoded", find_executable_hardcoded("anki"))
print("all", find_executable("anki"))
if __name__ == "__main__":
main()