Subversion Repositories freemyipod

Rev

Rev 501 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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