Skip to content

Commit 6c393c9

Browse files
committed
Add xdg-settings handling
1 parent f58ef2f commit 6c393c9

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

Makefile

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@ if ! type pip &> /dev/null; then
77
opkg update
88
opkg install python3-pip
99
fi
10-
pip install /tmp/liboxide-0.0.1.tar.gz
10+
pip install --force-reinstall /tmp/liboxide-0.0.1-py3-none-any.whl
1111
endef
1212
export SCRIPT
1313

14-
dist/liboxide-0.0.1.tar.gz:
14+
dist/liboxide-0.0.1.tar.gz: $(shell find liboxide -type f)
1515
python -m build --sdist
1616

17+
dist/liboxide-0.0.1-py3-none-any.whl: $(shell find liboxide -type f)
18+
python -m build --wheel
19+
1720
clean:
1821
git clean --force -dX
1922

20-
/tmp/liboxide-0.0.1.tar.gz: dist/liboxide-0.0.1.tar.gz
21-
rsync dist/liboxide-0.0.1.tar.gz [email protected]:/tmp
23+
deploy: dist/liboxide-0.0.1-py3-none-any.whl
24+
rsync dist/liboxide-0.0.1-py3-none-any.whl [email protected]:/tmp
2225

23-
install: /tmp/liboxide-0.0.1.tar.gz
26+
install: deploy
2427
echo -e "$$SCRIPT" | ssh [email protected] bash -le
2528

2629
test: install
2730
cat test.py | ssh [email protected] /opt/bin/python
2831

29-
.PHONY: clean install test /tmp/liboxide-0.0.1.tar.gz
32+
.PHONY: clean install test deploy

liboxide/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from ._apps import Application
2+
from ._apps import AppsAPI as apps
13
from ._errors import TooFewArguments
24
from ._errors import TooManyArguments
3-
from ._apps import AppsAPI as apps
5+
from ._notification import Notification
46
from ._notification import NotificationAPI as notification
57
from ._power import PowerAPI as power
68
from ._screen import ScreenAPI as screen
9+
from ._screen import Screenshot
710
from ._system import SystemAPI as system
11+
from ._wifi import BSS
12+
from ._wifi import Network
813
from ._wifi import WifiAPI as wifi
14+
from ._xdgsettings import Settings as settings

liboxide/_xdgsettings.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import itertools
2+
import json
3+
import subprocess
4+
5+
6+
class XDGSettingsException(Exception):
7+
pass
8+
9+
10+
class Settings(object):
11+
@classmethod
12+
def get(cls, key, subkey=None):
13+
args = [key]
14+
if subkey is not None:
15+
args.append(subkey)
16+
17+
stdout = subprocess.check_output(["/opt/bin/xdg-settings", "get"] + args)
18+
if stdout:
19+
return json.loads(stdout)
20+
21+
return None
22+
23+
@classmethod
24+
def set(cls, key: tuple[str] | str, val):
25+
if isinstance(key, str):
26+
args = [key]
27+
elif isinstance(key, tuple[str]):
28+
args = list(key)
29+
else:
30+
raise ValueError()
31+
32+
args.append(val)
33+
stdout = subprocess.check_output(["/opt/bin/xdg-settings", "set"] + args)
34+
if stdout:
35+
return json.loads(stdout)
36+
37+
return None
38+
39+
@classmethod
40+
def keys(cls) -> tuple[str]:
41+
stdout = subprocess.check_output(["/opt/bin/xdg-settings", "--list"])
42+
lines = stdout.decode("utf-8").rstrip().split("\n")
43+
res = []
44+
if lines.pop(0) != "Known properties:":
45+
raise XDGSettingsException("Unknown xdg-settings output")
46+
47+
peeker, lines = itertools.tee(lines)
48+
next(peeker)
49+
category = None
50+
for line in lines:
51+
key = line.lstrip().split(" ")[0]
52+
next_line = next(peeker, None)
53+
eof = next_line is None
54+
next_is_indented = not eof and next_line.startswith(" ")
55+
is_indented = line.startswith(" ")
56+
if category is not None and not is_indented:
57+
category = None
58+
59+
if not is_indented and next_is_indented:
60+
category = key
61+
continue
62+
63+
if category is None and not is_indented and (eof or not next_is_indented):
64+
res.append((key, None))
65+
continue
66+
67+
if category is not None and is_indented:
68+
res.append((category, key))
69+
continue
70+
71+
raise XDGSettingsException(
72+
"Unable to parse xdg-settings --list output\n"
73+
f" Current line: {line}\n"
74+
f" Line indented: {is_indented}\n"
75+
f" Next line indented: {next_is_indented}\n"
76+
f" Category: {category}"
77+
)
78+
79+
return res
80+
81+
@classmethod
82+
def items(cls):
83+
for key in cls.keys():
84+
yield (key, cls.get(*key))
85+
86+
@classmethod
87+
def values(cls):
88+
for key in cls.keys():
89+
yield cls.get(*key)

test.py

+6
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@
1212
for notification in liboxide.notification.allNotifications:
1313
app = liboxide.apps.getApplication(notification.application)
1414
print(f" {notification.identifier} owned by {app.displayName}")
15+
16+
path = liboxide.settings.get("apps", "lockscreenApplication")
17+
app = liboxide.Application(path)
18+
print(f"Lockscreen App: {app.displayName}")
19+
autoLock = liboxide.settings.get("autoLock")
20+
print(f"Automatic lock: {autoLock} minutes")

0 commit comments

Comments
 (0)