@@ -36,9 +36,9 @@ class Error(Exception):
3636[_DID_HEADER , _DID_DATA , _DID_RSRC ] = range (3 )
3737
3838# Various constants
39- REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
40- LINELEN = 64
41- RUNCHAR = chr (0x90 ) # run-length introducer
39+ REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
40+ LINELEN = 64
41+ RUNCHAR = chr (0x90 ) # run-length introducer
4242
4343#
4444# This code is no longer byte-order dependent
@@ -113,12 +113,12 @@ def __init__(self, ofp):
113113 self .ofp = ofp
114114 self .data = b''
115115 self .hqxdata = b''
116- self .linelen = LINELEN - 1
116+ self .linelen = LINELEN - 1
117117
118118 def write (self , data ):
119119 self .data = self .data + data
120120 datalen = len (self .data )
121- todo = (datalen // 3 ) * 3
121+ todo = (datalen // 3 ) * 3
122122 data = self .data [:todo ]
123123 self .data = self .data [todo :]
124124 if not data :
@@ -128,9 +128,9 @@ def write(self, data):
128128
129129 def _flush (self , force ):
130130 first = 0
131- while first <= len (self .hqxdata )- self .linelen :
131+ while first <= len (self .hqxdata ) - self .linelen :
132132 last = first + self .linelen
133- self .ofp .write (self .hqxdata [first :last ]+ b'\n ' )
133+ self .ofp .write (self .hqxdata [first :last ] + b'\n ' )
134134 self .linelen = LINELEN
135135 first = last
136136 self .hqxdata = self .hqxdata [first :]
@@ -139,8 +139,7 @@ def _flush(self, force):
139139
140140 def close (self ):
141141 if self .data :
142- self .hqxdata = \
143- self .hqxdata + binascii .b2a_hqx (self .data )
142+ self .hqxdata = self .hqxdata + binascii .b2a_hqx (self .data )
144143 self ._flush (1 )
145144 self .ofp .close ()
146145 del self .ofp
@@ -190,7 +189,7 @@ def __init__(self, name_finfo_dlen_rlen, ofp):
190189 def _writeinfo (self , name , finfo ):
191190 nl = len (name )
192191 if nl > 63 :
193- raise Error , 'Filename too long'
192+ raise Error ( 'Filename too long' )
194193 d = bytes ([nl ]) + name .encode ("latin-1" ) + b'\0 '
195194 tp , cr = finfo .Type , finfo .Creator
196195 if isinstance (tp , str ):
@@ -222,53 +221,52 @@ def _writecrc(self):
222221
223222 def write (self , data ):
224223 if self .state != _DID_HEADER :
225- raise Error , 'Writing data at the wrong time'
224+ raise Error ( 'Writing data at the wrong time' )
226225 self .dlen = self .dlen - len (data )
227226 self ._write (data )
228227
229228 def close_data (self ):
230229 if self .dlen != 0 :
231- raise Error , 'Incorrect data size, diff=%r' % (self .rlen ,)
230+ raise Error ( 'Incorrect data size, diff=%r' % (self .rlen ,) )
232231 self ._writecrc ()
233232 self .state = _DID_DATA
234233
235234 def write_rsrc (self , data ):
236235 if self .state < _DID_DATA :
237236 self .close_data ()
238237 if self .state != _DID_DATA :
239- raise Error , 'Writing resource data at the wrong time'
238+ raise Error ( 'Writing resource data at the wrong time' )
240239 self .rlen = self .rlen - len (data )
241240 self ._write (data )
242241
243242 def close (self ):
244243 if self .state < _DID_DATA :
245244 self .close_data ()
246245 if self .state != _DID_DATA :
247- raise Error , 'Close at the wrong time'
246+ raise Error ( 'Close at the wrong time' )
248247 if self .rlen != 0 :
249- raise Error , \
250- "Incorrect resource-datasize, diff=%r" % (self .rlen ,)
248+ raise Error ("Incorrect resource-datasize, diff=%r" % (self .rlen ,))
251249 self ._writecrc ()
252250 self .ofp .close ()
253251 self .state = None
254252 del self .ofp
255253
256254def binhex (inp , out ):
257- """(infilename, outfilename) - Create binhex-encoded copy of a file"""
255+ """binhex (infilename, outfilename): create binhex-encoded copy of a file"""
258256 finfo = getfileinfo (inp )
259257 ofp = BinHex (finfo , out )
260258
261259 ifp = io .open (inp , 'rb' )
262260 # XXXX Do textfile translation on non-mac systems
263- while 1 :
261+ while True :
264262 d = ifp .read (128000 )
265263 if not d : break
266264 ofp .write (d )
267265 ofp .close_data ()
268266 ifp .close ()
269267
270268 ifp = openrsrc (inp , 'rb' )
271- while 1 :
269+ while True :
272270 d = ifp .read (128000 )
273271 if not d : break
274272 ofp .write_rsrc (d )
@@ -291,29 +289,27 @@ def read(self, totalwtd):
291289 # much to decode: there may be newlines in the incoming data.
292290 while wtd > 0 :
293291 if self .eof : return decdata
294- wtd = ((wtd + 2 ) // 3 ) * 4
292+ wtd = ((wtd + 2 ) // 3 ) * 4
295293 data = self .ifp .read (wtd )
296294 #
297295 # Next problem: there may not be a complete number of
298296 # bytes in what we pass to a2b. Solve by yet another
299297 # loop.
300298 #
301- while 1 :
299+ while True :
302300 try :
303- decdatacur , self .eof = \
304- binascii .a2b_hqx (data )
301+ decdatacur , self .eof = binascii .a2b_hqx (data )
305302 break
306303 except binascii .Incomplete :
307304 pass
308305 newdata = self .ifp .read (1 )
309306 if not newdata :
310- raise Error , \
311- 'Premature EOF on binhex file'
307+ raise Error ('Premature EOF on binhex file' )
312308 data = data + newdata
313309 decdata = decdata + decdatacur
314310 wtd = totalwtd - len (decdata )
315311 if not decdata and not self .eof :
316- raise Error , 'Premature EOF on binhex file'
312+ raise Error ( 'Premature EOF on binhex file' )
317313 return decdata
318314
319315 def close (self ):
@@ -330,13 +326,13 @@ def __init__(self, ifp):
330326
331327 def read (self , wtd ):
332328 if wtd > len (self .post_buffer ):
333- self ._fill (wtd - len (self .post_buffer ))
329+ self ._fill (wtd - len (self .post_buffer ))
334330 rv = self .post_buffer [:wtd ]
335331 self .post_buffer = self .post_buffer [wtd :]
336332 return rv
337333
338334 def _fill (self , wtd ):
339- self .pre_buffer = self .pre_buffer + self .ifp .read (wtd + 4 )
335+ self .pre_buffer = self .pre_buffer + self .ifp .read (wtd + 4 )
340336 if self .ifp .eof :
341337 self .post_buffer = self .post_buffer + \
342338 binascii .rledecode_hqx (self .pre_buffer )
@@ -380,10 +376,10 @@ def __init__(self, ifp):
380376 #
381377 # Find initial colon.
382378 #
383- while 1 :
379+ while True :
384380 ch = ifp .read (1 )
385381 if not ch :
386- raise Error , "No binhex data found"
382+ raise Error ( "No binhex data found" )
387383 # Cater for \r\n terminated lines (which show up as \n\r, hence
388384 # all lines start with \r)
389385 if ch == b'\r ' :
@@ -407,14 +403,14 @@ def _checkcrc(self):
407403 # XXXX Is this needed??
408404 self .crc = self .crc & 0xffff
409405 if filecrc != self .crc :
410- raise Error , 'CRC error, computed %x, read %x' \
411- % (self .crc , filecrc )
406+ raise Error ( 'CRC error, computed %x, read %x'
407+ % (self .crc , filecrc ) )
412408 self .crc = 0
413409
414410 def _readheader (self ):
415411 len = self ._read (1 )
416412 fname = self ._read (ord (len ))
417- rest = self ._read (1 + 4 + 4 + 2 + 4 + 4 )
413+ rest = self ._read (1 + 4 + 4 + 2 + 4 + 4 )
418414 self ._checkcrc ()
419415
420416 type = rest [1 :5 ]
@@ -433,7 +429,7 @@ def _readheader(self):
433429
434430 def read (self , * n ):
435431 if self .state != _DID_HEADER :
436- raise Error , 'Read data at wrong time'
432+ raise Error ( 'Read data at wrong time' )
437433 if n :
438434 n = n [0 ]
439435 n = min (n , self .dlen )
@@ -447,7 +443,7 @@ def read(self, *n):
447443
448444 def close_data (self ):
449445 if self .state != _DID_HEADER :
450- raise Error , 'close_data at wrong time'
446+ raise Error ( 'close_data at wrong time' )
451447 if self .dlen :
452448 dummy = self ._read (self .dlen )
453449 self ._checkcrc ()
@@ -457,7 +453,7 @@ def read_rsrc(self, *n):
457453 if self .state == _DID_HEADER :
458454 self .close_data ()
459455 if self .state != _DID_DATA :
460- raise Error , 'Read resource data at wrong time'
456+ raise Error ( 'Read resource data at wrong time' )
461457 if n :
462458 n = n [0 ]
463459 n = min (n , self .rlen )
@@ -474,7 +470,7 @@ def close(self):
474470 self .ifp .close ()
475471
476472def hexbin (inp , out ):
477- """(infilename, outfilename) - Decode binhexed file"""
473+ """hexbin (infilename, outfilename) - Decode binhexed file"""
478474 ifp = HexBin (inp )
479475 finfo = ifp .FInfo
480476 if not out :
@@ -485,7 +481,7 @@ def hexbin(inp, out):
485481
486482 ofp = io .open (out , 'wb' )
487483 # XXXX Do translation on non-mac systems
488- while 1 :
484+ while True :
489485 d = ifp .read (128000 )
490486 if not d : break
491487 ofp .write (d )
@@ -496,7 +492,7 @@ def hexbin(inp, out):
496492 if d :
497493 ofp = openrsrc (out , 'wb' )
498494 ofp .write (d )
499- while 1 :
495+ while True :
500496 d = ifp .read_rsrc (128000 )
501497 if not d : break
502498 ofp .write (d )
@@ -510,13 +506,3 @@ def hexbin(inp, out):
510506 ofss .SetFInfo (nfinfo )
511507
512508 ifp .close ()
513-
514- def _test ():
515- fname = sys .argv [1 ]
516- binhex (fname , fname + '.hqx' )
517- hexbin (fname + '.hqx' , fname + '.viahqx' )
518- #hexbin(fname, fname+'.unpacked')
519- sys .exit (1 )
520-
521- if __name__ == '__main__' :
522- _test ()
0 commit comments