Repository navigation
Expand file tree
/
Copy pathengine_support.py
More file actions
95 lines (72 loc) · 3.12 KB
/
Copy pathengine_support.py
File metadata and controls
95 lines (72 loc) · 3.12 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
from __future__ import annotations
from dataclasses import dataclass
from host_commands import host_which
from i18n import get_app_config
@dataclass(frozen=True)
class EngineDefinition:
id: int
name: str
command: str
@property
def command_lower(self) -> str:
return (self.command or '').strip().lower()
@property
def is_firefox(self) -> bool:
return 'firefox' in self.command_lower
@property
def is_chromium_family(self) -> bool:
return 'chrome' in self.command_lower or 'chromium' in self.command_lower
@property
def supports_adblock(self) -> bool:
return self.is_firefox
@property
def supports_background_mode(self) -> bool:
return self.is_firefox
def _command_candidates(command: str) -> list[str]:
command = (command or '').strip().lower()
candidates: list[str] = []
if command:
candidates.append(command)
if 'firefox' in command:
candidates.extend(['firefox', 'firefox-esr'])
elif 'chrome' in command:
candidates.extend(['google-chrome', 'google-chrome-stable', 'chrome', 'chromium', 'chromium-browser'])
elif 'chromium' in command:
candidates.extend(['chromium', 'chromium-browser', 'google-chrome', 'google-chrome-stable', 'chrome'])
seen: set[str] = set()
unique: list[str] = []
for candidate in candidates:
if candidate and candidate not in seen:
seen.add(candidate)
unique.append(candidate)
return unique
def engine_available(engine: dict | EngineDefinition) -> bool:
command = engine.command if isinstance(engine, EngineDefinition) else engine.get('command', '')
return any(host_which(candidate) for candidate in _command_candidates(command))
def configured_engines() -> list[EngineDefinition]:
config = get_app_config()
raw_engines = config.get('engines', []) or [
{'id': 1, 'name': 'Firefox', 'command': 'firefox'},
{'id': 2, 'name': 'Chrome', 'command': 'google-chrome'},
]
return [EngineDefinition(int(engine['id']), engine['name'], engine.get('command', '')) for engine in raw_engines]
_AVAILABLE_ENGINES_CACHE: list[dict] | None = None
def available_engines() -> list[dict]:
global _AVAILABLE_ENGINES_CACHE
if _AVAILABLE_ENGINES_CACHE is None:
_AVAILABLE_ENGINES_CACHE = [engine.__dict__.copy() for engine in configured_engines() if engine_available(engine)]
return [dict(engine) for engine in _AVAILABLE_ENGINES_CACHE]
def engine_icon_name(engine_name: str) -> str:
engine_name = (engine_name or '').lower()
if 'firefox' in engine_name:
return 'firefox'
if 'chrome' in engine_name:
return 'google-chrome'
if 'chromium' in engine_name:
return 'chromium-browser'
return 'applications-internet-symbolic'
# Single shared snapshot of the available engines. The previous pattern declared
# ``ENGINES = available_engines()`` at the top of 8 different modules; since the
# underlying lookup is cached and the result is never mutated, one module-level
# constant imported everywhere is equivalent and keeps the source of truth here.
ENGINES = available_engines()