9
9
10
10
from collections import OrderedDict
11
11
import struct
12
+ import logging
12
13
13
14
14
15
def parse_specification (db_specification ):
@@ -61,6 +62,8 @@ def set_int(_bytearray, byte_index, _int):
61
62
"""
62
63
Set value in bytearray to int
63
64
"""
65
+ # make sure were dealing with an int
66
+ _int = int (_int )
64
67
# int needs two be two bytes.
65
68
byte0 = _int >> 8
66
69
byte1 = _int - (byte0 << 8 )
@@ -109,6 +112,7 @@ def set_string(_bytearray, byte_index, value):
109
112
:params value: string data
110
113
:params size: total possible string size
111
114
"""
115
+ assert isinstance (value , (str , unicode ))
112
116
# set len count
113
117
_bytearray [byte_index + 1 ] = len (value )
114
118
i = 0
@@ -137,6 +141,7 @@ def get_dword(_bytearray, byte_index):
137
141
138
142
139
143
def set_dword (_bytearray , byte_index , dword ):
144
+ dword = int (dword )
140
145
_bytes = struct .unpack ('4B' , struct .pack ('I' , dword ))
141
146
for i , b in enumerate (_bytes ):
142
147
_bytearray [byte_index + i ] = b
@@ -153,11 +158,14 @@ class DB(object):
153
158
layout_offset = None # at which byte in row specification should
154
159
# we start reading the data
155
160
db_offset = None # at which byte in db should we start reading?
156
- # first fields could be used for something else
161
+ # first fields could be be status data.
162
+ # and only the last part could be control data
163
+ # now you can be sure you will never overwrite
164
+ # critical parts of db
157
165
158
166
def __init__ (self , db_number , _bytearray ,
159
167
specification , row_size , size , id_field = None ,
160
- db_offset = 0 , layout_offset = 0 ):
168
+ db_offset = 0 , layout_offset = 0 , row_offset = 0 ):
161
169
162
170
self .db_number = db_number
163
171
self .size = size
@@ -166,6 +174,8 @@ def __init__(self, db_number, _bytearray,
166
174
167
175
self .db_offset = db_offset
168
176
self .layout_offset = layout_offset
177
+ self .row_offset = row_offset
178
+
169
179
self ._bytearray = _bytearray
170
180
self .specification = specification
171
181
# loop over bytearray. make rowObjects
@@ -187,10 +197,15 @@ def make_rows(self):
187
197
specification ,
188
198
row_size = row_size ,
189
199
db_offset = db_offset ,
190
- layout_offset = layout_offset )
200
+ layout_offset = layout_offset ,
201
+ row_offset = self .row_offset )
191
202
192
203
# store row object
193
204
key = row [id_field ] if id_field else i
205
+ if key and key in self .index :
206
+ msg = '%s not unique!' % key
207
+ logging .error (msg )
208
+ print msg
194
209
self .index [key ] = row
195
210
196
211
def __getitem__ (self , key , default = None ):
@@ -216,11 +231,12 @@ class DB_Row(object):
216
231
_specification = None # row specification
217
232
218
233
def __init__ (self , _bytearray , _specification , row_size = 0 ,
219
- db_offset = 0 , layout_offset = 0 ):
234
+ db_offset = 0 , layout_offset = 0 , row_offset = 0 ):
220
235
221
236
self .db_offset = db_offset # start point of row data in db
222
237
self .layout_offset = layout_offset # start point of row data in layout
223
238
self .row_size = row_size
239
+ self .row_offset = row_offset # start of writable part of row
224
240
225
241
assert (isinstance (_bytearray , (bytearray , DB )))
226
242
self ._bytearray = _bytearray
@@ -335,8 +351,15 @@ def write(self, client):
335
351
336
352
db_nr = self ._bytearray .db_number
337
353
offset = self .db_offset
338
- data = self .get_bytearray ()[offset :offset + self .row_size ]
339
- client .db_write (db_nr , self .db_offset , data )
354
+ data = self .get_bytearray ()[offset :offset + self .row_size ]
355
+ db_offset = self .db_offset
356
+
357
+ # indicate start of write only area of row!
358
+ if self .row_offset :
359
+ data = data [self .row_offset :]
360
+ db_offset += self .row_offset
361
+
362
+ client .db_write (db_nr , db_offset , data )
340
363
341
364
def read (self , client ):
342
365
"""
0 commit comments