Compare commits
3 Commits
bc50f0160e
...
de1774f60e
| Author | SHA1 | Date | |
|---|---|---|---|
| de1774f60e | |||
| 8c145ef2ff | |||
| 0ecd86bd64 |
@@ -29,6 +29,11 @@ def setup_command_line_arguments():
|
||||
help="RevPi PiControl object",
|
||||
)
|
||||
cli_picontrol.add_subparsers(obj_picontrol)
|
||||
obj_config = rpictl_obj.add_parser(
|
||||
"config",
|
||||
help="RevPi configuration object (revpi-config)",
|
||||
)
|
||||
cli_config.add_subparsers(obj_config)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
@@ -39,6 +44,9 @@ def main() -> int:
|
||||
if obj == "picontrol":
|
||||
rc = cli_picontrol.main()
|
||||
|
||||
elif obj == "config":
|
||||
rc = cli_config.main()
|
||||
|
||||
else:
|
||||
log.error(f"Unknown object: {obj}")
|
||||
rc = 1
|
||||
|
||||
93
src/revpi_middleware/cli_commands/cli_config.py
Normal file
93
src/revpi_middleware/cli_commands/cli_config.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
"""Command-Line for the picontrol object of CLI."""
|
||||
from argparse import ArgumentParser
|
||||
from logging import getLogger
|
||||
|
||||
from .dbus_helper import BusType, get_properties, simple_call
|
||||
from .. import proginit as pi
|
||||
from ..dbus_middleware1 import extend_interface
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
|
||||
def add_subparsers(parent_parser: ArgumentParser):
|
||||
parent_parser.add_argument(
|
||||
"action",
|
||||
choices=["enable", "disable", "status", "available", "list-features"],
|
||||
help="Action to be executed: enable, disable, status or available. "
|
||||
"To get all available features, use 'list-features'.",
|
||||
)
|
||||
parent_parser.add_argument(
|
||||
"feature",
|
||||
nargs="?",
|
||||
default="",
|
||||
help="Name of the feature to configer. To list all features use 'list-features' as action.",
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
action = pi.pargs.action
|
||||
dbus_value = False
|
||||
try:
|
||||
|
||||
if action == "list-features":
|
||||
dbus_value = get_properties(
|
||||
"available_features",
|
||||
interface=extend_interface("RevpiConfig"),
|
||||
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||
)
|
||||
for feature in dbus_value:
|
||||
print(feature)
|
||||
|
||||
return 0
|
||||
|
||||
# For the following actions, a feature name is required
|
||||
if pi.pargs.feature == "":
|
||||
raise Exception("Feature name is required")
|
||||
|
||||
if action == "enable":
|
||||
simple_call(
|
||||
"Enable",
|
||||
pi.pargs.feature,
|
||||
interface=extend_interface("RevpiConfig"),
|
||||
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||
)
|
||||
|
||||
elif action == "disable":
|
||||
simple_call(
|
||||
"Disable",
|
||||
pi.pargs.feature,
|
||||
interface=extend_interface("RevpiConfig"),
|
||||
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||
)
|
||||
|
||||
elif action == "status":
|
||||
dbus_value = simple_call(
|
||||
"GetStatus",
|
||||
pi.pargs.feature,
|
||||
interface=extend_interface("RevpiConfig"),
|
||||
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||
)
|
||||
|
||||
elif action == "available":
|
||||
dbus_value = simple_call(
|
||||
"GetAvailability",
|
||||
pi.pargs.feature,
|
||||
interface=extend_interface("RevpiConfig"),
|
||||
bus_type=BusType.SESSION if pi.pargs.use_session_bus else BusType.SYSTEM,
|
||||
)
|
||||
|
||||
else:
|
||||
raise Exception(f"Unknown action: {action}")
|
||||
|
||||
except Exception as e:
|
||||
log.error(f"Error: {e}")
|
||||
return 1
|
||||
|
||||
log.debug(
|
||||
f"D-Bus call of method {action} for feature {pi.pargs.feature} returned: {dbus_value}"
|
||||
)
|
||||
print(int(dbus_value))
|
||||
|
||||
return 0
|
||||
@@ -17,6 +17,18 @@ class BusType(Enum):
|
||||
SYSTEM = "system"
|
||||
|
||||
|
||||
def get_properties(
|
||||
property_name: str,
|
||||
interface: str,
|
||||
object_path=REVPI_DBUS_BASE_PATH,
|
||||
bus_type=BusType.SYSTEM,
|
||||
):
|
||||
bus = SessionBus() if bus_type is BusType.SESSION else SystemBus()
|
||||
revpi = bus.get(REVPI_DBUS_NAME, object_path)
|
||||
iface = revpi[interface]
|
||||
return getattr(iface, property_name)
|
||||
|
||||
|
||||
def simple_call(
|
||||
method: str,
|
||||
*args,
|
||||
|
||||
Reference in New Issue
Block a user