Package flumotion :: Package common :: Module enum
[hide private]

Source Code for Module flumotion.common.enum

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_enum -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """simple enum and implementation 
 23  """ 
 24   
 25  from twisted.python.reflect import qual 
 26  from twisted.spread import jelly 
 27   
 28  __version__ = "$Rev: 6964 $" 
 29  _enumClassRegistry = {} 
 30   
 31   
32 -class EnumMetaClass(type):
33 # pychecker likes this attribute to be there since we use it in this class 34 __enums__ = {} 35
36 - def __len__(self):
37 return len(self.__enums__)
38
39 - def __getitem__(self, value):
40 try: 41 return self.__enums__[value] 42 except KeyError: 43 raise StopIteration
44
45 - def __setitem__(self, value, enum):
46 self.__enums__[value] = enum 47 setattr(self, enum.name, enum)
48 49
50 -class Enum(object, jelly.Jellyable):
51 52 __metaclass__ = EnumMetaClass
53 - def __init__(self, value, name, nick=None):
54 self.value = value 55 self.name = name 56 57 if nick == None: 58 nick = name 59 60 self.nick = nick 61 self._enumClassName = self.__class__.__name__
62
63 - def __repr__(self):
64 return '<enum %s of type %s>' % (self.name, 65 self.__class__.__name__)
66
67 - def get(klass, value):
68 return klass.__enums__[value]
69 get = classmethod(get) 70
71 - def set(klass, value, item):
72 klass[value] = item
73 set = classmethod(set) 74
75 - def jellyFor(self, jellier):
76 sxp = jellier.prepare(self) 77 sxp.extend([ 78 qual(Enum), 79 self._enumClassName, 80 self.value, self.name, self.nick]) 81 return jellier.preserve(self, sxp)
82 83
84 -class EnumClass(object):
85 - def __new__(klass, type_name, names=(), nicks=(), **extras):
86 if nicks: 87 if len(names) != len(nicks): 88 raise TypeError("nicks must have the same length as names") 89 else: 90 nicks = names 91 92 for extra in extras.values(): 93 if not isinstance(extra, (tuple, list)): 94 raise TypeError('extra must be a sequence, not %s' % type(extra)) 95 96 if len(extra) != len(names): 97 raise TypeError("extra items must have a length of %d" % 98 len(names)) 99 100 # we reset __enums__ to {} otherwise it retains the other registered 101 # ones 102 etype = EnumMetaClass(type_name, (Enum,), {'__enums__': {}}) 103 for value, name in enumerate(names): 104 enum = etype(value, name, nicks[value]) 105 for extra_key, extra_values in extras.items(): 106 assert not hasattr(enum, extra_key) 107 setattr(enum, extra_key, extra_values[value]) 108 etype[value] = enum 109 110 _enumClassRegistry[type_name] = etype 111 return etype
112 113 114 # Enum unjellyer should not be a new style class, 115 # otherwise Twsited 2.0.1 will not recognise it.
116 -class EnumUnjellyer(jelly.Unjellyable):
117
118 - def unjellyFor(self, unjellier, jellyList):
119 enumClassName, value, name, nick = jellyList[1:] 120 enumClass = _enumClassRegistry.get(enumClassName, None) 121 if enumClass: 122 # Retrieve the enum singleton 123 enum = enumClass.get(value) 124 assert enum.name == name, "Inconsistent Enum Name" 125 else: 126 # Create a generic Enum container 127 enum = Enum(value, name, nick) 128 enum._enumClassName = enumClassName 129 return enum
130 131 132 jelly.setUnjellyableForClass(qual(Enum), EnumUnjellyer) 133