Completed
Push — main ( 3b3612...994b6d )
by Jace
25s queued 12s
created

slackoff.browser.detect()   A

Complexity

Conditions 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.3333
c 0
b 0
f 0
cc 5
nop 1
1
import plistlib
2
from pathlib import Path
3
4
import log
5
6
from . import script
7
8
FUNCTIONS = Path(__file__).parent / "browser.applescript"
9
PREFERENCES = (
10
    Path.home()
11
    / "Library"
12
    / "Preferences"
13
    / "com.apple.LaunchServices/com.apple.launchservices.secure.plist"
14
)
15
16
NAMES = {
17
    "com.apple.safari": "Safari",
18
    "com.google.chrome": "Google Chrome",
19
    "org.mozilla.firefox": "Firefox",
20
}
21
DEFAULT = NAMES["com.google.chrome"]
22
23
24
def detect(data: dict | None = None) -> str:
25
    if data is None:
26
        with PREFERENCES.open("rb") as fp:
27
            data = plistlib.load(fp)
28
29
    for handler in data.get("LSHandlers", []):
30
        if handler.get("LSHandlerURLScheme") == "http":
31
            role = handler["LSHandlerRoleAll"]
32
            return NAMES[role]
33
34
    log.warn("Unable to determine the default browser")
35
    return DEFAULT
36
37
38
def close(name: str = "") -> bool:
39
    replacements = {DEFAULT: name or detect()}
40
    return script.call(FUNCTIONS, "close()", replacements)
41