Module PyEnum
[hide private]
[frames] | no frames]

Source Code for Module PyEnum

  1  #!/usr/bin/env python 
  2   
  3  """ 
  4  @copyright: Copyright (C) 2008, 2010 Oracle and/or its affiliates. All rights reserved. 
  5   
  6  @license:   See the file COPYING for redistribution information. 
  7   
  8  @summary:   Simulate the classic C enum type (with some Pythonic improvements). 
  9  """ 
 10   
 11  import re 
 12   
13 -class EnumUpdateError(Exception):
14 """ 15 Exception raised when trying update any PyEnum attributes directly. 16 """
17 - def __init__(self, message):
18 """ 19 @param message: Error message for the exception. 20 @type message: C{str} 21 """ 22 self.message = message
23
24 -class PyEnum(object):
25 """ 26 Class that simulate the C enum type 27 28 @note: PyEnum does not allow general access to the class variables 29 """
30 - def __init__(self, *enum_ids):
31 """ 32 Create the enum instance. 33 34 @param enum_ids: List of the enum identifiers, an optional value 35 can be associated with the key in the form of 36 name=value 37 @type enum_ids: C{list} 38 """ 39 enum_value = 0 40 enum_re = re.compile("\s*[=]\s*") 41 self.__enum_ids__ = {} 42 for enum_id in enum_ids: 43 enum_pair = enum_re.split(enum_id.strip()) 44 if len(enum_pair) < 1 or len(enum_pair) > 2: 45 raise SyntaxError("invalid enum value syntax") 46 47 elif len(enum_pair) == 2: 48 enum_value = int(enum_pair[1], 0) 49 50 self.__enum_ids__[enum_pair[0]] = enum_value 51 enum_value += 1
52
53 - def __getattr__(self, enum_id):
54 """ 55 Get the value for a specific enum id. 56 57 @param enum_id: Identifier of the enum value to retrieve. 58 @type enum_id: C{str} 59 60 @return: The enum value. 61 @rtype: C{int} 62 63 @raise AttributeError: on failure 64 """ 65 try: 66 return self.__enum_ids__[enum_id] 67 68 except: 69 raise AttributeError
70
71 - def __getitem__(self, enum_id):
72 """ 73 Get the value for a specific enum id. 74 75 @param enum_id: Identifier of the enum value to retrieve 76 @type enum_id: C{str} 77 78 @return: The enum value. 79 @rtype: C{int} 80 81 @raise AttributeError: on failure 82 """ 83 if type(enum_id) == str: 84 return self.__getattr__(enum_id) 85 else: 86 for enum_key in self.__enum_ids__: 87 if self.__enum_ids__[enum_key] == enum_id: 88 return enum_key
89
90 - def __setattr__(self, attr_name, attr_val):
91 """ 92 Set the value for a specific enum id. 93 94 @param attr_name: Identifier of the attribute to set 95 @type attr_name: C{str} 96 97 @param attr_val: Object to assing to the attribute 98 @type attr_val: C{object} 99 100 @note: PyEnum does not allow updating the enums, it will raise 101 EnumUpdateError. 102 103 @raise EnumUpdateError: on failure 104 """ 105 if attr_name == "__enum_ids__" or \ 106 attr_name == "__enum_iter__": 107 self.__dict__[attr_name] = attr_val 108 109 else: 110 raise EnumUpdateError("No changes to %s allowed" % attr_name)
111
112 - def __iter__(self):
113 """ 114 Get an iterator object. 115 116 @return: Iterator object. 117 @rtype: C{iter} 118 """ 119 self.__enum_iter__ = self.__enum_ids__.__iter__() 120 return self
121
122 - def next(self):
123 """ 124 Get the next iterator value. 125 126 @return: Next iterator object. 127 @rtype: C{iter} 128 """ 129 return self.__enum_iter__.next()
130
131 - def getID(self, enum_value):
132 """ 133 Get the the id for a specific value. 134 @note: The ID of the first matching value will be returned. 135 136 @param enum_value: Enum value to get the identifier for. 137 @type enum_value: C{str} 138 139 @return: The enum identifier. 140 @rtype: C{str} 141 142 @raise AttributeError: on failure 143 """ 144 try: 145 for enum_key in self.__enum_ids__: 146 if self.__enum_ids__[enum_key] == enum_value: 147 return enum_key 148 149 except: 150 raise AttributeError
151
152 - def dump(self):
153 """ 154 Dump all the enum identifier and values. 155 """ 156 for enum_id in self.__enum_ids__.keys(): 157 print "%s=%s" % (enum_id, self.__enum_ids__[enum_id])
158
159 - def toString(self):
160 """ 161 Return all the enum identifier and values in a comma delimited string. 162 """ 163 enum_str = "" 164 for enum_id in self.__enum_ids__.keys(): 165 enum_str = enum_str + "%s=%s," % (enum_id, 166 self.__enum_ids__[enum_id]) 167 168 return enum_str.rstrip(',')
169