Skip to content

Commit

Permalink
Support strongly typed signals
Browse files Browse the repository at this point in the history
In addition to properties and methods, the arguments of a signal have also
types which can be defined with `TypedSignal`, a subclass of `signal`.
  • Loading branch information
xZise committed Oct 21, 2016
1 parent f04257f commit 3a113bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pydbus/strong_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Decorators for methods and properties to strongly typed the values."""
import inspect

from pydbus.generic import signal
from pydbus.xml_generator import verify_arguments


Expand Down Expand Up @@ -34,3 +35,10 @@ def decorate(func):
verify_arguments(func)
return func
return decorate


class TypedSignal(signal):

def __init__(self, arguments):
super(TypedSignal, self).__init__()
self.arg_types = arguments
15 changes: 14 additions & 1 deletion pydbus/xml_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from itertools import islice
from xml.etree import ElementTree

from pydbus.generic import signal


PROPERTY_EMITS_SIGNAL = "org.freedesktop.DBus.Property.EmitsChangedSignal"

Expand Down Expand Up @@ -149,7 +151,7 @@ def valid_member(member):
"""Only select members with the correct type and name."""
if isinstance(member, property):
member = member.fget
elif not ismethod(member):
elif not ismethod(member) and not isinstance(member, signal):
return False
return member.__name__[0].isupper()

Expand Down Expand Up @@ -190,6 +192,17 @@ def valid_member(member):
entry, "annotation",
{"name": PROPERTY_EMITS_SIGNAL, "value": "true"})
attributes["access"] = "readwrite"
elif isinstance(value, signal):
if hasattr(value, "arguments"):
arguments = value.arguments
elif require_strong_typing:
raise ValueError(
"No argument definitions for signal "
"'{}'".format(value.__name__))
entry = ElementTree.SubElement(value, "signal")
for arg, arg_type in arguments:
ElementTree.SubElement(
entry, "arg", {"name": arg, "type": arg_type})
elif ismethod(value):
arg_types, ret_type = verify_arguments(value)
entry = ElementTree.SubElement(get_interface(value), "method")
Expand Down

0 comments on commit 3a113bc

Please sign in to comment.