Blame ibus/attribute.py

Packit 3ff832
# vim:set et sts=4 sw=4:
Packit 3ff832
#
Packit 3ff832
# ibus - The Input Bus
Packit 3ff832
#
Packit 3ff832
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
# Copyright (c) 2007-2010 Red Hat, Inc.
Packit 3ff832
#
Packit 3ff832
# This library is free software; you can redistribute it and/or
Packit 3ff832
# modify it under the terms of the GNU Lesser General Public
Packit 3ff832
# License as published by the Free Software Foundation; either
Packit 3ff832
# version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
#
Packit 3ff832
# This library is distributed in the hope that it will be useful,
Packit 3ff832
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
# Lesser General Public License for more details.
Packit 3ff832
#
Packit 3ff832
# You should have received a copy of the GNU Lesser General Public
Packit 3ff832
# License along with this library; if not, write to the Free Software
Packit 3ff832
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
# USA
Packit 3ff832
Packit 3ff832
__all__ = (
Packit 3ff832
        "ATTR_TYPE_UNDERLINE",
Packit 3ff832
        "ATTR_TYPE_FOREGROUND",
Packit 3ff832
        "ATTR_TYPE_BACKGROUND",
Packit 3ff832
        "ATTR_UNDERLINE_NONE",
Packit 3ff832
        "ATTR_UNDERLINE_SINGLE",
Packit 3ff832
        "ATTR_UNDERLINE_DOUBLE",
Packit 3ff832
        "ATTR_UNDERLINE_LOW",
Packit 3ff832
        "ATTR_UNDERLINE_ERROR",
Packit 3ff832
        "Attribute",
Packit 3ff832
        "AttributeUnderline",
Packit 3ff832
        "AttributeForeground",
Packit 3ff832
        "AttributeBackground",
Packit 3ff832
        "AttrList",
Packit 3ff832
        "ARGB", "RGB"
Packit 3ff832
    )
Packit 3ff832
Packit 3ff832
import dbus
Packit 3ff832
from exception import IBusException
Packit 3ff832
from serializable import *
Packit 3ff832
Packit 3ff832
ATTR_TYPE_UNDERLINE = 1
Packit 3ff832
ATTR_TYPE_FOREGROUND = 2
Packit 3ff832
ATTR_TYPE_BACKGROUND = 3
Packit 3ff832
Packit 3ff832
ATTR_UNDERLINE_NONE = 0
Packit 3ff832
ATTR_UNDERLINE_SINGLE = 1
Packit 3ff832
ATTR_UNDERLINE_DOUBLE = 2
Packit 3ff832
ATTR_UNDERLINE_LOW = 3
Packit 3ff832
ATTR_UNDERLINE_ERROR = 4
Packit 3ff832
Packit 3ff832
class Attribute(Serializable):
Packit 3ff832
    __gtype_name__ = "PYIBusAttribute"
Packit 3ff832
    __NAME__ = "IBusAttribute"
Packit 3ff832
    def __init__ (self, type=0, value=0, start_index=0, end_index=0):
Packit 3ff832
        super(Attribute, self).__init__()
Packit 3ff832
        self.__type = type
Packit 3ff832
        self.__value = value
Packit 3ff832
        self.__start_index = start_index
Packit 3ff832
        self.__end_index = end_index
Packit 3ff832
Packit 3ff832
    def get_type(self):
Packit 3ff832
        return self.__type
Packit 3ff832
Packit 3ff832
    def get_value(self):
Packit 3ff832
        return self.__value
Packit 3ff832
Packit 3ff832
    def get_start_index(self):
Packit 3ff832
        return self.__start_index
Packit 3ff832
Packit 3ff832
    def get_end_index(self):
Packit 3ff832
        return self.__end_index
Packit 3ff832
Packit 3ff832
    type        = property(get_type)
Packit 3ff832
    value       = property(get_value)
Packit 3ff832
    start_index = property(get_start_index)
