Skip to content

Commit dd66119

Browse files
author
Anders Hellerup Madsen
committed
support signatures to signal decorator
1 parent f158687 commit dd66119

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
@@ -122,17 +122,16 @@ def wrapped(*args, **kwargs):
122122

123123

124124
class _Signal:
125-
def __init__(self, fn, name, disabled=False):
125+
def __init__(self, fn, name, disabled=False, signature: Optional[str] = None):
126126
inspection = inspect.signature(fn)
127127

128128
args = []
129-
signature = ''
130129
signature_tree = None
131130

132-
return_annotation = parse_annotation(inspection.return_annotation)
131+
if signature is None:
132+
signature = parse_annotation(inspection.return_annotation)
133133

134-
if return_annotation:
135-
signature = return_annotation
134+
if signature:
136135
signature_tree = SignatureTree._get(signature)
137136
for type_ in signature_tree.types:
138137
args.append(intr.Arg(type_, intr.ArgDirection.OUT))
@@ -147,7 +146,7 @@ def __init__(self, fn, name, disabled=False):
147146
self.introspection = intr.Signal(self.name, args)
148147

149148

150-
def signal(name: str = None, disabled: bool = False):
149+
def signal(name: str = None, disabled: bool = False, signature: Optional[str] = None):
151150
"""A decorator to mark a class method of a :class:`ServiceInterface` to be a DBus signal.
152151
153152
The signal is broadcast on the bus when the decorated class method is
@@ -185,7 +184,7 @@ def two_strings_signal(self, val1, val2) -> 'ss':
185184
@no_type_check_decorator
186185
def decorator(fn):
187186
fn_name = name if name else fn.__name__
188-
signal = _Signal(fn, fn_name, disabled)
187+
signal = _Signal(fn, fn_name, disabled, signature)
189188

190189
@wraps(fn)
191190
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)