Make DBusExporter reusable for other services
Do not hardcode Polkit namespace into DBusExporter, provide it as a parameter in the constructor.
This commit is contained in:
parent
b15de12ec9
commit
4502d7abd7
3 changed files with 6 additions and 5 deletions
2
tuned.py
2
tuned.py
|
|
@ -79,7 +79,7 @@ if __name__ == "__main__":
|
||||||
args.no_socket = True
|
args.no_socket = True
|
||||||
|
|
||||||
if not args.no_dbus:
|
if not args.no_dbus:
|
||||||
app.attach_to_dbus(consts.DBUS_BUS, consts.DBUS_OBJECT, consts.DBUS_INTERFACE)
|
app.attach_to_dbus(consts.DBUS_BUS, consts.DBUS_OBJECT, consts.DBUS_INTERFACE, consts.NAMESPACE)
|
||||||
|
|
||||||
if not args.no_socket:
|
if not args.no_socket:
|
||||||
app.attach_to_unix_socket()
|
app.attach_to_unix_socket()
|
||||||
|
|
|
||||||
|
|
@ -71,11 +71,11 @@ class Application(object):
|
||||||
self._handle_signal(signal.SIGINT, self._controller.terminate)
|
self._handle_signal(signal.SIGINT, self._controller.terminate)
|
||||||
self._handle_signal(signal.SIGTERM, self._controller.terminate)
|
self._handle_signal(signal.SIGTERM, self._controller.terminate)
|
||||||
|
|
||||||
def attach_to_dbus(self, bus_name, object_name, interface_name):
|
def attach_to_dbus(self, bus_name, object_name, interface_name, namespace):
|
||||||
if self._dbus_exporter is not None:
|
if self._dbus_exporter is not None:
|
||||||
raise TunedException("DBus interface is already initialized.")
|
raise TunedException("DBus interface is already initialized.")
|
||||||
|
|
||||||
self._dbus_exporter = exports.dbus.DBusExporter(bus_name, interface_name, object_name)
|
self._dbus_exporter = exports.dbus.DBusExporter(bus_name, interface_name, object_name, namespace)
|
||||||
exports.register_exporter(self._dbus_exporter)
|
exports.register_exporter(self._dbus_exporter)
|
||||||
|
|
||||||
def attach_to_unix_socket(self):
|
def attach_to_unix_socket(self):
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ class DBusExporter(interfaces.ExporterInterface):
|
||||||
to an object we dynamically construct.
|
to an object we dynamically construct.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, bus_name, interface_name, object_name):
|
def __init__(self, bus_name, interface_name, object_name, namespace):
|
||||||
# Monkey patching of the D-Bus library _method_reply_error() to reply
|
# Monkey patching of the D-Bus library _method_reply_error() to reply
|
||||||
# tracebacks via D-Bus only if in the debug mode. It doesn't seem there is a
|
# tracebacks via D-Bus only if in the debug mode. It doesn't seem there is a
|
||||||
# more simple way how to cover all possible exceptions that could occur in
|
# more simple way how to cover all possible exceptions that could occur in
|
||||||
|
|
@ -82,6 +82,7 @@ class DBusExporter(interfaces.ExporterInterface):
|
||||||
self._bus_name = bus_name
|
self._bus_name = bus_name
|
||||||
self._interface_name = interface_name
|
self._interface_name = interface_name
|
||||||
self._object_name = object_name
|
self._object_name = object_name
|
||||||
|
self._namespace = namespace
|
||||||
self._thread = None
|
self._thread = None
|
||||||
self._bus_object = None
|
self._bus_object = None
|
||||||
self._polkit = polkit()
|
self._polkit = polkit()
|
||||||
|
|
@ -130,7 +131,7 @@ class DBusExporter(interfaces.ExporterInterface):
|
||||||
raise Exception("Method with this name is already exported.")
|
raise Exception("Method with this name is already exported.")
|
||||||
|
|
||||||
def wrapper(owner, *args, **kwargs):
|
def wrapper(owner, *args, **kwargs):
|
||||||
action_id = consts.NAMESPACE + "." + method.__name__
|
action_id = self._namespace + "." + method.__name__
|
||||||
caller = args[-1]
|
caller = args[-1]
|
||||||
log.debug("checking authorization for action '%s' requested by caller '%s'" % (action_id, caller))
|
log.debug("checking authorization for action '%s' requested by caller '%s'" % (action_id, caller))
|
||||||
ret = self._polkit.check_authorization(caller, action_id)
|
ret = self._polkit.check_authorization(caller, action_id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue