Use MetaClass to register serializable classes automatically
authorPeng Huang <shawn.p.huang@gmail.com>
Tue, 30 Jun 2009 08:45:45 +0000 (16:45 +0800)
committerPeng Huang <shawn.p.huang@gmail.com>
Tue, 30 Jun 2009 08:45:45 +0000 (16:45 +0800)
ibus/attribute.py
ibus/component.py
ibus/enginedesc.py
ibus/factory.py
ibus/lookuptable.py
ibus/observedpath.py
ibus/property.py
ibus/serializable.py
ibus/text.py

index 7ba3846..4976f0d 100644 (file)
@@ -92,8 +92,6 @@ class Attribute(Serializable):
         self.__start_index = struct.pop(0)
         self.__end_index = struct.pop(0)
 
-serializable_register(Attribute)
-
 class AttributeUnderline (Attribute):
     def __init__(self, value, start_index, end_index):
         Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
@@ -139,8 +137,6 @@ class AttrList(Serializable):
     def __iter__ (self):
         return self._attrs.__iter__ ()
 
-serializable_register(AttrList)
-
 def test():
     attr_list = AttrList()
     attr_list.append (Attribute())
index cb00da8..93bd804 100644 (file)
@@ -121,8 +121,6 @@ class Component(Serializable):
         self.__observed_paths = map(deserialize_object, struct.pop(0))
         self.__engines = map(deserialize_object, struct.pop(0))
 
-serializable_register(Component)
-
 def test():
     text = Component("Hello", "", "", "", "", "", "", "")
     value = serialize_object(text)
index 21c4be2..7a4f5d5 100644 (file)
@@ -96,8 +96,6 @@ class EngineDesc(Serializable):
         self.__icon = struct.pop(0)
         self.__layout = struct.pop(0)
 
-serializable_register(EngineDesc)
-
 def test():
     engine = EngineDesc("Hello", "", "", "", "", "", "", "")
     value = serialize_object(engine)
index 990767a..5bd969e 100644 (file)
@@ -130,5 +130,3 @@ class FactoryInfo(Serializable):
         self.__authors = struct.pop(0)
         self.__credits = struct.pop(0)
 
-serializable_register(FactoryInfo)
-
index ab758bc..c592ee3 100644 (file)
@@ -215,9 +215,6 @@ class LookupTable(Serializable):
         self.__candidates = map(deserialize_object, struct.pop(0))
         self.__labels = map(deserialize_object, struct.pop(0))
 
-
-serializable_register(LookupTable)
-
 def test():
     t = LookupTable()
     # attrs = AttrList()
index 67fd5f2..f5f68dd 100644 (file)
@@ -54,8 +54,6 @@ class ObservedPath(Serializable):
         self.__path = struct.pop(0)
         self.__mtime = struct.pop(0)
 
-serializable_register(ObservedPath)
-
 def test():
     op = ObservedPath("/tmp", 111)
     value = serialize_object(op)
index 118a673..f95499c 100644 (file)
@@ -174,8 +174,6 @@ class Property(Serializable):
 
         self.__sub_props = deserialize_object(props)
 
-serializable_register(Property)
-
 class PropList(Serializable):
     __gtype_name__ = "IBusPropList"
     __NAME__ = "IBusPropList"
@@ -220,8 +218,6 @@ class PropList(Serializable):
     def __getitem__(self, i):
         return self.__props.__getitem__(i)
 
-serializable_register(PropList)
-
 def test():
     props = PropList()
     props.append(Property(u"a"))
index 6779ba6..ad0b066 100644 (file)
@@ -20,7 +20,6 @@
 # Boston, MA  02111-1307  USA
 
 __all__ = (
-        "serializable_register",
         "Serializable",
         "serialize_object",
         "deserialize_object",
@@ -33,8 +32,8 @@ import gobject
 __serializable_name_dict = dict()
 
 def serializable_register(classobj):
-    if not issubclass(classobj, Serializable):
-        raise "%s is not a sub-class of Serializable" % str(classobj)
+    if not issubclass(classobj, Serializable):
+        raise "%s is not a sub-class of Serializable" % str(classobj)
     __serializable_name_dict[classobj.__NAME__] = classobj
 
 def serialize_object(o):
@@ -55,7 +54,14 @@ def deserialize_object(v):
         return o
     return v
 
+class SerializableMeta(gobject.GObjectMeta):
+    def __init__(cls, name, bases, dict_):
+        super(SerializableMeta, cls).__init__(name, bases, dict_)
+        if "__NAME__" in cls.__dict__:
+            serializable_register(cls)
+
 class Serializable(Object):
+    __metaclass__ = SerializableMeta
     __gtype_name__ = "IBusSerializable"
     __NAME__ = "IBusSerializable"
     def __init__(self):
index e5a132e..952c9d9 100644 (file)
@@ -58,8 +58,6 @@ class Text(Serializable):
         self.__text = struct.pop(0)
         self.__attrs = deserialize_object(struct.pop(0))
 
-serializable_register(Text)
-
 def test():
     text = Text("Hello")
     value = serialize_object(text)