Skip to content

Commit cecfe43

Browse files
author
Anders Hellerup Madsen
committed
support signatures to signal decorator
1 parent a7758c3 commit cecfe43

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

dbus_next/service.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,16 @@ def wrapped(*args, **kwargs):
107107

108108

109109
class _Signal:
110-
def __init__(self, fn, name, disabled=False):
110+
def __init__(self, fn, name, disabled=False, signature: Optional[str] = None):
111111
inspection = inspect.signature(fn)
112112

113113
args = []
114-
signature = ''
115114
signature_tree = None
116115

117-
return_annotation = parse_annotation(inspection.return_annotation)
116+
if signature is None:
117+
signature = parse_annotation(inspection.return_annotation)
118118

119-
if return_annotation:
120-
signature = return_annotation
119+
if signature:
121120
signature_tree = SignatureTree._get(signature)
122121
for type_ in signature_tree.types:
123122
args.append(intr.Arg(type_, intr.ArgDirection.OUT))
@@ -132,7 +131,7 @@ def __init__(self, fn, name, disabled=False):
132131
self.introspection = intr.Signal(self.name, args)
133132

134133

135-
def signal(name: str = None, disabled: bool = False):
134+
def signal(name: str = None, disabled: bool = False, signature: Optional[str] = None):
136135
"""A decorator to mark a class method of a :class:`ServiceInterface` to be a DBus signal.
137136
138137
The signal is broadcast on the bus when the decorated class method is
@@ -170,7 +169,7 @@ def two_strings_signal(self, val1, val2) -> 'ss':
170169
@no_type_check_decorator
171170
def decorator(fn):
172171
fn_name = name if name else fn.__name__
173-
signal = _Signal(fn, fn_name, disabled)
172+
signal = _Signal(fn, fn_name, disabled, signature)
174173

175174
@wraps(fn)
176175
def wrapped(self, *args, **kwargs):

test/service/test_decorators.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def foo_prop(self) -> int:
6262
def foo_prop(self, val: int):
6363
self._foo_prop = val
6464

65+
@signal(signature="as")
66+
def foo_signal(self) -> List[str]:
67+
return ['result']
68+
6569

6670
def test_method_decorator():
6771
interface = ExampleInterface()
@@ -96,7 +100,7 @@ def test_method_decorator():
96100
assert not method.disabled
97101
assert type(method.introspection) is intr.Method
98102

99-
assert len(signals) == 2
103+
assert len(signals) == 3
100104

101105
signal = signals[0]
102106
assert signal.name == 'renamed_signal'
@@ -105,6 +109,12 @@ def test_method_decorator():
105109
assert type(signal.introspection) is intr.Signal
106110

107111
signal = signals[1]
112+
assert signal.name == 'foo_signal'
113+
assert signal.signature == 'as'
114+
assert not signal.disabled
115+
assert type(signal.introspection) is intr.Signal
116+
117+
signal = signals[2]
108118
assert signal.name == 'some_signal'
109119
assert signal.signature == 'as'
110120
assert not signal.disabled
@@ -177,7 +187,7 @@ def test_interface_introspection():
177187
signals = xml.findall('signal')
178188
properties = xml.findall('property')
179189

180-
assert len(xml) == 6
190+
assert len(xml) == 7
181191
assert len(methods) == 2
182-
assert len(signals) == 1
192+
assert len(signals) == 2
183193
assert len(properties) == 3

0 commit comments

Comments
 (0)