Show
Ignore:
Timestamp:
11/20/07 00:04:09 (16 years ago)
Author:
jerome
Message:

Major code cleaning. Now clearer, although probably a bit slower since
a file can be opened several times.
Now universal line opening mode is only used when needed (PS, PDF and plain
text), and binary opening mode is used for the other formats.
This mean we will be able to remove mmap calls wherever possible, finally.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/zjstream.py

    r463 r491  
    2323"""This modules implements a page counter for ZjStream documents.""" 
    2424 
    25 import sys 
    26 import os 
    27 import mmap 
    28 from struct import unpack 
     25import struct 
    2926 
    3027import pdlparser 
     
    3633        if self.firstblock[:4] == "ZJZJ" : 
    3734            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") 
    38             self.littleEndian() 
    39             return True 
     35            return self.littleEndian() 
    4036        elif self.firstblock[:4] == "JZJZ" :     
    4137            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") 
    42             self.bigEndian() 
    43             return True 
     38            return self.bigEndian() 
    4439        else :     
    4540            return False 
     
    4742    def littleEndian(self) : 
    4843        """Toggles to little endianness.""" 
    49         self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" } 
    50         self.unpackShort = self.unpackType[2] 
    51         self.unpackLong = self.unpackType[4] 
    52         return 0 
     44        self.unpackHeader = "<IIIHH" 
     45        return True 
    5346         
    5447    def bigEndian(self) : 
    5548        """Toggles to big endianness.""" 
    56         self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" } 
    57         self.unpackShort = self.unpackType[2] 
    58         self.unpackLong = self.unpackType[4] 
    59         return 0 
     49        self.unpackHeader = ">IIIHH" 
     50        return True 
    6051         
    6152    def getJobSize(self) : 
    6253        """Computes the number of pages in a ZjStream document.""" 
    63         infileno = self.infile.fileno() 
    64         minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) 
    65         pos = 4 
     54        unpack = struct.unpack 
     55        self.infile.seek(4, 0) # Skip ZJZJ/JZJZ header 
    6656        startpagecount = endpagecount = 0 
    67         try : 
    68             try : 
    69                 while 1 : 
    70                     header = minfile[pos:pos+16] 
    71                     if len(header) != 16 : 
    72                         break 
    73                     totalChunkSize = unpack(self.unpackLong, header[:4])[0] 
    74                     chunkType = unpack(self.unpackLong, header[4:8])[0] 
    75                     numberOfItems = unpack(self.unpackLong, header[8:12])[0] 
    76                     reserved = unpack(self.unpackShort, header[12:14])[0] 
    77                     signature = unpack(self.unpackShort, header[14:])[0] 
    78                     pos += totalChunkSize 
    79                     if chunkType == 0 : 
    80                         self.logdebug("startDoc") 
    81                     elif chunkType == 1 :     
    82                         self.logdebug("endDoc") 
    83                     elif chunkType == 2 :     
    84                         self.logdebug("startPage") 
    85                         startpagecount += 1 
    86                     elif chunkType == 3 : 
    87                         self.logdebug("endPage") 
    88                         endpagecount += 1 
    89                          
    90                     #self.logdebug("Chunk size : %s" % totalChunkSize) 
    91                     #self.logdebug("Chunk type : 0x%08x" % chunkType) 
    92                     #self.logdebug("# items : %s" % numberOfItems) 
    93                     #self.logdebug("reserved : 0x%04x" % reserved) 
    94                     #self.logdebug("signature : 0x%04x" % signature) 
    95                     #self.logdebug("\n") 
    96             except IndexError : # EOF ? 
    97                 pass  
    98         finally :         
    99             minfile.close() 
     57        while True : 
     58            header = self.infile.read(16) 
     59            if not header : 
     60                break 
     61            try :     
     62                (totalChunkSize, 
     63                 chunkType, 
     64                 numberOfItems, 
     65                 reserved, 
     66                 signature) = unpack(self.unpackHeader, header) 
     67            except struct.error : 
     68                raise pdlparser.PDLParserError, "This file doesn't seem to be valid ZjStream datas." 
     69            self.infile.seek(totalChunkSize - len(header), 1) 
     70            if chunkType == 2 :     
     71                #self.logdebug("startPage") 
     72                startpagecount += 1 
     73            elif chunkType == 3 : 
     74                #self.logdebug("endPage") 
     75                endpagecount += 1 
     76            #elif chunkType == 0 : 
     77            #    self.logdebug("startDoc") 
     78            #elif chunkType == 1 :     
     79            #    self.logdebug("endDoc") 
     80                 
     81            #self.logdebug("Chunk size : %s" % totalChunkSize) 
     82            #self.logdebug("Chunk type : 0x%08x" % chunkType) 
     83            #self.logdebug("# items : %s" % numberOfItems) 
     84            #self.logdebug("reserved : 0x%04x" % reserved) 
     85            #self.logdebug("signature : 0x%04x" % signature) 
     86            #self.logdebug("\n") 
    10087             
    101         if startpagecount != endpagecount :     
    102             sys.stderr.write("ERROR: Incorrect ZjStream datas.\n") 
     88        # Number of endpage commands should be sufficient, 
     89        # but we never know : someone could try to cheat the printer 
     90        # by starting a page but not ending it, and ejecting it manually 
     91        # later on. Not sure if the printers would support this, but 
     92        # taking the max value works around the problem in any case. 
     93        self.logdebug("StartPage : %i    EndPage : %i" % (startpagecount, endpagecount)) 
    10394        return max(startpagecount, endpagecount) 
    104          
    105 if __name__ == "__main__" :     
    106     pdlparser.test(Parser)