def test_09(self): """TestTifMarker: Initialise and read two Physical records with bad previous markers.""" myBytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00'\ +b'\x40'*19\ +b'\x00\x00\x00\x00\x01\x00\x00\x00\x34\x00\x00\x00'\ +b'\x20'*9\ +b'\x01\x00\x00\x00\x1e\x00\x00\x00\x40\x00\x00\x00'\ +b'\x01\x00\x00\x00\x34\x00\x00\x00\x4c\x00\x00\x00' myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) # Now read myTmr.read(myRs) #print myTmr self.assertEqual(myTmr.markers(), (0, 0, 31)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) # Should raise as \x01 rather than correct value \x00 try: myTmr.read(myRs) self.fail('TifMarker.ExceptionTifMarker not raised') except TifMarker.ExceptionTifMarker: pass
def test_13(self): """TestTifMarker: Two Physical records with padding of 6 bytes succeeds when allowPrPadding=True.""" myBytes = b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x1f\x00\x00\x00' \ + b'\x40'*19 \ + b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x34\x00\x00\x00' \ + b'\x20'*9 \ + b'\x01\x00\x00\x00' + b'\x1f\x00\x00\x00' + b'\x40\x00\x00\x00' \ + b'\x01\x00\x00\x00' + b'\x34\x00\x00\x00' + b'\x4c\x00\x00\x00' myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs, allowPrPadding=True) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) # Now read myTmr.read(myRs) #print myTmr self.assertEqual(myTmr.markers(), (0, 0, 0x1F)) # Simulate reading just 13 bytes of PR when 19 exist. self.assertEqual(b'\x40' * 13, myRs.stream.read(13)) myTmr.read(myRs) #print myTmr self.assertEqual(myTmr.markers(), (0, 0, 0x34)) # Simulate reading just 3 bytes of PR when 9 exist. self.assertEqual(b'\x20' * 3, myRs.stream.read(3)) # EOF myTmr.read(myRs) self.assertEqual(myTmr.markers(), (1, 0x34, 0x4c)) # Read after EOF try: myTmr.read(myRs) self.fail('TifMarker.ExceptionTifMarker not raised') except RawStream.ExceptionRawStream: pass self.assertTrue(myTmr.eof)
def test_07(self): """TestTifMarker: Initialise and read with bad next marker.""" myBytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x1e\x00\x00\x00'\ +b'\x40'*19\ +b'\x01\x00\x00\x00\x00\x00\x00\x00\x2b\x00\x00\x00'\ +b'\x01\x00\x00\x00\x1e\x00\x00\x00\x37\x00\x00\x00' myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) # Now read myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 30)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) #print 'tell()', myRs.tell() try: myTmr.read(myRs) self.fail( 'Tif read failed to raise TifMarker.ExceptionTifMarker') except TifMarker.ExceptionTifMarker: pass self.assertEqual(myTmr.markers(), (0, 0, 30)) try: myTmr.read(myRs) self.fail('TifMarker.ExceptionTifMarker not raised') except TifMarker.ExceptionTifMarker: pass
def test_08(self): """TestTifMarker: Initialise and read two Physical records with good previous markers.""" myBytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00'\ +b'\x40'*19\ +b'\x00\x00\x00\x00\x00\x00\x00\x00\x34\x00\x00\x00'\ +b'\x20'*9\ +b'\x01\x00\x00\x00\x1f\x00\x00\x00\x40\x00\x00\x00'\ +b'\x01\x00\x00\x00\x34\x00\x00\x00\x4c\x00\x00\x00' myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) # Now read myTmr.read(myRs) #print myTmr self.assertEqual(myTmr.markers(), (0, 0, 31)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) myTmr.read(myRs) #print myTmr self.assertEqual(myTmr.markers(), (0, 0, 52)) self.assertEqual(b'\x20' * 9, myRs.stream.read(9)) #print 'tell()', myRs.tell() # EOF myTmr.read(myRs) self.assertEqual(myTmr.markers(), (1, 52, 76)) # Read after EOF try: myTmr.read(myRs) self.fail('TifMarker.ExceptionTifMarker not raised') except RawStream.ExceptionRawStream: pass self.assertTrue(myTmr.eof)
def __init__(self, theFile, theFileId=None, keepGoing=False): """Constructor with a file path or file-like object. TODO: checksum. """ super(PhysRecRead, self).__init__(theFileId, keepGoing) try: self.stream = RawStream.RawStream(theFile, mode='rb', fileId=self.fileId) except IOError: raise ExceptionPhysRec( 'PhysRecRead: Can not open LIS file "%s" for read' % self.fileId) # Rewind to start of file self.stream.seek(0) # Flag for EOF self.isEOF = False # How far we are into the logical data for this PR self._ldIndex = 0 # Bytes of logical data read or skipped in this Logical Record self._ldTell = 0 # Flag set if this PR is the first of a LR # This does not rely on predecessor bits self._isLrStart = True # Flag to say if we must read a PRH before any read/skip operation self._mustReadHead = True self.tif = TifMarker.TifMarkerRead(self.stream, allowPrPadding=keepGoing) # Reset the stream self.tif may initialise TIF and read stream self.stream.seek(0) self.startOfLr = 0
def test_06(self): """TestTifMarker: Initialise and read, no TIF markers.""" myBytes = b'\x40' * 19 # Now read it back myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertFalse(myTmr.hasTif) self.assertEqual(myTmr.markers(), (0, 0, 0)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0))
def test_04(self): """TestTifMarker: Initialise, write a Physical Record and EOF and read it back.""" myIo = io.BytesIO() myTmw = TifMarker.TifMarkerWrite() myPrS = (b'\x40' * 19, b'\x20' * 9) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmw.write(myRs, len(myPrS[0])) myRs.stream.write(myPrS[0]) myTmw.write(myRs, len(myPrS[1])) myRs.stream.write(myPrS[1]) myTmw.close(myRs) myBytes = myIo.getvalue() self.assertEqual(len(myBytes), len(myPrS[0]) + len(myPrS[1]) + 3 * 4 * 4) self.assertEqual('TIF True >: 0x 1 0x 40 0x 4c', str(myTmw)) #print #print myBytes.encode('unicode-escape') #print ''.join(['\\x%02x' % ord(c) for c in myBytes]) self.assertEqual( myBytes, b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x1f\x00\x00\x00' \ + b'\x40'*19 \ + b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x34\x00\x00\x00' \ + b'\x20'*9 \ + b'\x01\x00\x00\x00' + b'\x1f\x00\x00\x00' + b'\x40\x00\x00\x00' \ + b'\x01\x00\x00\x00' + b'\x34\x00\x00\x00' + b'\x4c\x00\x00\x00' ) # Now read it back myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0x1f)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) #print 'tell()', myRs.tell() myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0x34)) self.assertEqual(b'\x20' * 9, myRs.stream.read(9)) # This reads both type 1 markers myTmr.read(myRs) self.assertEqual(myTmr.markers(), (1, 0x34, 0x4c)) try: myTmr.read(myRs) self.fail('RawStream.ExceptionRawStream not raised') except RawStream.ExceptionRawStream: pass
def test_10(self): """TestTifMarker: Initialise and read two Physical records and random access + reset().""" myBytes = b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x1f\x00\x00\x00' \ + b'\x40'*19 \ + b'\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x34\x00\x00\x00' \ + b'\x20'*9 \ + b'\x01\x00\x00\x00' + b'\x1f\x00\x00\x00' + b'\x40\x00\x00\x00' \ + b'\x01\x00\x00\x00' + b'\x34\x00\x00\x00' + b'\x4c\x00\x00\x00' myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0x1f)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0x34)) self.assertEqual(b'\x20' * 9, myRs.stream.read(9)) # This reads both type 1 markers myTmr.read(myRs) self.assertEqual(myTmr.markers(), (1, 0x34, 0x4c)) # # Now seek and reset # myRs.seek(0) myTmr.reset() self.assertEqual(myTmr.markers(), (0, 0, 0)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0x1f)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 0x34)) self.assertEqual(b'\x20' * 9, myRs.stream.read(9)) # This reads both type 1 markers myTmr.read(myRs) self.assertEqual(myTmr.markers(), (1, 0x34, 0x4c)) try: myTmr.read(myRs) self.fail('RawStream.ExceptionRawStream not raised') except RawStream.ExceptionRawStream: pass
def _stripTIF(sIn, sOut): myTm = TifMarker.TifMarkerRead(sIn, allowPrPadding=False) if myTm.hasTif: while myTm.tifType == 0: bLen = myTm.tifNext - sIn.tell() msg = 'stripTif(): Tell: 0x{:08x} Len: 0x{:08x} TIF: {:s}'.format( sIn.tell(), bLen, str(myTm)) logging.debug(msg) if sOut is None: print(msg) sIn.seek(myTm.tifNext) else: sOut.write(sIn.read(bLen)) try: myTm.read(sIn) except RawStream.ExceptionRawStreamEOF as err: logging.error( 'Premature EOF of input so output terminated: {:s}'.format( str(err))) return
def test_12(self): """TestTifMarker: Two Physical records with padding of 6 bytes when allowPrPadding=False.""" myBytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x25\x00\x00\x00'\ +b'\x40'*(19+6)\ +b'\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00'\ +b'\x20'*(9+6)\ +b'\x01\x00\x00\x00\x25\x00\x00\x00\x4c\x00\x00\x00'\ +b'\x01\x00\x00\x00\x40\x00\x00\x00\x58\x00\x00\x00' myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs, allowPrPadding=False) self.assertTrue(myTmr.hasTif) self.assertFalse(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) # Now read myTmr.read(myRs) #print myTmr self.assertEqual(myTmr.markers(), (0, 0, 0x25)) # Simulate reading just 19 bytes of PR when 19+6 exist. self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) self.assertRaises(TifMarker.ExceptionTifMarker, myTmr.read, myRs)
def test_05(self): """TestTifMarker: Initialise and read reversed markers.""" myBytes = b'\x00\x00\x00\x00'+b'\x00\x00\x00\x00'+b'\x00\x00\x00\x1f'\ +b'\x40'*19\ +b'\x00\x00\x00\x01'+b'\x00\x00\x00\x00'+b'\x00\x00\x00\x2b'\ +b'\x00\x00\x00\x01'+b'\x00\x00\x00\x1f'+b'\x00\x00\x00\x37' # Now read it back myIo = io.BytesIO(myBytes) with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs: myTmr = TifMarker.TifMarkerRead(myRs) self.assertTrue(myTmr.hasTif) self.assertTrue(myTmr.isReversed) self.assertEqual(myTmr.markers(), (0, 0, 0)) myTmr.read(myRs) self.assertEqual(myTmr.markers(), (0, 0, 31)) self.assertEqual(b'\x40' * 19, myRs.stream.read(19)) #print 'tell()', myRs.tell() myTmr.read(myRs) self.assertEqual(myTmr.markers(), (1, 31, 55)) try: myTmr.read(myRs) self.fail('RawStream.ExceptionRawStream not raised') except RawStream.ExceptionRawStream: pass