Packit 3ff832
    end_index   = property(get_end_index)
Packit 3ff832
Packit 3ff832
    def serialize(self, struct):
Packit 3ff832
        super(Attribute, self).serialize(struct)
Packit 3ff832
        struct.append (dbus.UInt32(self.__type))
Packit 3ff832
        struct.append (dbus.UInt32(self.__value))
Packit 3ff832
        struct.append (dbus.UInt32(self.__start_index))
Packit 3ff832
        struct.append (dbus.UInt32(self.__end_index))
Packit 3ff832
Packit 3ff832
    def deserialize(self, struct):
Packit 3ff832
        super(Attribute, self).deserialize(struct)
Packit 3ff832
        if len(struct) < 4:
Packit 3ff832
            raise IBusException ("Can not deserialize IBusAttribute")
Packit 3ff832
Packit 3ff832
        self.__type = struct.pop(0)
Packit 3ff832
        self.__value = struct.pop(0)
Packit 3ff832
        self.__start_index = struct.pop(0)
Packit 3ff832
        self.__end_index = struct.pop(0)
Packit 3ff832
Packit 3ff832
class AttributeUnderline (Attribute):
Packit 3ff832
    def __init__(self, value, start_index, end_index):
Packit 3ff832
        Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
Packit 3ff832
Packit 3ff832
class AttributeForeground (Attribute):
Packit 3ff832
    def __init__(self, value, start_index, end_index):
Packit 3ff832
        Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)
Packit 3ff832
Packit 3ff832
class AttributeBackground (Attribute):
Packit 3ff832
    def __init__(self, value, start_index, end_index):
Packit 3ff832
        Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)
Packit 3ff832
Packit 3ff832
def ARGB (a, r, g, b):
Packit 3ff832
    return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
Packit 3ff832
Packit 3ff832
def RGB (r, g, b):
Packit 3ff832
    return ARGB (255, r, g, b)
Packit 3ff832
Packit 3ff832
class AttrList(Serializable):
Packit 3ff832
    __gtype_name__ = "PYIBusAttrList"
Packit 3ff832
    __NAME__ = "IBusAttrList"
Packit 3ff832
    def __init__ (self, attrs = []):
Packit 3ff832
        super(AttrList, self).__init__()
Packit 3ff832
        self._attrs = []
Packit 3ff832
        for attr in attrs:
Packit 3ff832
            self.append (attr)
Packit 3ff832
Packit 3ff832
    def append (self, attr):
Packit 3ff832
        assert isinstance (attr, Attribute)
Packit 3ff832
        self._attrs.append (attr)
Packit 3ff832
Packit 3ff832
    def serialize (self, struct):
Packit 3ff832
        super(AttrList, self).serialize (struct)
Packit 3ff832
        array = map (lambda a: serialize_object(a), self._attrs)
Packit 3ff832
        array = dbus.Array (array, signature = "v")
Packit 3ff832
        struct.append(array)
Packit 3ff832
Packit 3ff832
    def deserialize (self, struct):
Packit 3ff832
        super(AttrList, self).deserialize(struct)
Packit 3ff832
        attrs = map(lambda v: deserialize_object(v), struct.pop(0))
Packit 3ff832
        self._attrs = attrs
Packit 3ff832
Packit 3ff832
    def __iter__ (self):
Packit 3ff832
        return self._attrs.__iter__ ()
Packit 3ff832
Packit 3ff832
def test():
Packit 3ff832
    attr_list = AttrList()
Packit 3ff832
    attr_list.append (Attribute())
Packit 3ff832
    attr_list.append (Attribute())
Packit 3ff832
    attr_list.append (Attribute())
Packit 3ff832
    attr_list.append (Attribute())
Packit 3ff832
    attr_list.append (Attribute())
Packit 3ff832
    value = serialize_object(attr_list)
Packit 3ff832
    attr_list = deserialize_object(value)
Packit 3ff832
Packit 3ff832
if __name__ == "__main__":
Packit 3ff832
    test()