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_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')
 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
Exemple #4
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))
    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_01(self):
     """TestTifMarker: Initialise and write EOF."""
     myTh = TifMarker.TifMarkerWrite()
     self.assertEqual('TIF  True >:  0x       0  0x       0  0x       0',
                      str(myTh))