4848)
4949
5050
51- def readCString (f , nStrings = 1 , bufsize = 1000 , startPos = None , strip = True ,
52- rewind = False ):
51+ def read_c_string (f , n_strings = 1 , bufsize = 1000 , start_pos = None , strip = True ,
52+ rewind = False ):
5353 """Read a zero-terminated string from a file object.
5454
5555 Read and return a zero-terminated string from a file object.
5656
5757 Parameters
5858 ----------
5959 f : fileobj
60- File object to use
61- nStrings : int, optional
60+ File object to use. Object should implement tell, seek, and read.
61+ n_strings : int, optional
6262 Number of strings to search (and return). Default is 1.
6363 bufsize: int, optional
6464 Define the buffer size that should be searched for the string.
6565 Default is 1000 bytes.
66- startPos : int, optional
66+ start_pos : int, optional
6767 Define the start file position from which to search. If None then start
6868 where the file object currently points to. Default is None.
6969 strip : bool, optional
@@ -77,24 +77,19 @@ def readCString(f, nStrings=1, bufsize=1000, startPos=None, strip=True,
7777 -------
7878 str_list : generator of string(s)
7979 """
80- currentPos = f .tell ()
81- if strip :
82- suffix = b''
83- else :
84- suffix = b'\x00 '
85- if startPos is not None :
86- f .seek (startPos )
80+ current_pos = f .tell ()
81+ suffix = b'' if strip else b'\x00 '
82+ if start_pos is not None :
83+ f .seek (start_pos )
8784 data = f .read (bufsize )
8885 lines = data .split (b'\x00 ' )
8986 str_list = []
9087 if rewind :
91- f .seek (currentPos )
88+ f .seek (current_pos )
9289 else :
93- offset = 0
94- for s in range (nStrings ):
95- offset += len (lines [s ]) + 1
96- f .seek (currentPos + offset )
97- for s in range (nStrings ):
90+ offsets = [len (lines [s ]) + 1 for s in range (n_strings )]
91+ f .seek (current_pos + sum (offsets ))
92+ for s in range (n_strings ):
9893 str_list .append (lines [s ] + suffix )
9994 return str_list
10095
@@ -111,8 +106,10 @@ def parse_BV_header(hdr_dict_proto, fileobj, parent_hdr_dict=None):
111106 tuple of format described in Notes below.
112107 fileobj : fileobj
113108 File object to use. Make sure that the current position is at the
114- beginning of the header (e.g. at 0).
115- parent_hdr_dict: OrderedDict
109+ beginning of the header (e.g. at 0). Object should implement tell,
110+ seek, and read.
111+ parent_hdr_dict: None or OrderedDict, optional
112+ Default is None. None results in empty `OrderedDict`.
116113 When parse_BV_header() is called recursively the already filled
117114 (parent) hdr_dict is passed to give access to n_fields_name fields
118115 outside the current scope (see below).
@@ -158,30 +155,30 @@ def parse_BV_header(hdr_dict_proto, fileobj, parent_hdr_dict=None):
158155 'NrOfLags' in the VMP file header).
159156 """
160157 hdr_dict = OrderedDict ()
161- for name , format , def_or_name in hdr_dict_proto :
158+ for name , pack_format , def_or_name in hdr_dict_proto :
162159 # handle zero-terminated strings
163- if format == 'z' :
164- value = readCString (fileobj )[0 ]
160+ if pack_format == 'z' :
161+ value = read_c_string (fileobj )[0 ]
165162 # handle array fields
166- elif isinstance (format , tuple ):
163+ elif isinstance (pack_format , tuple ):
167164 value = []
168165 # check the length of the array to expect
169166 if def_or_name in hdr_dict :
170167 n_values = hdr_dict [def_or_name ]
171168 else :
172169 n_values = parent_hdr_dict [def_or_name ]
173170 for i in range (n_values ):
174- value .append (parse_BV_header (format , fileobj , hdr_dict ))
171+ value .append (parse_BV_header (pack_format , fileobj , hdr_dict ))
175172 # handle conditional fields
176173 elif isinstance (def_or_name , tuple ):
177174 if hdr_dict [def_or_name [1 ]] == def_or_name [2 ]:
178- bytes = fileobj .read (calcsize (format ))
179- value = unpack ('<' + format , bytes )[0 ]
175+ raw_bytes = fileobj .read (calcsize (pack_format ))
176+ value = unpack ('<' + pack_format , raw_bytes )[0 ]
180177 else : # assign the default value
181178 value = def_or_name [0 ]
182- else : # unpack bytes of type format
183- bytes = fileobj .read (calcsize (format ))
184- value = unpack ('<' + format , bytes )[0 ]
179+ else : # unpack raw_bytes of type pack_format
180+ raw_bytes = fileobj .read (calcsize (pack_format ))
181+ value = unpack ('<' + pack_format , raw_bytes )[0 ]
185182 hdr_dict [name ] = value
186183 return hdr_dict
187184
@@ -197,43 +194,45 @@ def pack_BV_header(hdr_dict_proto, hdr_dict, parent_hdr_dict=None):
197194 hdr_dict_proto: tuple
198195 tuple of format described in Notes of :func:`parse_BV_header`
199196 hdr_dict: OrderedDict
200- hdr_dict that contains the fields and values to for the respective
201- BV file format.
202- parent_hdr_dict: OrderedDict
203- When parse_BV_header() is called recursively the already filled
204- (parent) hdr_dict is passed to give access to n_fields_name fields
205- outside the current scope (see below).
197+ hdr_dict that contains the fields and values to for the respective
198+ BV file format.
199+ parent_hdr_dict: None or OrderedDict, optional
200+ Default is None. None results in empty `OrderedDict`.
201+ When parse_BV_header() is called recursively the already filled
202+ (parent) hdr_dict is passed to give access to n_fields_name fields
203+ outside the current scope (see below).
206204
207205 Returns
208206 -------
209207 binaryblock : bytes
210208 Binary representation of header ready for writing to file.
211209 """
212210 binary_parts = []
213- for name , format , def_or_name in hdr_dict_proto :
211+ for name , pack_format , def_or_name in hdr_dict_proto :
214212 value = hdr_dict [name ]
215213 # handle zero-terminated strings
216- if format == 'z' :
214+ if pack_format == 'z' :
217215 part = value + b'\x00 '
218216 # handle array fields
219- elif isinstance (format , tuple ):
217+ elif isinstance (pack_format , tuple ):
220218 # check the length of the array to expect
221219 if def_or_name in hdr_dict :
222220 n_values = hdr_dict [def_or_name ]
223221 else :
224222 n_values = parent_hdr_dict [def_or_name ]
225223 sub_parts = []
226224 for i in range (n_values ):
227- sub_parts .append (pack_BV_header (format , value [i ], hdr_dict ))
225+ sub_parts .append (pack_BV_header (pack_format , value [i ],
226+ hdr_dict ))
228227 part = b'' .join (sub_parts )
229228 # handle conditional fields
230229 elif isinstance (def_or_name , tuple ):
231230 if hdr_dict [def_or_name [1 ]] == def_or_name [2 ]:
232- part = pack ('<' + format , value )
231+ part = pack ('<' + pack_format , value )
233232 else :
234233 continue
235234 else :
236- part = pack ('<' + format , value )
235+ part = pack ('<' + pack_format , value )
237236 binary_parts .append (part )
238237 return b'' .join (binary_parts )
239238
@@ -249,26 +248,27 @@ def calc_BV_header_size(hdr_dict_proto, hdr_dict, parent_hdr_dict=None):
249248 hdr_dict_proto: tuple
250249 tuple of format described in Notes of :func:`parse_BV_header`
251250 hdr_dict: OrderedDict
252- hdr_dict that contains the fields and values to for the respective
253- BV file format.
254- parent_hdr_dict: OrderedDict
255- When parse_BV_header() is called recursively the already filled
256- (parent) hdr_dict is passed to give access to n_fields_name fields
257- outside the current scope (see below).
251+ hdr_dict that contains the fields and values to for the respective
252+ BV file format.
253+ parent_hdr_dict: None or OrderedDict, optional
254+ Default is None. None results in empty `OrderedDict`.
255+ When parse_BV_header() is called recursively the already filled
256+ (parent) hdr_dict is passed to give access to n_fields_name fields
257+ outside the current scope (see below).
258258
259259 Returns
260260 -------
261261 hdr_size : int
262262 Size of header when packed into bytes ready for writing to file.
263263 """
264264 hdr_size = 0
265- for name , format , def_or_name in hdr_dict_proto :
265+ for name , pack_format , def_or_name in hdr_dict_proto :
266266 value = hdr_dict [name ]
267267 # handle zero-terminated strings
268- if format == 'z' :
268+ if pack_format == 'z' :
269269 hdr_size += len (value ) + 1
270270 # handle array fields
271- elif isinstance (format , tuple ):
271+ elif isinstance (pack_format , tuple ):
272272 # check the length of the array to expect
273273 if def_or_name in hdr_dict :
274274 n_values = hdr_dict [def_or_name ]
@@ -277,15 +277,16 @@ def calc_BV_header_size(hdr_dict_proto, hdr_dict, parent_hdr_dict=None):
277277 for i in range (n_values ):
278278 # recursively iterate through the fields of all items
279279 # in the array
280- hdr_size += calc_BV_header_size (format , value [i ], hdr_dict )
280+ hdr_size += calc_BV_header_size (pack_format , value [i ],
281+ hdr_dict )
281282 # handle conditional fields
282283 elif isinstance (def_or_name , tuple ):
283284 if hdr_dict [def_or_name [1 ]] == def_or_name [2 ]:
284- hdr_size += calcsize (format )
285+ hdr_size += calcsize (pack_format )
285286 else :
286287 continue
287288 else :
288- hdr_size += calcsize (format )
289+ hdr_size += calcsize (pack_format )
289290 return hdr_size
290291
291292
@@ -319,9 +320,9 @@ def update_BV_header(hdr_dict_proto, hdr_dict_old, hdr_dict_new,
319320 An updated version hdr_dict correcting effects of changed nested and
320321 conditional fields.
321322 """
322- for name , format , def_or_name in hdr_dict_proto :
323+ for name , pack_format , def_or_name in hdr_dict_proto :
323324 # handle nested loop fields
324- if isinstance (format , tuple ):
325+ if isinstance (pack_format , tuple ):
325326 # calculate the change of array length and the new array length
326327 if def_or_name in hdr_dict_old :
327328 delta_values = hdr_dict_new [def_or_name ] - \
@@ -333,13 +334,14 @@ def update_BV_header(hdr_dict_proto, hdr_dict_old, hdr_dict_new,
333334 n_values = parent_new [def_or_name ]
334335 if delta_values > 0 : # add nested loops
335336 for i in range (delta_values ):
336- hdr_dict_new [name ].append (_proto2default (format , hdr_dict_new ))
337+ hdr_dict_new [name ].append (_proto2default (pack_format ,
338+ hdr_dict_new ))
337339 elif delta_values < 0 : # remove nested loops
338340 for i in range (abs (delta_values )):
339341 hdr_dict_new [name ].pop ()
340342 # loop over nested fields
341343 for i in range (n_values ):
342- update_BV_header (format , hdr_dict_old [name ][i ],
344+ update_BV_header (pack_format , hdr_dict_old [name ][i ],
343345 hdr_dict_new [name ][i ], hdr_dict_old ,
344346 hdr_dict_new )
345347 return hdr_dict_new
@@ -354,16 +356,16 @@ def _proto2default(proto, parent_default_hdr=None):
354356 See :func:`parse_BV_header` for description of `proto` format.
355357 """
356358 default_hdr = OrderedDict ()
357- for name , format , def_or_name in proto :
358- if isinstance (format , tuple ):
359+ for name , pack_format , def_or_name in proto :
360+ if isinstance (pack_format , tuple ):
359361 value = []
360362 # check the length of the array to expect
361363 if def_or_name in default_hdr :
362364 n_values = default_hdr [def_or_name ]
363365 else :
364366 n_values = parent_default_hdr [def_or_name ]
365367 for i in range (n_values ):
366- value .append (_proto2default (format , default_hdr ))
368+ value .append (_proto2default (pack_format , default_hdr ))
367369 default_hdr [name ] = value
368370 # handle conditional fields
369371 elif isinstance (def_or_name , tuple ):
0 commit comments