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

slackoff.browser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 29
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A close() 0 3 1
A detect() 0 12 5
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