Changeset 00c2de63 in mainline for tools/xstruct.py
- Timestamp:
- 2011-07-29T14:50:22Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b6759f4
- Parents:
- 6c69d19 (diff), 7ae249d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/xstruct.py
r6c69d19 r00c2de63 1 1 # 2 2 # Copyright (c) 2008 Martin Decky 3 # Copyright (c) 2011 Martin Sucha 3 4 # All rights reserved. 4 5 # … … 31 32 32 33 import struct 34 import types 35 36 ranges = { 37 'B': ((int, long), 0x00, 0xff), 38 'H': ((int, long), 0x0000, 0xffff), 39 'L': ((int, long), 0x00000000, 0xffffffff), 40 'Q': ((int, long), 0x0000000000000000, 0xffffffffffffffff), 41 'b': ((int, long), -0x80, 0x7f), 42 'h': ((int, long), -0x8000, 0x7fff), 43 'l': ((int, long), -0x80000000, 0x7fffffff) , 44 'q': ((int, long), -0x8000000000000000, 0x7fffffffffffffff), 45 } 46 47 def check_range(varname, fmt, value): 48 if value == None: 49 raise ValueError('Variable "%s" not set' % varname) 50 if not fmt in ranges: 51 return 52 vartype, varmin, varmax = ranges[fmt] 53 if not isinstance(value, vartype): 54 raise ValueError('Variable "%s" is %s but should be %s' % 55 (varname, str(type(value)), str(vartype))) 56 if value < varmin or value > varmax: 57 raise ValueError('Variable "%s" value %s out of range %s..%s' % 58 (varname, repr(value), repr(varmin), repr(varmax))) 33 59 34 60 class Struct: … … 38 64 def pack(self): 39 65 args = [] 40 for variable in self._args_: 41 if (isinstance(self.__dict__[variable], list)): 42 for item in self.__dict__[variable]: 66 for variable, fmt, length in self._args_: 67 value = self.__dict__[variable] 68 if isinstance(value, list): 69 if length != None and length != len(value): 70 raise ValueError('Variable "%s" length %u does not match %u' % 71 (variable, len(value), length)) 72 for index, item in enumerate(value): 73 check_range(variable + '[' + repr(index) + ']', fmt, item) 43 74 args.append(item) 44 75 else: 45 args.append(self.__dict__[variable])46 76 check_range(variable, fmt, value) 77 args.append(value) 47 78 return struct.pack(self._format_, *args) 79 80 def unpack(self, data): 81 values = struct.unpack(self._format_, data) 82 i = 0 83 for variable, fmt, length in self._args_: 84 self.__dict__[variable] = values[i] 85 i += 1 48 86 49 87 def create(definition): … … 77 115 subtokens = token.split("[") 78 116 117 length = None 79 118 if (len(subtokens) > 1): 80 format += "%d" % int(subtokens[1].split("]")[0]) 119 length = int(subtokens[1].split("]")[0]) 120 format += "%d" % length 81 121 82 122 format += variable 83 123 84 124 inst.__dict__[subtokens[0]] = None 85 args.append( subtokens[0])125 args.append((subtokens[0], variable, length)) 86 126 87 127 variable = None
Note:
See TracChangeset
for help on using the changeset viewer.