def _compare(self, cls, expected_xml): """Compare cls specification with the given xml.""" generated_xml = get_xml(cls) # pylint: disable=no-member self.assertEqual( XMLGenerator.prettify_xml(generated_xml), XMLGenerator.prettify_xml(expected_xml) )
def test_property(self): """Test the property element.""" self._compare( XMLGenerator.create_property("PropertyName", "PropertyType", "PropertyAccess"), '<property ' 'access="PropertyAccess" ' 'name="PropertyName" ' 'type="PropertyType" />')
def test_parameter(self): """Test the parameter element.""" self._compare( XMLGenerator.create_parameter("ParameterName", "ParameterType", "ParameterDirection"), '<arg ' 'direction="ParameterDirection" ' 'name="ParameterName" ' 'type="ParameterType" />')
def test_xml_specification(self): """Test the generated specification.""" self._set_service(ExampleInterface()) expected_xml = ''' <node> <!--Specifies ExampleInterface--> <interface name="my.testing.Example"> <method name="GetInfo"> <arg direction="in" name="arg" type="s"></arg> <arg direction="out" name="return" type="s"></arg> </method> <method name="Hello"> <arg direction="in" name="name" type="s"></arg> <arg direction="out" name="return" type="s"></arg> </method> <method name="Knock"></method> <signal name="Knocked"></signal> <property access="read" name="Name" type="s"></property> <method name="Raise"> <arg direction="in" name="message" type="s"></arg> </method> <method name="ReturnArgs"> <arg direction="out" name="return_0" type="i"></arg> <arg direction="out" name="return_1" type="b"></arg> <arg direction="out" name="return_2" type="s"></arg> </method> <property access="write" name="Secret" type="s"></property> <property access="readwrite" name="Value" type="i"></property> <signal name="Visited"> <arg direction="out" name="name" type="s"></arg> </signal> </interface> </node> ''' generated_xml = self.service.__dbus_xml__ self.assertEqual( XMLGenerator.prettify_xml(expected_xml), XMLGenerator.prettify_xml(generated_xml) )
def test_signal(self): """Test the signal element.""" element = XMLGenerator.create_signal("SignalName") xml = '<signal name="SignalName" />' self._compare(element, xml)
def test_method(self): """Test the method element.""" element = XMLGenerator.create_method("MethodName") xml = '<method name="MethodName" />' self._compare(element, xml)
def test_interface(self): """Test the interface element.""" self._compare(XMLGenerator.create_interface("InterfaceName"), '<interface name="InterfaceName" />')
def test_node(self): """Test the node element.""" self._compare(XMLGenerator.create_node(), '<node />')
def _compare(self, element, xml): self.assertEqual( XMLGenerator.prettify_xml(XMLGenerator.element_to_xml(element)), XMLGenerator.prettify_xml(xml))
def for_publication(self): """Return a DBus representation.""" return ChatInterface(self) def find_room(self, name): """Find or create a chat room.""" if name not in self._rooms: self._rooms[name] = Room(name) return self._rooms[name] if __name__ == "__main__": # Print the generated XML specifications. print(XMLGenerator.prettify_xml(ChatInterface.__dbus_xml__)) print(XMLGenerator.prettify_xml(RoomInterface.__dbus_xml__)) try: # Create the chat. chat = Chat() # Publish the chat at /org/example/Chat. SESSION_BUS.publish_object(CHAT.object_path, chat.for_publication()) # Register the service name org.example.Chat. SESSION_BUS.register_service(CHAT.service_name) # Start the event loop. loop = EventLoop() loop.run()
@dbus_interface(HELLO_WORLD.interface_name) class HelloWorld(object): """The DBus interface for HelloWorld.""" def Hello(self, name: Str) -> Str: """Generate a greeting. :param name: someone to say hello :return: a greeting """ return "Hello {}!".format(name) if __name__ == "__main__": # Print the generated XML specification. print(XMLGenerator.prettify_xml(HelloWorld.__dbus_xml__)) try: # Create an instance of the class HelloWorld. hello_world = HelloWorld() # Publish the instance at /org/example/HelloWorld. SESSION_BUS.publish_object(HELLO_WORLD.object_path, hello_world) # Register the service name org.example.HelloWorld. SESSION_BUS.register_service(HELLO_WORLD.service_name) # Start the event loop. loop = EventLoop() loop.run() finally:
def users_changed(self): """Signal the user list change.""" return self._users_changed def register_user(self, user: User): """Register a new user.""" if any(u for u in self.users if u.name == user.name): raise InvalidUser("User {} exists.".format(user.name)) self._users.append(user) self._users_changed.emit() if __name__ == "__main__": # Print the generated XML specification. print(XMLGenerator.prettify_xml(RegisterInterface.__dbus_xml__)) try: # Create the register. register = Register() # Publish the register at /org/example/Register. SESSION_BUS.publish_object(REGISTER.object_path, RegisterInterface(register)) # Register the service name org.example.Register. SESSION_BUS.register_service(REGISTER.service_name) # Start the event loop. loop = EventLoop() loop.run()