-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
84 lines (67 loc) · 2.59 KB
/
helpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sublime
from typing import Callable, Any, Literal, TypeVar, cast
from functools import wraps, reduce
from types import MappingProxyType
import operator
import json
import subprocess
import logging
Prev = TypeVar("Prev")
New = TypeVar("New")
def settings_watcher(prop_selector: str):
prop_getter = tuple(map(operator.itemgetter, prop_selector.split(".")))
app_settings: Any = None
def decorator(fn: Callable[[Prev, New], Any]):
saved_value: Any = None
def setup_app_settings():
nonlocal app_settings
app_settings = sublime.load_settings(f"{__package__}.sublime-settings")
@wraps(fn)
def wrapper():
nonlocal saved_value
if app_settings is None:
setup_app_settings()
try:
new_value = reduce(lambda setting, f: f(setting), prop_getter, app_settings)
if saved_value is None or new_value != saved_value:
previous_value = saved_value
saved_value = new_value
return fn(new_value, previous_value)
except KeyError:
logging.getLogger(__package__).exception(f"Unable to read {prop_selector}")
if saved_value is not None:
return fn(saved_value, saved_value)
return wrapper
return decorator
colorSchemeMap = MappingProxyType({1: "dark", 2: "light"})
def parse_dbus_call(output: str) -> Literal["dark", "light"]:
result = json.loads(output)
system_scheme: Literal[1, 2] = result["data"][0]["data"]
return cast(Any, colorSchemeMap[system_scheme])
def parse_dbus_monitor(output: str) -> Literal["dark", "light"]:
result = json.loads(output)
system_scheme: Literal[1, 2] = result["payload"]["data"][2]["data"]
return cast(Any, colorSchemeMap[system_scheme])
def read_system_theme():
try:
output = subprocess.check_output(
[
"/usr/bin/busctl",
"--user",
"--json=short",
"call",
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings",
"ReadOne",
"ss",
"org.freedesktop.appearance",
"color-scheme",
],
universal_newlines=True,
stderr=subprocess.STDOUT,
)
return parse_dbus_call(output)
except subprocess.SubprocessError:
logging.getLogger(__package__).exception("Unable to read theme portal settings")
return None