Package pype32 :: Module baseclasses
[hide private]
[frames] | no frames]

Source Code for Module pype32.baseclasses

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*-  
  3   
  4  # Copyright (c) 2013, Nahuel Riva  
  5  # All rights reserved.  
  6  #  
  7  # Redistribution and use in source and binary forms, with or without  
  8  # modification, are permitted provided that the following conditions are met:  
  9  #  
 10  #     * Redistributions of source code must retain the above copyright notice,  
 11  #       this list of conditions and the following disclaimer.  
 12  #     * Redistributions in binary form must reproduce the above copyright  
 13  #       notice,this list of conditions and the following disclaimer in the  
 14  #       documentation and/or other materials provided with the distribution.  
 15  #     * Neither the name of the copyright holder nor the names of its  
 16  #       contributors may be used to endorse or promote products derived from  
 17  #       this software without specific prior written permission.  
 18  #  
 19  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
 20  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  
 21  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  
 22  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE  
 23  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  
 24  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  
 25  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  
 26  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  
 27  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
 28  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
 29  # POSSIBILITY OF SUCH DAMAGE.  
 30   
 31  """ 
 32  Base classes. 
 33  """ 
 34   
 35  __revision__ = "$Id$" 
 36   
37 -class BaseStructClass(object):
38 """ Base class containing methods used by many others classes in the library."""
39 - def __init__(self, shouldPack = True):
40 """ 41 @type shouldPack: bool 42 @param shouldPack: (Optional) If the value is set to C{True}, the class will be packed. If the value is 43 set to C{False}, the class will not be packed. 44 """ 45 self.shouldPack = shouldPack 46 self._attrsList = []
47
48 - def __str__(self):
49 s = "" 50 for i in self._attrsList: 51 attr = getattr(self, i) 52 if hasattr(attr, "shouldPack") and attr.shouldPack: 53 s += str(attr) 54 return s
55
56 - def __len__(self):
57 return len(str(self))
58
59 - def __dir__(self):
60 return sorted(self._attrsList or self.__dict__.keys())
61
62 - def sizeof(self):
63 return len(self)
64
65 - def getFields(self):
66 """ 67 Returns all the class attributues. 68 69 @rtype: dict 70 @return: A dictionary containing all the class attributes. 71 """ 72 d = {} 73 for i in self._attrsList: 74 key = i 75 value = getattr(self, i) 76 d[key] = value 77 return d
78
79 - def getType(self):
80 """ 81 This method should be implemented in the inherited classes. When implemented, returns 82 an integer value to identified the corresponding class. 83 84 @raise NotImplementedError: The method wasn't implemented in the inherited class. 85 """ 86 raise NotImplementedError("getType() method not implemented.")
87
88 -class DataTypeBaseClass(object):
89 - def __init__(self, value = 0, endianness = "<", signed = False, shouldPack = True):
90 """ 91 @type value: int 92 @param value: The value used to build the L{BYTE} object. 93 94 @type endianness: str 95 @param endianness: (Optional) Indicates the endianness of the L{BYTE} object. The C{<} indicates little-endian while C{>} indicates big-endian. 96 97 @type signed: bool 98 @param signed: (Optional) If set to C{True} the L{BYTE} object will be packed as signed. If set to C{False} it will be packed as unsigned. 99 100 @type shouldPack: bool 101 @param shouldPack: (Optional) If set to c{True}, the object will be packed. If set to C{False}, the object won't be packed. 102 """ 103 self.value = value 104 self.endianness = endianness 105 self.signed = signed 106 self.shouldPack = shouldPack
107
108 - def __dir__(self):
109 return sorted(self.__dict__.keys())
110
111 - def __eq__(self, other):
112 result = None 113 114 if isinstance(other, self.__class__): 115 result = self.value == other.value 116 else: 117 result = self.value == other 118 return result
119
120 - def __ne__(self, other):
121 result = None 122 123 if isinstance(other, self.__class__): 124 result = self.value != other.value 125 else: 126 result = self.value != other 127 return result
128
129 - def __lt__(self, other):
130 result = None 131 132 if isinstance(other, self.__class__): 133 result = self.value < other.value 134 else: 135 result = self.value < other 136 return result
137
138 - def __gt__(self, other):
139 result = None 140 141 if isinstance(other, self.__class__): 142 result = self.value > other.value 143 else: 144 result = self.value > other 145 return result
146
147 - def __le__(self, other):
148 result = None 149 150 if isinstance(other, self.__class__): 151 result = self.value <= other.value 152 else: 153 result = self.value <= other 154 return result
155
156 - def __ge__(self, other):
157 result = None 158 159 if isinstance(other, self.__class__): 160 result = self.value >= other.value 161 else: 162 result = self.value >= other 163 return result
164
165 - def __add__(self, other):
166 result = None 167 168 if isinstance(other, self.__class__): 169 try: 170 result = self.value + other.value 171 except TypeError, e: 172 raise e 173 else: 174 try: 175 result = self.value + other 176 except TypeError, e: 177 raise e 178 return result
179
180 - def __sub__(self, other):
181 result = None 182 if isinstance(other, self.__class__): 183 try: 184 result = self.value - other.value 185 except TypeError, e: 186 raise e 187 else: 188 try: 189 result = self.value - other 190 except TypeError, e: 191 raise e 192 return result
193
194 - def __mul__(self, other):
195 result = None 196 if isinstance(other, self.__class__): 197 result = self.value * other.value 198 else: 199 try: 200 result = self.value * other 201 except TypeError, e: 202 raise e 203 return result
204
205 - def __div__(self, other):
206 result = None 207 if isinstance(other, self.__class__): 208 try: 209 result = self.value / other.value 210 except (TypeError, ZeroDivisionError) as e: 211 raise e 212 else: 213 try: 214 result = self.value / other 215 except (TypeError, ZeroDivisionError) as e: 216 raise e 217 return result
218
219 - def __xor__(self, other):
220 result = None 221 if isinstance(other, self.__class__): 222 result = self.value ^ other.value 223 else: 224 try: 225 result = self.value ^ other 226 except TypeError, e: 227 raise e 228 return result
229
230 - def __rshift__(self, other):
231 result = None 232 if isinstance(other, self.__class__): 233 result = self.value >> other.value 234 else: 235 try: 236 result = self.value >> other 237 except TypeError, e: 238 raise e 239 return result
240
241 - def __lshift__(self, other):
242 result = None 243 if isinstance(other, self.__class__): 244 result = self.value << other.value 245 else: 246 try: 247 result = self.value << other 248 except TypeError, e: 249 raise e 250 return result
251
252 - def __and__(self, other):
253 result = None 254 if isinstance(other, self.__class__): 255 result = self.value & other.value 256 else: 257 try: 258 result = self.value & other 259 except TypeError, e: 260 raise e 261 return result
262
263 - def __or__(self, other):
264 result = None 265 if isinstance(other, self.__class__): 266 result = self.value | other.value 267 else: 268 try: 269 result = self.value | other 270 except TypeError, e: 271 raise e 272 return result
273