-
Notifications
You must be signed in to change notification settings - Fork 1
/
binparse.py
executable file
·66 lines (50 loc) · 1.64 KB
/
binparse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pprint
class Field(object):
fields = []
order = {}
names = {}
def __init__(self):
Field.order[self] = len(Field.order)
Field.fields.append(self)
def __repr__(self):
return '<{0} {1}>'.format(Field.names[self],
self.__class__.__name__)
class UInt32(Field):
pass
class UInt8(Field):
pass
class MetaStructure(type):
def __new__(metaclass, class_name, bases, attributes):
for k in attributes:
Field.names[attributes[k]] = k
print
print 'add %s:%s'%(k, attributes[k])
pprint.pprint(Field.names)
return type.__new__(metaclass, class_name, bases, attributes)
def __init__(self, *args, **kwargs):
print 'm.__init__', args, kwargs
class Structure(object):
__metaclass__ = MetaStructure
def __init__(self, *args, **kwargs):
print '__init__', args, kwargs
class Spam(Structure):
file_length = UInt32()
x = UInt8()
y = UInt8()
spam = Spam(3, spam='beans')
print
print 'Field.fields', Field.fields
print 'Field.order'
pprint.pprint(Field.order)
print 'Field.names'
pprint.pprint(Field.names)
# How to specify arrays?
# array_no1 = Array(UInt32, 4) # simple and explicit
# array_no2 = [UInt32() for _ in range(4)] # hard to make it do what we want
# array_no3 = Uint32[4]() # Tom's suggestion
# array_no4 = Uint32()*4 # compare with numpy
# array_no5 = Uint32(repeat=4)
# name = NullTerminatedString()
# str_len = SByte()
# manifest = CountedString(size=str_len)
# vim: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: