def _generate_property(self, member, member_name): """Generate DBus property defined by class member. :param member: a property object :param member_name: a property name :return: a property element raises DBusSpecificationError: if the property is invalid """ access = None type_hint = None try: # Process the setter. if member.fset: [(_, type_hint, _)] = self._iterate_parameters(member.fset) access = self.ACCESS_WRITE # Process the getter. if member.fget: [(_, type_hint, _)] = self._iterate_parameters(member.fget) access = self.ACCESS_READ except ValueError: raise DBusSpecificationError("Property %s has invalid parameters." % member_name) # Property has both. if member.fget and member.fset: access = self.ACCESS_READWRITE if access is None: raise DBusSpecificationError("Property %s is not accessible." % member_name) return self.xml_generator.create_property(member_name, get_dbus_type(type_hint), access)
def _generate_signal(cls, member, member_name): """Generate signal defined by a class member. :param member: a dbus_signal object. :param member_name: a name of the signal :return: a signal element raises DBusSpecificationError: if signal has defined return type """ element = cls.xml_generator.create_signal(member_name) method = member.definition if not method: return element for name, type_hint, direction in cls._iterate_parameters(method): # Only input parameters can be defined. if direction == DBusSpecification.DIRECTION_OUT: raise DBusSpecificationError( "Signal %s has defined return type." % member_name) # All parameters are exported as output parameters (see specification). direction = DBusSpecification.DIRECTION_OUT parameter = cls.xml_generator.create_parameter( name, get_dbus_type(type_hint), direction) cls.xml_generator.add_child(element, parameter) return element
def _generate_signal(self, member, member_name): """Generate signal defined by a class member. :param member: a dbus_signal object. :param member_name: a name of the signal :return: a signal element raises DBusSpecificationError: if signal has defined return type """ element = self.xml_generator.get_signal_element(member_name) method = member.definition if not method: return element for name, type_hint, direction in self._iterate_parameters(method): # Only input parameters can be defined. if direction == self.DIRECTION_OUT: raise DBusSpecificationError("Signal %s has defined return type." % member_name) # All parameters are exported as output parameters (see specification). direction = self.DIRECTION_OUT parameter = self.xml_generator.create_parameter(name, get_dbus_type(type_hint), direction) self.xml_generator.add_child(element, parameter) return element
def invalid_test(self): """Test the invalid types.""" with self.assertRaises(TypeError): get_dbus_type(Dict[List[Bool], Bool]) with self.assertRaises(TypeError): get_dbus_type(Dict[Variant, Int]) with self.assertRaises(TypeError): get_dbus_type(Tuple[Int, Double, Dict[Tuple[Int, Int], Bool]]) with self.assertRaises(TypeError): get_dbus_type(Set[Int])
def unknown_test(self): """Test the unknown type.""" class UnknownType: pass with self.assertRaises(ValueError): get_dbus_type(UnknownType) with self.assertRaises(ValueError): get_dbus_type(List[UnknownType]) with self.assertRaises(ValueError): get_dbus_type(Tuple[Int, Str, UnknownType]) with self.assertRaises(ValueError): get_dbus_type(Dict[Int, UnknownType])
def invalid_test(self): """Test the invalid types.""" with self.assertRaises(ValueError): get_dbus_type(Dict[List[Bool], Bool]) with self.assertRaises(ValueError): get_dbus_type(Dict[Variant, Int]) with self.assertRaises(ValueError): get_dbus_type(Tuple[Int, Double, Dict[Tuple[Int, Int], Bool]])
def _generate_method(self, member, member_name): """Generate method defined by given class member. :param member: a method object :param member_name: a name of the method :return: a method element """ method = self.xml_generator.get_method_element(member_name) # Process the parameters. for name, type_hint, direction in self._iterate_parameters(member): # Create the parameter element. parameter = self.xml_generator.create_parameter(name, get_dbus_type(type_hint), direction) # Add the element to the method element. self.xml_generator.add_child(method, parameter) return method
def _generate_method(cls, member, member_name): """Generate method defined by given class member. :param member: a method object :param member_name: a name of the method :return: a method element """ method = cls.xml_generator.create_method(member_name) # Process the parameters. for name, type_hint, direction in cls._iterate_parameters(member): # Create the parameter element. parameter = cls.xml_generator.create_parameter( name, get_dbus_type(type_hint), direction) # Add the element to the method element. cls.xml_generator.add_child(method, parameter) return method
def _compare(self, type_hint, expected_string): """Compare generated and expected types.""" # Generate a type string. dbus_type = get_dbus_type(type_hint) self.assertEqual(dbus_type, expected_string) self.assertTrue(GLib.VariantType.string_is_valid(dbus_type)) # Create a variant type from a type hint. variant_type = get_variant_type(type_hint) self.assertIsInstance(variant_type, GLib.VariantType) self.assertEqual(variant_type.dup_string(), expected_string) expected_type = GLib.VariantType.new(expected_string) self.assertTrue(expected_type.equal(variant_type)) # Create a variant type from a type string. variant_type = get_variant_type(expected_string) self.assertIsInstance(variant_type, GLib.VariantType) self.assertTrue(expected_type.equal(variant_type))
def _generate_property(cls, member, member_name): """Generate DBus property defined by class member. :param member: a property object :param member_name: a property name :return: a property element raises DBusSpecificationError: if the property is invalid """ access = None type_hint = None try: # Process the setter. if member.fset: [(_, type_hint, _)] = cls._iterate_parameters(member.fset) access = DBusSpecification.ACCESS_WRITE # Process the getter. if member.fget: [(_, type_hint, _)] = cls._iterate_parameters(member.fget) access = DBusSpecification.ACCESS_READ except ValueError: raise DBusSpecificationError( "Property %s has invalid parameters." % member_name) # Property has both. if member.fget and member.fset: access = DBusSpecification.ACCESS_READWRITE if access is None: raise DBusSpecificationError("Property %s is not accessible." % member_name) return cls.xml_generator.create_property(member_name, get_dbus_type(type_hint), access)
def _compare(self, type_hint, expected_string): """Compare generated and expected types.""" dbus_type = get_dbus_type(type_hint) self.assertEqual(dbus_type, expected_string) self.assertTrue(GLib.VariantType.string_is_valid(dbus_type))