Skip to content

Commit 59fd30e

Browse files
committed
Client connects to system bus by default
1 parent 7a075bc commit 59fd30e

File tree

5 files changed

+41
-46
lines changed

5 files changed

+41
-46
lines changed

crystalfontz/dbus/bus.py

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,19 @@
1-
import functools
21
import logging
3-
from typing import Optional
42

5-
import click
63
from sdbus import ( # pyright: ignore [reportMissingModuleSource]
74
sd_bus_open_system,
85
sd_bus_open_user,
96
set_default_bus,
107
)
118

12-
from crystalfontz.cli import SyncCommand
13-
149
logger = logging.getLogger(__name__)
1510

16-
BusType = Optional[bool]
17-
BUS_TYPE = click.BOOL
18-
19-
DEFAULT_BUS = None
20-
USER_BUS = True
21-
SYSTEM_BUS = False
22-
23-
24-
def bus_type_option(fn: SyncCommand) -> SyncCommand:
25-
@click.option(
26-
"--user/--system",
27-
type=BUS_TYPE,
28-
default=DEFAULT_BUS,
29-
help="Connect to either the user or system bus",
30-
)
31-
@functools.wraps(fn)
32-
def wrapper(*args, **kwargs) -> None:
33-
user = kwargs.pop("user")
34-
kwargs["bus_type"] = user
35-
return fn(*args, **kwargs)
3611

37-
return wrapper
12+
def select_session_bus() -> None:
13+
logger.debug("Connecting to the user session bus")
14+
set_default_bus(sd_bus_open_user())
3815

3916

40-
def configure_bus(bus_type: BusType) -> None:
41-
if bus_type is USER_BUS:
42-
logger.info("Connecting to the user session bus")
43-
set_default_bus(sd_bus_open_user())
44-
elif bus_type is SYSTEM_BUS:
45-
logger.info("Connecting to the system bus")
46-
set_default_bus(sd_bus_open_system())
47-
else:
48-
logger.info("Connecting to the default bus")
17+
def select_system_bus() -> None:
18+
logger.debug("Connecting to the system bus")
19+
set_default_bus(sd_bus_open_system())

crystalfontz/dbus/client/cli.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
OutputMode,
3030
WATCHDOG_SETTING,
3131
)
32-
from crystalfontz.dbus.bus import bus_type_option, BusType, configure_bus
32+
from crystalfontz.dbus.bus import select_session_bus, select_system_bus
3333
from crystalfontz.dbus.client import DbusClient
3434
from crystalfontz.dbus.config import StagedConfig
3535
from crystalfontz.dbus.domain import (
@@ -173,15 +173,20 @@ def warn_dirty() -> None:
173173
envvar="CRYSTALFONTZ_RETRY_TIMES",
174174
help="How many times to retry a command if a response times out",
175175
)
176-
@bus_type_option
176+
@click.option(
177+
"--user/--default",
178+
type=click.BOOL,
179+
default=None,
180+
help="Connect to either the user or default bus",
181+
)
177182
@click.pass_context
178183
def main(
179184
ctx: click.Context,
180185
log_level: LogLevel,
181186
output: OutputMode,
182187
timeout: Optional[float],
183188
retry_times: Optional[int],
184-
bus_type: BusType,
189+
user: Optional[bool],
185190
) -> None:
186191
"""
187192
Control your Crystalfontz device.
@@ -193,7 +198,10 @@ def main(
193198
echo.mode = output
194199

195200
async def load() -> None:
196-
configure_bus(bus_type)
201+
if user:
202+
select_session_bus()
203+
elif user is None:
204+
select_system_bus()
197205

198206
client = DbusClient()
199207
ctx.obj = Obj(

crystalfontz/dbus/service/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
from crystalfontz.cli import LogLevel
1212
from crystalfontz.config import GLOBAL_FILE
13-
from crystalfontz.dbus.bus import bus_type_option, BusType, configure_bus
13+
from crystalfontz.dbus.bus import (
14+
select_session_bus,
15+
select_system_bus,
16+
)
1417
from crystalfontz.dbus.error import handle_dbus_error
1518
from crystalfontz.dbus.interface import DBUS_NAME, DbusInterface, load_client
1619

@@ -71,9 +74,14 @@ async def serve(config_file: Optional[str] = None) -> None:
7174
default="INFO",
7275
help="Set the log level",
7376
)
74-
@bus_type_option
77+
@click.option(
78+
"--user/--system",
79+
type=click.BOOL,
80+
default=None,
81+
help="Connect to either the user or system bus",
82+
)
7583
def main(
76-
global_: bool, config_file: str, log_level: LogLevel, bus_type: BusType
84+
global_: bool, config_file: str, log_level: LogLevel, user: Optional[bool]
7785
) -> None:
7886
"""
7987
Expose the Crystalfontz device as a DBus service.
@@ -91,6 +99,9 @@ def main(
9199
elif global_:
92100
file = GLOBAL_FILE
93101

94-
configure_bus(bus_type)
102+
if user:
103+
select_session_bus()
104+
elif user is False:
105+
select_system_bus()
95106

96107
asyncio.run(serve(file))

docs/dbus/client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Options:
1616
device before timing out
1717
--retry-times INTEGER How many times to retry a command if a
1818
response times out
19-
--user / --system Connect to either the user or system bus
19+
--user / --default Connect to either the user or default bus
2020
--help Show this message and exit.
2121

2222
Commands:

tests/conftest.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def cli(cli_env: EnvFactory) -> Cli:
5858
def dbus_service(
5959
cli_env: EnvFactory, request: pytest.FixtureRequest
6060
) -> Generator[None, None, None]:
61-
cli = Cli(["python3", "-m", "crystalfontz.dbus.service"], env=cli_env())
61+
cli = Cli(["python3", "-m", "crystalfontz.dbus.service", "--user"], env=cli_env())
6262

6363
if request.config.getoption("--system"):
6464
logger.info("Connecting to system bus")
@@ -73,7 +73,12 @@ def dbus_service(
7373
def dbus_cli(
7474
cli_env: EnvFactory, dbus_service: None, request: pytest.FixtureRequest
7575
) -> Cli:
76-
argv: List[str] = ["python3", "-m", "crystalfontz.dbus.client"]
76+
argv: List[str] = [
77+
"python3",
78+
"-m",
79+
"crystalfontz.dbus.client",
80+
"--system" if request.config.getoption("--system") else "--user",
81+
]
7782

7883
if not request.config.getoption("--system"):
7984
argv.append("--user")

0 commit comments

Comments
 (0)