Index: emcore/trunk/tools/misc.py |
— | — | @@ -27,6 +27,8 @@ |
28 | 28 | """
|
29 | 29 |
|
30 | 30 | import sys
|
| 31 | +from ctypes import *
|
| 32 | +from _ctypes import _SimpleCData
|
31 | 33 | import libemcoredata
|
32 | 34 |
|
33 | 35 | class Logger(object):
|
— | — | @@ -97,6 +99,105 @@ |
98 | 100 | self.__dict__ = self
|
99 | 101 |
|
100 | 102 |
|
| 103 | +class c_enum(_SimpleCData):
|
| 104 | + """
|
| 105 | + Resembles the enum datatype from C with an 8 bit size.
|
| 106 | + Returns the associated string of a value with c_enum[i]
|
| 107 | + Returns the current value of the associated value as c_enum.__repr__()
|
| 108 | + Comparison operators work with strings and values at the same time.
|
| 109 | +
|
| 110 | + ATTENTION: You can not really see if this is initialized or not.
|
| 111 | + If it is uninitialized it will return the first entry of the enum.
|
| 112 | + While this may be circumvented by changing the default value to
|
| 113 | + something else this will not work if the enum is placed inside a
|
| 114 | + ctypes structure as the __init__() method will not be called then.
|
| 115 | + """
|
| 116 | + _type_ = c_uint8._type_
|
| 117 | +
|
| 118 | + def __init__(self, value = 0):
|
| 119 | + if type(value) == str:
|
| 120 | + value = getattr(self, value)
|
| 121 | + _SimpleCData.__init__(self, value)
|
| 122 | + self[value]
|
| 123 | +
|
| 124 | + def __getattr__(self, name):
|
| 125 | + if name == "value":
|
| 126 | + return self.value
|
| 127 | + for key, value in enumerate(self._fields_):
|
| 128 | + if value == name:
|
| 129 | + return key
|
| 130 | +
|
| 131 | + def __getitem__(self, lookupkey):
|
| 132 | + for key, value in enumerate(self._fields_):
|
| 133 | + if key == lookupkey:
|
| 134 | + return value
|
| 135 | + raise IndexError("Value %d not in range of possible enum values for %s!" % (lookupkey, self.__class__.__name__))
|
| 136 | +
|
| 137 | + def __str__(self):
|
| 138 | + return self[self.value]
|
| 139 | +
|
| 140 | + def __repr__(self):
|
| 141 | + return self.__str__()
|
| 142 | +
|
| 143 | + def __eq__(self, other):
|
| 144 | + if type(other) == str:
|
| 145 | + try: return getattr(self, other) == self.value
|
| 146 | + except AttributeError: return False
|
| 147 | + else:
|
| 148 | + return self.value == other
|
| 149 | +
|
| 150 | + def __lt__(self, other):
|
| 151 | + if type(other) == str:
|
| 152 | + try: return self.value < getattr(self, other)
|
| 153 | + except AttributeError: return False
|
| 154 | + else:
|
| 155 | + return self.value < other
|
| 156 | +
|
| 157 | + def __gt__(self, other):
|
| 158 | + if type(other) == str:
|
| 159 | + try: return self.value > getattr(self, other)
|
| 160 | + except AttributeError: return False
|
| 161 | + else:
|
| 162 | + return self.value > other
|
| 163 | +
|
| 164 | + def __le__(self, other):
|
| 165 | + if self.value == other or self.value < other:
|
| 166 | + return True
|
| 167 | + return False
|
| 168 | +
|
| 169 | + def __ge__(self, other):
|
| 170 | + if self.value == other or self.value > other:
|
| 171 | + return True
|
| 172 | + return False
|
| 173 | +
|
| 174 | + def __ne__(self, other):
|
| 175 | + if self.value == other:
|
| 176 | + return False
|
| 177 | + return True
|
| 178 | +
|
| 179 | +
|
| 180 | +class ExtendedCStruct(LittleEndianStructure):
|
| 181 | + """
|
| 182 | + This is a subclass of the LittleEndianStructure.
|
| 183 | + It implements functions to easily convert
|
| 184 | + structures to/from strings and Bunches.
|
| 185 | + """
|
| 186 | + def _from_bunch(self, bunch):
|
| 187 | + for field, _ in self._fields_:
|
| 188 | + if field in bunch:
|
| 189 | + setattr(self, field, getattr(bunch, field))
|
| 190 | +
|
| 191 | + def _to_bunch(self):
|
| 192 | + return Bunch(**{field: getattr(self, field) for field, _ in self._fields_})
|
| 193 | +
|
| 194 | + def _from_string(self, string):
|
| 195 | + memmove(addressof(self), string, sizeof(self))
|
| 196 | +
|
| 197 | + def _to_string(self):
|
| 198 | + return string_at(addressof(self), sizeof(self))
|
| 199 | +
|
| 200 | +
|
| 201 | +
|
101 | 202 | class Error(Exception):
|
102 | 203 | def __init__(self, value=None):
|
103 | 204 | self.value = value
|