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_03(self):
        """TestTifMarker: Initialise, write a Physical Record and write EOF."""
        myIo = io.BytesIO()
        myTh = TifMarker.TifMarkerWrite()
        myPr = b'\x40' * 19
        with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs:
            myTh.write(myRs, len(myPr))
            myRs.stream.write(myPr)
            myTh.close(myRs)
            myBytes = myIo.getvalue()
        self.assertEqual(len(myBytes), len(myPr) + 12 * 3)
        self.assertEqual('TIF  True >:  0x       1  0x      2b  0x      37',
                         str(myTh))
        #print
        #print myBytes.encode('unicode-escape')
        #print ''.join(['\\x%02x' % ord(c) for c in myBytes])
        self.assertEqual(
            myBytes,
            # Opening
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00'\
            # PR

            +b'\x40'*19\
            # EOF

            +b'\x01\x00\x00\x00\x00\x00\x00\x00\x2b\x00\x00\x00'\
            +b'\x01\x00\x00\x00\x1f\x00\x00\x00\x37\x00\x00\x00'
        )
 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_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_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 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
Beispiel #7
0
 def test_03_01(self):
     """TestTifMarker: Initialise, write two Physical Records and write EOF."""
     myIo = io.BytesIO()
     myTh = TifMarker.TifMarkerWrite()
     myPr = b'\xFF' * 12
     with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs:
         myTh.write(myRs, len(myPr))
         myRs.stream.write(myPr)
         myTh.write(myRs, len(myPr))
         myRs.stream.write(myPr)
         myTh.close(myRs)
         myBytes = myIo.getvalue()
     self.assertEqual(len(myBytes), len(myPr) * 2 + 12 * 4)
     self.assertEqual('TIF  True >:  0x       1  0x      3c  0x      48',
                      str(myTh))
     # print()
     # for i in range(0, len(myBytes), 12):
     #     print(myBytes[i:i+12])
     self.assertEqual(
         myBytes,
         # Opening
         b'\x00\x00\x00\x00\x00\x00\x00\x00\x18\x00\x00\x00'
         # PR 1
         + myPr
         # TIF for PR 2
         + b'\x00\x00\x00\x00\x00\x00\x00\x000\x00\x00\x00'
         # PR 2
         + myPr
         # EOF
         + b'\x01\x00\x00\x00\x18\x00\x00\x00<\x00\x00\x00' +
         b'\x01\x00\x00\x000\x00\x00\x00H\x00\x00\x00')
Beispiel #8
0
 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_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
Beispiel #11
0
 def __init__(
         self,
         theFile,
         theFileId=None,
         keepGoing=False,
         hasTif=False,
         thePrLen=PR_MAX_LENGTH,
         thePrt=PhysRecTail(),
 ):
     """Constructor with:
     theFile - A file like object or string, if the latter it assumed to be a path.
     theFileId - File identifier, this could be a path for example. If
         None the RawStream will try and cope with it.
     keepGoing - If True we do our best to keep going.
     hasTif - Insert TIF markers or not.
     thePrLen - Max Physical Record length, defaults to the maximum possible length.
     thePrt - Physical Records Trailer settings (defaults to PhysRec.PhysRecTail())."""
     super().__init__(theFileId, keepGoing)
     #print('thePrt', thePrt)
     try:
         self.stream = RawStream.RawStream(theFile,
                                           mode='wb',
                                           fileId=self.fileId)
     except IOError:
         raise ExceptionPhysRec(
             'PhysRecWrite: Can not open LIS file "%s" for read' %
             self.fileId)
     self.tif = None
     if hasTif:
         self.tif = TifMarker.TifMarkerWrite()
     self._prLen = thePrLen
     self._prt = thePrt
     # Calculate the maximum payload length
     self._maxPayloadLen = self._prLen - PR_PRH_LENGTH - self._prt.prtLen
     if self._prLen > PR_MAX_LENGTH:
         raise ExceptionPhysRecWrite(
             'PhysRecWrite PR length {:d} greater than allowed: {:d}'.
             format(self._prLen, PR_MAX_LENGTH))
     if self._maxPayloadLen < 1:
         raise ExceptionPhysRecWrite(
             'PhysRecWrite no space for payload in {:d} in PR length {:d}'.
             format(self._maxPayloadLen, self._prLen))
Beispiel #12
0
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_02(self):
        """TestTifMarker: Initialise and write EOF."""
        myIo = io.BytesIO()
        myTh = TifMarker.TifMarkerWrite()
        with RawStream.RawStream(myIo, mode='wb', fileId='MyFile') as myRs:
            myTh.write(myRs, 0)
            myTh.close(myRs)
            myBytes = myIo.getvalue()
        self.assertEqual(len(myBytes), 12 * 3)
        self.assertEqual('TIF  True >:  0x       1  0x      18  0x      24',
                         str(myTh))
        #print
        #print myBytes.encode('unicode-escape')
        #print ''.join(['\\x%02x' % ord(c) for c in myBytes])
        self.assertEqual(
            myBytes,
            # Opening
            b'\x00\x00\x00\x00'+b'\x00\x00\x00\x00'+b'\x0c\x00\x00\x00'\
            # EOF

            +b'\x01\x00\x00\x00'+b'\x00\x00\x00\x00'+b'\x18\x00\x00\x00'\
            +b'\x01\x00\x00\x00'+b'\x0c\x00\x00\x00'+b'\x24\x00\x00\x00'
            )
 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
 def test_01(self):
     """TestTifMarker: Initialise and write EOF."""
     myTh = TifMarker.TifMarkerWrite()
     self.assertEqual('TIF  True >:  0x       0  0x       0  0x       0',
                      str(myTh))