sebastien - in flumotion/trunk: . flumotion/common flumotion/test

flumotion-commit at lists.fluendo.com flumotion-commit at lists.fluendo.com
Wed May 9 19:06:45 CEST 2007


Author: sebastien
Date: Wed May  9 19:06:43 2007
New Revision: 4921

Modified:
   flumotion/trunk/ChangeLog
   flumotion/trunk/flumotion/common/enum.py
   flumotion/trunk/flumotion/test/test_enum.py
Log:
2007-03-30  Sebastien Merle  <sebastien at fluendo.com>

	* flumotion/common/enum.py: 
	Modified the Enum jellying for the enum to be unjellyed
	even without knowing the custom enum class.
	It allow a peer to proxy an enum value without having to 
	know about the enum custom class.
	In practice it allow a component to declar custom enums,
	and an admin/ui to use it without the manager knowing about it.

2007-03-29  Sebastien Merle  <sebastien at fluendo.com>

	* flumotion/test/test_enum.py: 
	* flumotion/common/enum.py: 
	Added the ability for the enum values (not the proper enum) to
	be jellyed and unjellyed. Now an enum value can be directly 
	used with spread without converting to int.


Modified: flumotion/trunk/ChangeLog
==============================================================================
--- flumotion/trunk/ChangeLog	(original)
+++ flumotion/trunk/ChangeLog	Wed May  9 19:06:43 2007
@@ -1,3 +1,21 @@
+2007-03-30  Sebastien Merle  <sebastien at fluendo.com>
+
+	* flumotion/common/enum.py: 
+	Modified the Enum jellying for the enum to be unjellyed
+	even without knowing the custom enum class.
+	It allow a peer to proxy an enum value without having to 
+	know about the enum custom class.
+	In practice it allow a component to declar custom enums,
+	and an admin/ui to use it without the manager knowing about it.
+
+2007-03-29  Sebastien Merle  <sebastien at fluendo.com>
+
+	* flumotion/test/test_enum.py: 
+	* flumotion/common/enum.py: 
+	Added the ability for the enum values (not the proper enum) to
+	be jellyed and unjellyed. Now an enum value can be directly 
+	used with spread without converting to int.
+
 2007-05-09  Zaheer Abbas Merali  <<zaheerabbas at merali dot org>>
 
 	* flumotion/test/test_manager_depgraph.py

Modified: flumotion/trunk/flumotion/common/enum.py
==============================================================================
--- flumotion/trunk/flumotion/common/enum.py	(original)
+++ flumotion/trunk/flumotion/common/enum.py	Wed May  9 19:06:43 2007
@@ -23,6 +23,10 @@
 Enum class implementation
 """
 
+from twisted.python.reflect import qual
+from twisted.spread import jelly
+
+_enumClassRegistry = {}
 
 class EnumMetaClass(type):
     # pychecker likes this attribute to be there since we use it in this class
@@ -41,7 +45,8 @@
         self.__enums__[value] = enum
         setattr(self, enum.name, enum)
 
-class Enum(object):
+
+class Enum(object, jelly.Jellyable, jelly.Unjellyable):
     __metaclass__ = EnumMetaClass
     def __init__(self, value, name, nick=None):
         self.value = value
@@ -51,6 +56,7 @@
             nick = name
             
         self.nick = nick
+        self._enumClassName = self.__class__.__name__
 
     def __repr__(self):
         return '<enum %s of type %s>' % (self.name,
@@ -63,6 +69,28 @@
     def set(klass, value, item):
         klass[value] = item
     set = classmethod(set)
+    
+    def jellyFor(self, jellier):
+        sxp = jellier.prepare(self)
+        sxp.extend([
+            qual(Enum),
+            self._enumClassName,
+            self.value, self.name, self.nick])
+        return jellier.preserve(self, sxp)
+
+    def unjellyFor(self, unjellier, jellyList):
+        enumClassName, value, name, nick = jellyList[1:]
+        enumClass = _enumClassRegistry.get(enumClassName, None)
+        if enumClass:
+            enum = enumClass.get(value)
+            assert enum.name == name, "Inconsistent Enum Name"
+            return enum
+        self._enumClassName = enumClassName
+        self.value = value
+        self.name = name
+        self.nick = nick
+        return self
+
 
 class EnumClass(object):
     def __new__(klass, type_name, names=(), nicks=(), **extras):
@@ -90,4 +118,7 @@
                 setattr(enum, extra_key, extra_values[value])
             etype[value] = enum
             
+        _enumClassRegistry[type_name] = etype
         return etype
+
+jelly.setUnjellyableForClass(qual(Enum), Enum)

Modified: flumotion/trunk/flumotion/test/test_enum.py
==============================================================================
--- flumotion/trunk/flumotion/test/test_enum.py	(original)
+++ flumotion/trunk/flumotion/test/test_enum.py	Wed May  9 19:06:43 2007
@@ -22,6 +22,7 @@
 from twisted.trial import unittest
 
 from flumotion.common import enum
+from twisted.spread import jelly
 
 class TestEnum(unittest.TestCase):
     def testEnumSimple(self):
@@ -118,3 +119,10 @@
         a = enum.EnumClass('FooType', ('Foo', 'Bar'))
         self.failUnless(repr(a.Foo))
         self.failUnless(isinstance(repr(a.Foo), str))
+
+    def testJelly(self):
+        a = enum.EnumClass('FooType', ('Foo', 'Bar'))
+        self.assertEquals(jelly.unjelly(jelly.jelly(a.Foo)), a.Foo)
+        self.assertEquals(jelly.unjelly(jelly.jelly(a.Bar)), a.Bar)
+        self.assertNotEquals(jelly.unjelly(jelly.jelly(a.Foo)), a.Bar)
+        self.assertNotEquals(jelly.unjelly(jelly.jelly(a.Bar)), a.Foo)


More information about the flumotion-commit mailing list