| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
""" |
| 4 |
Service Object for DBus |
| 5 |
""" |
| 6 |
|
| 7 |
# Disable some pylint stuff |
| 8 |
# pylint: disable=no-member |
| 9 |
|
| 10 |
import types |
| 11 |
import dbus |
| 12 |
import dbus.service |
| 13 |
|
| 14 |
|
| 15 |
def copy_func(function_reference, name=None): |
| 16 |
""" |
| 17 |
Copy function |
| 18 |
|
| 19 |
:param function_reference: Function |
| 20 |
:type function_reference: func |
| 21 |
|
| 22 |
:param name: Name of function |
| 23 |
:type name: str |
| 24 |
|
| 25 |
:return: Copy of function |
| 26 |
:rtype: func |
| 27 |
""" |
| 28 |
if hasattr(function_reference, 'code'): |
| 29 |
return types.FunctionType(function_reference.code, function_reference.globals, name or function_reference.func_name, function_reference.defaults, function_reference.closure) |
| 30 |
else: |
| 31 |
return types.FunctionType(function_reference.__code__, function_reference.__globals__, name or function_reference.func_name, function_reference.__defaults__, function_reference.__closure__) |
| 32 |
|
| 33 |
|
| 34 |
class DBusService(dbus.service.Object): |
| 35 |
""" |
| 36 |
DBus Service object |
| 37 |
|
| 38 |
Allows for dynamic method adding |
| 39 |
""" |
| 40 |
BUS_NAME = 'org.razer' |
| 41 |
|
| 42 |
def __init__(self, object_path): |
| 43 |
""" |
| 44 |
Init the object |
| 45 |
|
| 46 |
:param object_path: DBus Object name |
| 47 |
:type object_path: str |
| 48 |
""" |
| 49 |
# We could pass (bus, object_path) here, but we rather register the object manually. |
| 50 |
super().__init__() |
| 51 |
|
| 52 |
bus = dbus.SessionBus() |
| 53 |
|
| 54 |
# the constructor of BusName registers the bus, the returned object is not used but must be kept |
| 55 |
self.bus_name_obj = dbus.service.BusName(self.BUS_NAME, bus) |
| 56 |
|
| 57 |
self.add_to_connection(bus, object_path) |
| 58 |
|
| 59 |
def add_dbus_method(self, interface_name, function_name, function, in_signature=None, out_signature=None, byte_arrays=False): |
| 60 |
""" |
| 61 |
Add method to DBus Object |
| 62 |
|
| 63 |
:param interface_name: DBus interface name |
| 64 |
:type interface_name: str |
| 65 |
|
| 66 |
:param function_name: DBus function name |
| 67 |
:type function_name: str |
| 68 |
|
| 69 |
:param function: Function reference |
| 70 |
:type function: object |
| 71 |
|
| 72 |
:param in_signature: DBus function signature |
| 73 |
:type in_signature: str |
| 74 |
|
| 75 |
:param out_signature: DBus function signature |
| 76 |
:type out_signature: str |
| 77 |
|
| 78 |
:param byte_arrays: Is byte array |
| 79 |
:type byte_arrays: bool |
| 80 |
""" |
| 81 |
|
| 82 |
# Get class key for use in the DBus introspection table |
| 83 |
class_key = [key for key in self._dbus_class_table.keys() if key.endswith(self.__class__.__name__)][0] |
| 84 |
|
| 85 |
# Create a copy of the function so that if its used multiple times it won't affect other instances if the names changed |
| 86 |
function_deepcopy = copy_func(function, function_name) |
| 87 |
func = dbus.service.method(interface_name, in_signature=in_signature, out_signature=out_signature, byte_arrays=byte_arrays)(function_deepcopy) |
| 88 |
|
| 89 |
# Add method to DBus tables |
| 90 |
try: |
| 91 |
self._dbus_class_table[class_key][interface_name][function_name] = func |
| 92 |
except KeyError: |
| 93 |
self._dbus_class_table[class_key][interface_name] = {function_name: func} |
| 94 |
|
| 95 |
# Add method to class as DBus expects it to be there. |
| 96 |
setattr(self.__class__, function_name, func) |
| 97 |
|
| 98 |
def del_dbus_method(self, interface_name, function_name): |
| 99 |
""" |
| 100 |
Remove method from DBus Object |
| 101 |
|
| 102 |
:param interface_name: DBus interface name |
| 103 |
:type interface_name: str |
| 104 |
|
| 105 |
:param function_name: DBus function name |
| 106 |
:type function_name: str |
| 107 |
""" |
| 108 |
|
| 109 |
# Get class key for use in the DBus introspection table |
| 110 |
class_key = [key for key in self._dbus_class_table.keys() if key.endswith(self.__class__.__name__)][0] |
| 111 |
|
| 112 |
# Remove method from DBus tables |
| 113 |
# Remove method from class |
| 114 |
try: |
| 115 |
del self._dbus_class_table[class_key][interface_name][function_name] |
| 116 |
delattr(DBusService, function_name) |
| 117 |
|
| 118 |
except (KeyError, AttributeError): |
| 119 |
pass |