Beispiel #1
0
    def _set_properties(self, state_dict):
        """Creates and set attributes of *self* based on contents of
        *state_dict*.

        .. note:: Translates '-' to '_', so a key of 'icon-type' for example
         becomes 'icon_type'.

        """
        # don't store id in state dictionary - make it a proper instance
        # attribute. If id is not present, raise a ValueError.
        try:
            self.id = int(state_dict['id'][1])
        except KeyError:
            raise ValueError(
                "State dictionary does not contain required 'id' key.")

        self.__state = {}
        for key, value in translate_state_keys(state_dict).items():
            if key == 'id':
                continue
            try:
                self.__state[key] = create_value_instance(value, self, key)
            except ValueError as e:
                _logger.warning("While constructing attribute '%s.%s': %s",
                                self.__class__.__name__, key, str(e))
Beispiel #2
0
    def test_invalid_no_data(self):
        data = dbus.Array([
            dbus.Int32(0),
        ])
        fn = lambda: create_value_instance(data, None, None)

        self.assertThat(
            fn,
            raises(ValueError("Cannot create attribute, no data supplied")))
Beispiel #3
0
    def test_date_time(self):
        data = dbus.Array([
            dbus.Int32(ValueType.DATETIME),
            dbus.Int32(0),
        ])

        attr = create_value_instance(data, None, None)

        self.assertThat(attr, IsInstance(DateTime))
Beispiel #4
0
 def test_plain_array(self):
     data = dbus.Array([
         dbus.Int32(ValueType.PLAIN),
         dbus.Array([dbus.String("Hello"),
                     dbus.String("World")])
     ])
     attr = create_value_instance(data, None, None)
     self.assertThat(attr, Equals(["Hello", "World"]))
     self.assertThat(attr, IsInstance(PlainType))
Beispiel #5
0
    def test_size(self):
        data = dbus.Array([
            dbus.Int32(ValueType.SIZE),
            dbus.Int32(0),
            dbus.Int32(10),
        ])

        attr = create_value_instance(data, None, None)

        self.assertThat(attr, IsInstance(Size))
Beispiel #6
0
    def test_point(self):
        data = dbus.Array([
            dbus.Int32(ValueType.POINT),
            dbus.Int32(0),
            dbus.Int32(10),
        ])

        attr = create_value_instance(data, None, None)

        self.assertThat(attr, IsInstance(Point))
Beispiel #7
0
    def test_color(self):
        data = dbus.Array([
            dbus.Int32(ValueType.COLOR),
            dbus.Int32(10),
            dbus.Int32(20),
            dbus.Int32(230),
            dbus.Int32(255),
        ])

        attr = create_value_instance(data, None, None)

        self.assertThat(attr, IsInstance(Color))
Beispiel #8
0
    def test_rectangle(self):
        data = dbus.Array([
            dbus.Int32(ValueType.RECTANGLE),
            dbus.Int32(0),
            dbus.Int32(10),
            dbus.Int32(20),
            dbus.Int32(30),
        ])

        attr = create_value_instance(data, None, None)

        self.assertThat(attr, IsInstance(Rectangle))
Beispiel #9
0
    def test_invalid_size(self):
        data = dbus.Array([
            dbus.Int32(ValueType.SIZE),
            dbus.Int32(0),
        ])

        fn = lambda: create_value_instance(data, None, None)

        self.assertThat(
            fn,
            raises(
                ValueError(
                    "Size must be constructed with 2 arguments, not 1")))
Beispiel #10
0
    def test_invalid_rectangle(self):
        data = dbus.Array([
            dbus.Int32(ValueType.RECTANGLE),
            dbus.Int32(0),
        ])

        fn = lambda: create_value_instance(data, None, None)

        self.assertThat(
            fn,
            raises(
                ValueError(
                    "Rectangle must be constructed with 4 arguments, not 1")))
Beispiel #11
0
    def test_invalid_point3d(self):
        data = dbus.Array([
            dbus.Int32(ValueType.POINT3D),
            dbus.Int32(0),
            dbus.Int32(0),
        ])

        fn = lambda: create_value_instance(data, None, None)

        self.assertThat(
            fn,
            raises(
                ValueError(
                    "Point3D must be constructed with 3 arguments, not 2")))
Beispiel #12
0
    def test_invalid_date_time(self):
        data = dbus.Array([
            dbus.Int32(ValueType.DATETIME),
            dbus.Int32(0),
            dbus.Int32(0),
            dbus.Int32(0),
        ])

        fn = lambda: create_value_instance(data, None, None)

        self.assertThat(
            fn,
            raises(
                ValueError(
                    "DateTime must be constructed with 1 arguments, not 3")))
Beispiel #13
0
    def test_unknown_type_id(self):
        """Unknown type Ids should result in a plain type, along with a log
        message.

        """

        data = dbus.Array([
            dbus.Int32(543),
            dbus.Int32(0),
            dbus.Boolean(False),
            dbus.String("Hello World")
        ])
        attr = create_value_instance(data, None, None)
        self.assertThat(attr, IsInstance(PlainType))
        self.assertThat(attr, IsInstance(dbus.Array))
        self.assertThat(attr, Equals([0, False, "Hello World"]))
Beispiel #14
0
    def test_plain_uint64(self):
        data = dbus.Array([dbus.Int32(ValueType.PLAIN), dbus.UInt64(2**40)])
        attr = create_value_instance(data, None, None)

        self.assertThat(attr, Equals(2**40))
        self.assertThat(attr, IsInstance(PlainType))
Beispiel #15
0
    def test_plain_boolean(self):
        data = dbus.Array([dbus.Int32(ValueType.PLAIN), dbus.Boolean(False)])
        attr = create_value_instance(data, None, None)

        self.assertThat(attr, Equals(False))
        self.assertThat(attr, IsInstance(PlainType))