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 attr.shouldPack: 53 s += str(attr) 54 return s
55
56 - def __len__(self):
57 return len(str(self))
58
59 - def sizeof(self):
60 return len(self)
61
62 - def getFields(self):
63 """ 64 Returns all the class attributues. 65 66 @rtype: dict 67 @return: A dictionary containing all the class attributes. 68 """ 69 d = {} 70 for i in self._attrsList: 71 key = i 72 value = getattr(self, i) 73 d[key] = value 74 return d
75
76 - def getType(self):
77 """ 78 This method should be implemented in the inherited classes. When implemented, returns 79 an integer value to identified the corresponding class. 80 81 @raise NotImplementedError: The method wasn't implemented in the inherited class. 82 """ 83 raise NotImplementedError("getType() method not implemented.")
84
85 -class DataTypeBaseClass(object):
86 - def __init__(self, value = 0, endianness = "<", signed = False, shouldPack = True):
87 """ 88 @type value: int 89 @param value: The value used to build the L{BYTE} object. 90 91 @type endianness: str 92 @param endianness: (Optional) Indicates the endianness of the L{BYTE} object. The C{<} indicates little-endian while C{>} indicates big-endian. 93 94 @type signed: bool 95 @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. 96 97 @type shouldPack: bool 98 @param shouldPack: (Optional) If set to c{True}, the object will be packed. If set to C{False}, the object won't be packed. 99 """ 100 self.value = value 101 self.endianness = endianness 102 self.signed = signed 103 self.shouldPack = shouldPack
104
105 - def __eq__(self, other):
106 result = None 107 108 if isinstance(other, self.__class__): 109 result = self.value == other.value 110 else: 111 result = self.value == other 112 return result
113
114 - def __ne__(self, other):
115 result = None 116 117 if isinstance(other, self.__class__): 118 result = self.value != other.value 119 else: 120 result = self.value != other 121 return result
122
123 - def __lt__(self, other):
124 result = None 125 126 if isinstance(other, self.__class__): 127 result = self.value < other.value 128 else: 129 result = self.value < other 130 return result
131
132 - def __gt__(self, other):
133 result = None 134 135 if isinstance(other, self.__class__): 136 result = self.value > other.value 137 else: 138 result = self.value > other 139 return result
140
141 - def __le__(self, other):
142 result = None 143 144 if isinstance(other, self.__class__): 145 result = self.value <= other.value 146 else: 147 result = self.value <= other 148 return result
149
150 - def __ge__(self, other):
151 result = None 152 153 if isinstance(other, self.__class__): 154 result = self.value >= other.value 155 else: 156 result = self.value >= other 157 return result
158
159 - def __add__(self, other):
160 result = None 161 162 if isinstance(other, self.__class__): 163 try: 164 result = self.value + other.value 165 except TypeError, e: 166 raise e 167 else: 168 try: 169 result = self.value + other 170 except TypeError, e: 171 raise e 172 return result
173
174 - def __sub__(self, other):
175 result = None 176 if isinstance(other, self.__class__): 177 try: 178 result = self.value - other.value 179 except TypeError, e: 180 raise e 181 else: 182 try: 183 result = self.value - other 184 except TypeError, e: 185 raise e 186 return result
187
188 - def __mul__(self, other):
189 result = None 190 if isinstance(other, self.__class__): 191 result = self.value * other.value 192 else: 193 try: 194 result = self.value * other 195 except TypeError, e: 196 raise e 197 return result
198
199 - def __div__(self, other):
200 result = None 201 if isinstance(other, self.__class__): 202 try: 203 result = self.value / other.value 204 except (TypeError, ZeroDivisionError) as e: 205 raise e 206 else: 207 try: 208 result = self.value / other 209 except (TypeError, ZeroDivisionError) as e: 210 raise e 211 return result
212
213 - def __xor__(self, other):
214 result = None 215 if isinstance(other, self.__class__): 216 result = self.value ^ other.value 217 else: 218 try: 219 result = self.value ^ other 220 except TypeError, e: 221 raise e 222 return result
223
224 - def __rshift__(self, other):
225 result = None 226 if isinstance(other, self.__class__): 227 result = self.value >> other.value 228 else: 229 try: 230 result = self.value >> other 231 except TypeError, e: 232 raise e 233 return result
234
235 - def __lshift__(self, other):
236 result = None 237 if isinstance(other, self.__class__): 238 result = self.value << other.value 239 else: 240 try: 241 result = self.value << other 242 except TypeError, e: 243 raise e 244 return result
245
246 - def __and__(self, other):
247 result = None 248 if isinstance(other, self.__class__): 249 result = self.value & other.value 250 else: 251 try: 252 result = self.value & other 253 except TypeError, e: 254 raise e 255 return result
256
257 - def __or__(self, other):
258 result = None 259 if isinstance(other, self.__class__): 260 result = self.value | other.value 261 else: 262 try: 263 result = self.value | other 264 except TypeError, e: 265 raise e 266 return result
267