Exemple #1
0
 def __init__(
         self,
         theFile,
         theFileId=None,
         keepGoing=False,
         hasTif=False,
         thePrLen=PhysRec.PR_MAX_LENGTH,
         thePrt=PhysRec.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__(theFile, theFileId, 'w', keepGoing)
     try:
         self._prh = PhysRec.PhysRecWrite(
             self.file,
             self.fileId,
             self.keepGoing,
             hasTif,
             thePrLen,
             thePrt,
         )
     except PhysRec.ExceptionPhysRec as e:
         raise ExceptionFileWrite('FileWrite.__init__(): error "%s"' %
                                  str(e))
Exemple #2
0
 def setup(self, arg):
     pr_len, lr_len, lr_count = tuple(int(s) for s in arg.split('-'))
     self.io_file = io.BytesIO()
     pr_writer = PhysRec.PhysRecWrite(
         self.io_file,
         theFileId='WriteFile',
         keepGoing=False,
         hasTif=True,
         thePrLen=pr_len,
         thePrt=PhysRec.PhysRecTail(hasRecNum=True, fileNum=42, hasCheckSum=True),
     )
     for i in range(lr_count):
         pr_writer.writeLr(b'\xff' * lr_len)
Exemple #3
0
 def setup(self, arg):
     pr_len, lr_len, lr_count = tuple(int(s) for s in arg.split('-'))
     io_file = io.BytesIO()
     self.pr_writer = PhysRec.PhysRecWrite(
         io_file,
         theFileId='WriteFile',
         keepGoing=False,
         hasTif=False,
         thePrLen=pr_len,
         thePrt=PhysRec.PhysRecTail(
             hasRecNum=True,
             fileNum=42,
             hasCheckSum=False
         ),
     )
     self.logical_data = b'\xff' * lr_len
Exemple #4
0
 def _retStdFile(self, f, prLen=PhysRec.PR_MAX_LENGTH):
     return File.FileWrite(
         theFile=self._retFilePath(f),
         theFileId=self._retFilePath(f),
         keepGoing=False,
         hasTif=True,
         thePrLen=prLen,
         thePrt=PhysRec.PhysRecTail(hasRecNum=True, fileNum=255, hasCheckSum=True),
     )
Exemple #5
0
 def time_pr_read(self, arg):
     _pr_len, lr_len, lr_count = tuple(int(s) for s in arg.split('-'))
     pr_read = PhysRec.PhysRecRead(theFile=self.io_file, theFileId='MyFile', keepGoing=False)
     while 1:
         logical_data = pr_read.readLrBytes()
         if logical_data is None:
             break
         assert len(logical_data) == lr_len
         lr_count -= 1
     assert lr_count == 0
Exemple #6
0
 def __init__(self, theFile, theFileId=None, keepGoing=False):
     """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.
     """
     super(FileRead, self).__init__(theFile, theFileId, 'r', keepGoing)
     try:
         self._prh = PhysRec.PhysRecRead(self.file, self.fileId,
                                         self.keepGoing)
     except PhysRec.ExceptionPhysRec as e:
         raise ExceptionFileRead('FileRead.__init__(): error "%s"' % str(e))
def scanFile(fp, isVerbose, keepGoing, dumpTellS, theS=sys.stdout):
    #    print(dumpTellS)
    try:
        myPrh = PhysRec.PhysRecRead(fp, fp, keepGoing)
    except PhysRec.ExceptionPhysRec as err:
        print('Can not open file, error: %s' % str(err))
        return
    # Now other stuff generated by this loop
    theS.write('Offset        Length  Type  Logical Data\n')
    myLdSigma = bytes()
    myOffs = myPrh.tellLr()
    # Histogram of lengths and types
    myHistLen = Histogram.Histogram()
    myHistTyp = Histogram.Histogram()
    for myLd, isLdStart in myPrh.genLd():
        if isLdStart:
            if len(myLdSigma) == 0:
                # First time through the loop then don't write anything
                pass
            else:
                # This is not the first time through the loop
                # so write out the trailing LogicalData length
                lrType = -1
                if len(myLdSigma) > 0:
                    lrType = myLdSigma[0]
                    myHistLen.add(len(myLdSigma))
                    myHistTyp.add(lrType)
                theS.write('0x{:08X}  {:8d}  {:4d}'.format(
                    myOffs, len(myLdSigma), lrType))
                if myOffs not in dumpTellS \
                and not isVerbose and len(myLdSigma) > LEN_TRUNCATE:
                    theS.write('  {!r:s}...\n'.format(
                        myLdSigma[0:LEN_TRUNCATE]))
                else:
                    theS.write('  {!r:s}\n'.format(myLdSigma))
                myLdSigma = bytes()
                myOffs = myPrh.tellLr()
        myLdSigma += myLd
    if len(myLdSigma) > 0:
        theS.write('0x{:08X}  {:8d}  {:4d}'.format(myOffs, len(myLdSigma),
                                                   lrType))
        if myOffs not in dumpTellS \
        and not isVerbose and len(myLdSigma) > LEN_TRUNCATE:
            theS.write('  {!r:s}...\n'.format(myLdSigma[0:LEN_TRUNCATE]))
        else:
            theS.write('  {:s}\n'.format(myLdSigma))
    theS.write('Histogram of Logical Data lengths:\n')
    theS.write(myHistLen.strRep(100, valTitle='Bytes', inclCount=True))
    theS.write('\n')
    theS.write('Histogram of Logical Record types:\n')
    theS.write(myHistTyp.strRep(100, inclCount=True))
    theS.write('\n')
Exemple #8
0
def write_logical_data_to_physical_records(
    logical_data_records: typing.List[bytes],
    has_tif: bool = False,
    pr_len: int = PhysRec.PR_MAX_LENGTH,
    pr_tail: PhysRec.PhysRecTail = PhysRec.PhysRecTail()
) -> io.BytesIO:
    """
    Takes logical data and writes it into a contiguous set of physical records returning a binary file.
    This is quite useful for testing.

    pr_tail has the following arguments: hasRecNum=False, fileNum=None, hasCheckSum=False
    """
    file_obj = io.BytesIO()
    prh = PhysRec.PhysRecWrite(file_obj,
                               theFileId=None,
                               keepGoing=False,
                               hasTif=has_tif,
                               thePrLen=pr_len,
                               thePrt=pr_tail)
    for ld in logical_data_records:
        prh.writeLr(ld)
    return file_obj
Exemple #9
0
def scanFile(fp, keepGoing, theS=sys.stdout):
    try:
        myPrh = PhysRec.PhysRecRead(fp, fp, keepGoing)
    except PhysRec.ExceptionPhysRec as err:
        print('Can not open file, error: %s' % str(err))
        return
    # Now other stuff generated by this loop
    myHeader = myPrh.strHeader() + '   LR Attr [Total LD]'
    theS.write(myHeader)
    theS.write('\n')
    theS.write(' start '.center(len(myHeader), '-'))
    myLdSum = -1
    numPR = 0
    # Histogram of PR lengths
    myLenHist = Histogram.Histogram()
    for myLd, isLdStart in myPrh.genLd():
        myLenHist.add(myPrh.prLen)
        if isLdStart:
            if myLdSum == -1:
                # First time through the loop then don't write anything
                pass
            else:
                # This is not the first time through the loop
                # so write out the trailing LogicalData length
                theS.write(' [%8d]' % myLdSum)
            myLdSum = 0
        myLdSum += len(myLd)
        theS.write('\n')
        theS.write(str(myPrh))
        if isLdStart:
            #theS.write(' >')
            #theS.write(' 0x{0:08X}'.format(myPrh.tellLr()))
            if len(myLd) >= 2:
                #print(myLd)
                h, a = LD_STRUCT_HEAD.unpack(myLd[:LD_STRUCT_HEAD.size])
                theS.write(' 0x{0:02X} 0x{1:02x}'.format(h, a))
            else:
                theS.write(' 0x??')
        else:
            theS.write(' + --   --')
        #theS.write(' %6d' % len(myLd))
        numPR += 1
    theS.write(' [%8d]' % myLdSum)
    theS.write('\n')
    theS.write('%s\n' % str(myPrh))
    theS.write(' EOF '.center(len(myHeader), '-'))
    theS.write('\n')
    theS.write('PR Count: %d\n' % numPR)
    theS.write('Histogram of Physical Record lengths:\n')
    theS.write(myLenHist.strRep(100, valTitle='Bytes', inclCount=True))
    theS.write('\n')