| Trees | Indices | Help |
|---|
|
|
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 """
23 Enum class implementation
24 """
25
26 from twisted.python.reflect import qual
27 from twisted.spread import jelly
28
29 _enumClassRegistry = {}
30
32 # pychecker likes this attribute to be there since we use it in this class
33 __enums__ = {}
34
36 return len(self.__enums__)
37
43
47
48
50
51 __metaclass__ = EnumMetaClass
53 self.value = value
54 self.name = name
55
56 if nick == None:
57 nick = name
58
59 self.nick = nick
60 self._enumClassName = self.__class__.__name__
61
65
68 get = classmethod(get)
69
71 klass[value] = item
72 set = classmethod(set)
73
81
82
85 if nicks:
86 if len(names) != len(nicks):
87 raise TypeError("nicks must have the same length as names")
88 else:
89 nicks = names
90
91 for extra in extras.values():
92 if not isinstance(extra, tuple):
93 raise TypeError('extra must be a tuple, not %s' % type(extra))
94
95 if len(extra) != len(names):
96 raise TypeError("extra items must have a length of %d" %
97 len(names))
98
99 # we reset __enums__ to {} otherwise it retains the other registered
100 # ones
101 etype = EnumMetaClass(type_name, (Enum,), {'__enums__': {}})
102 for value, name in enumerate(names):
103 enum = etype(value, name, nicks[value])
104 for extra_key, extra_values in extras.items():
105 assert not hasattr(enum, extra_key)
106 setattr(enum, extra_key, extra_values[value])
107 etype[value] = enum
108
109 _enumClassRegistry[type_name] = etype
110 return etype
111
112
113 # Enum unjellyer should not be a new style class,
114 # otherwise Twsited 2.0.1 will not recognise it.
116
118 enumClassName, value, name, nick = jellyList[1:]
119 enumClass = _enumClassRegistry.get(enumClassName, None)
120 if enumClass:
121 # Retrieve the enum singleton
122 enum = enumClass.get(value)
123 assert enum.name == name, "Inconsistent Enum Name"
124 else:
125 # Create a generic Enum container
126 enum = Enum(value, name, nick)
127 enum._enumClassName = enumClassName
128 return enum
129
130
131 jelly.setUnjellyableForClass(qual(Enum), EnumUnjellyer)
132
| Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Thu Aug 7 15:45:50 2008 | http://epydoc.sourceforge.net |