Changeset 520 for pkpgcounter

Show
Ignore:
Timestamp:
11/28/07 00:52:52 (17 years ago)
Author:
jerome
Message:

Code cleaning.

Location:
pkpgcounter/trunk/pkpgpdls
Files:
20 modified

Legend:

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

    r519 r520  
    5656        self.workfile = None  
    5757        self.mustclose = None 
     58        self.firstblock = None 
     59        self.lastblock = None 
    5860         
    5961    def getJobSize(self) :     
     
    126128            self.workfile.close()     
    127129         
     130    def readFirstAndLastBlocks(self) :     
     131        """Reads the first and last blocks of data.""" 
     132        # Now read first and last block of the input file 
     133        # to be able to detect the real file format and the parser to use. 
     134        self.firstblock = self.workfile.read(pdlparser.FIRSTBLOCKSIZE) 
     135        try : 
     136            self.workfile.seek(-pdlparser.LASTBLOCKSIZE, 2) 
     137            self.lastblock = self.workfile.read(pdlparser.LASTBLOCKSIZE) 
     138        except IOError :     
     139            self.lastblock = "" 
     140             
    128141    def detectPDLHandler(self) :     
    129142        """Tries to autodetect the document format. 
     
    133146        if not os.stat(self.filename).st_size : 
    134147            raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) 
    135          
    136         # Now read first and last block of the input file 
    137         # to be able to detect the real file format and the parser to use. 
    138         firstblock = self.workfile.read(pdlparser.FIRSTBLOCKSIZE) 
    139         try : 
    140             self.workfile.seek(-pdlparser.LASTBLOCKSIZE, 2) 
    141             lastblock = self.workfile.read(pdlparser.LASTBLOCKSIZE) 
    142         except IOError :     
    143             lastblock = "" 
     148        self.readFirstAndLastBlocks() 
    144149             
    145150        # IMPORTANT : the order is important below. FIXME. 
     
    162167                       plain) :     # IMPORTANT : don't move this one up ! 
    163168            try :                
    164                 return module.Parser(self.filename, firstblock, 
    165                                                     lastblock, 
    166                                                     self.options.debug) 
     169                return module.Parser(self) 
    167170            except pdlparser.PDLParserError : 
    168171                pass # try next parser 
  • pkpgcounter/trunk/pkpgpdls/dvi.py

    r492 r520  
    3636        """Returns True if data is DVI, else False.""" 
    3737        try : 
    38             if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) : 
     38            if (ord(self.parent.firstblock[0]) == 0xf7) \ 
     39                and (ord(self.parent.lastblock[-1]) == 0xdf) : 
    3940                self.logdebug("DEBUG: Input file is in the DVI format.") 
    4041                return True 
  • pkpgcounter/trunk/pkpgpdls/escp2.py

    r491 r520  
    3131    def isValid(self) :         
    3232        """Returns True if data is ESC/P2, else False.""" 
    33         if self.firstblock.startswith("\033@") or \ 
    34            self.firstblock.startswith("\033*") or \ 
    35            self.firstblock.startswith("\n\033@") or \ 
    36            self.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 
     33        if self.parent.firstblock.startswith("\033@") or \ 
     34           self.parent.firstblock.startswith("\033*") or \ 
     35           self.parent.firstblock.startswith("\n\033@") or \ 
     36           self.parent.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 
    3737            self.logdebug("DEBUG: Input file is in the ESC/P2 format.") 
    3838            return True 
  • pkpgcounter/trunk/pkpgpdls/escpages03.py

    r491 r520  
    3535    def isValid(self) :         
    3636        """Returns True if data is TIFF, else False.""" 
    37         if self.firstblock.startswith("\033\1@EJL") and \ 
    38             (self.firstblock.find("=ESC/PAGES03\n") != -1) : 
     37        if self.parent.firstblock.startswith("\033\1@EJL") and \ 
     38            (self.parent.firstblock.find("=ESC/PAGES03\n") != -1) : 
    3939            self.logdebug("DEBUG: Input file is in the ESC/PageS03 format.") 
    4040            return True 
  • pkpgcounter/trunk/pkpgpdls/hbp.py

    r491 r520  
    3434    def isValid(self) :         
    3535        """Returns True if data is HBP, else False.""" 
    36         if self.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 : 
     36        if self.parent.firstblock.find("@PJL ENTER LANGUAGE = HBP\n") != -1 : 
    3737            self.logdebug("DEBUG: Input file is in the HBP format.") 
    3838            return True 
  • pkpgcounter/trunk/pkpgpdls/inkcoverage.py

    r492 r520  
    112112    try : 
    113113        image = Image.open(fname) 
    114     except IOError, msg :    
     114    except (IOError, OverflowError), msg :    
    115115        raise pdlparser.PDLParserError, "%s (%s)" % (msg, fname) 
    116116    else :     
  • pkpgcounter/trunk/pkpgpdls/lidil.py

    r491 r520  
    6262        # for simplicity's sake. 
    6363        EOFMarker = "$\x00\x10\x00\x08\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff$$\x00\x10\x00\x06\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff$"  
    64         if self.firstblock.startswith(BOFMarker) \ 
    65            and self.lastblock.endswith(EOFMarker) : 
     64        if self.parent.firstblock.startswith(BOFMarker) \ 
     65           and self.parent.lastblock.endswith(EOFMarker) : 
    6666            self.logdebug("DEBUG: Input file is in the Hewlett-Packard LIDIL format.") 
    6767            return True 
  • pkpgcounter/trunk/pkpgpdls/mstrash.py

    r519 r520  
    3636         
    3737           Identifying datas taken from the file command's magic database. 
    38            IMPORTANT : some magic values are not reused here because they 
    39            IMPORTANT : seem to be specific to some particular i18n release. 
     38           IMPORTANT : some magic values are not reused here because they 
     39           IMPORTANT : seem to be specific to some particular i18n release. 
    4040        """    
    41         if self.firstblock.startswith("PO^Q`") \ 
    42            or self.firstblock.startswith("\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1") \ 
    43            or self.firstblock.startswith("\xfe7\x00#") \ 
    44            or self.firstblock.startswith("\xdb\xa5-\x00\x00\x00") \ 
    45            or self.firstblock.startswith("\x31\xbe\x00\x00") \ 
    46            or self.firstblock[2112:].startswith("MSWordDoc") : 
     41        if self.parent.firstblock.startswith("PO^Q`") \ 
     42           or self.parent.firstblock.startswith("\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1") \ 
     43           or self.parent.firstblock.startswith("\xfe7\x00#") \ 
     44           or self.parent.firstblock.startswith("\xdb\xa5-\x00\x00\x00") \ 
     45           or self.parent.firstblock.startswith("\x31\xbe\x00\x00") \ 
     46           or self.parent.firstblock[2112:].startswith("MSWordDoc") : 
    4747            self.logdebug("DEBUG: Input file seems to be in a Microsoft shitty file format.") 
    4848            return True 
  • pkpgcounter/trunk/pkpgpdls/ooo.py

    r491 r520  
    3232    def isValid(self) :         
    3333        """Returns True if data is OpenDocument, else False.""" 
    34         if self.firstblock[:2] == "PK" : 
     34        if self.parent.firstblock[:2] == "PK" : 
    3535            try : 
    3636                self.archive = zipfile.ZipFile(self.infile) 
  • pkpgcounter/trunk/pkpgpdls/pcl345.py

    r508 r520  
    9292    def isValid(self) :     
    9393        """Returns True if data is PCL3/4/5, else False.""" 
    94         if self.firstblock.startswith("\033E\033") or \ 
    95            (self.firstblock.startswith("\033*rbC") and (not self.lastblock[-3:] == "\f\033@")) or \ 
    96            self.firstblock.startswith("\033*rB\033") or \ 
    97            self.firstblock.startswith("\033%8\033") or \ 
    98            (self.firstblock.find("\033%-12345X") != -1) or \ 
    99            (self.firstblock.find("@PJL ENTER LANGUAGE=PCL\012\015\033") != -1) or \ 
    100            (self.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.firstblock.find("\033E\033") != -1)) : 
     94        if self.parent.firstblock.startswith("\033E\033") or \ 
     95           (self.parent.firstblock.startswith("\033*rbC") and (not self.parent.lastblock[-3:] == "\f\033@")) or \ 
     96           self.parent.firstblock.startswith("\033*rB\033") or \ 
     97           self.parent.firstblock.startswith("\033%8\033") or \ 
     98           (self.parent.firstblock.find("\033%-12345X") != -1) or \ 
     99           (self.parent.firstblock.find("@PJL ENTER LANGUAGE=PCL\012\015\033") != -1) or \ 
     100           (self.parent.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.parent.firstblock.find("\033E\033") != -1)) : 
    101101            self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.") 
    102102            return True 
  • pkpgcounter/trunk/pkpgpdls/pclxl.py

    r492 r520  
    8383    def isValid(self) :     
    8484        """Returns True if data is PCLXL aka PCL6, else False.""" 
    85         if (((self.firstblock[:128].find("\033%-12345X") != -1) and \ 
    86              (self.firstblock.find(" HP-PCL XL;") != -1) and \ 
    87              ((self.firstblock.find("LANGUAGE=PCLXL") != -1) or \ 
    88               (self.firstblock.find("LANGUAGE = PCLXL") != -1)))) \ 
    89              or ((self.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.firstblock.find(" HP-PCL XL;") != -1))) : 
     85        if (((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 
     86             (self.parent.firstblock.find(" HP-PCL XL;") != -1) and \ 
     87             ((self.parent.firstblock.find("LANGUAGE=PCLXL") != -1) or \ 
     88              (self.parent.firstblock.find("LANGUAGE = PCLXL") != -1)))) \ 
     89             or ((self.parent.firstblock.startswith(chr(0xcd)+chr(0xca)) and (self.parent.firstblock.find(" HP-PCL XL;") != -1))) : 
    9090            self.logdebug("DEBUG: Input file is in the PCLXL (aka PCL6) format.") 
    9191            return True 
  • pkpgcounter/trunk/pkpgpdls/pdf.py

    r492 r520  
    4747    def isValid(self) :     
    4848        """Returns True if data is PDF, else False.""" 
    49         if self.firstblock.startswith("%PDF-") or \ 
    50            self.firstblock.startswith("\033%-12345X%PDF-") or \ 
    51            ((self.firstblock[:128].find("\033%-12345X") != -1) and (self.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ 
    52            (self.firstblock.find("%PDF-") != -1) : 
     49        if self.parent.firstblock.startswith("%PDF-") or \ 
     50           self.parent.firstblock.startswith("\033%-12345X%PDF-") or \ 
     51           ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and (self.parent.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ 
     52           (self.parent.firstblock.find("%PDF-") != -1) : 
    5353            self.logdebug("DEBUG: Input file is in the PDF format.") 
    5454            return True 
  • pkpgcounter/trunk/pkpgpdls/pdlparser.py

    r493 r520  
    4343    totiffcommands = None        # Default command to convert to TIFF 
    4444    openmode = "rb"              # Default file opening mode 
    45     def __init__(self, filename, firstblock, lastblock, debug=0) : 
     45    def __init__(self, parent) : 
    4646        """Initialize the generic parser.""" 
    47         self.filename = filename 
    48         self.debug = debug 
     47        self.parent = parent 
    4948        self.infile = None 
    50         (self.firstblock, self.lastblock) = (firstblock, lastblock) 
    5149        if not self.isValid() : 
    5250            raise PDLParserError, "Invalid file format !" 
     
    6058            # parsing will greatly benefit from this. 
    6159            psyco.bind(self.getJobSize) 
    62         self.infile = open(self.filename, self.openmode) 
    63         # self.logdebug("Opened %s in '%s' mode." % (self.filename, self.openmode)) 
     60        self.infile = open(self.parent.filename, self.openmode) 
     61        # self.logdebug("Opened %s in '%s' mode." % (self.parent.filename, self.openmode)) 
    6462             
    6563    def __del__(self) : 
     
    7068    def logdebug(self, message) :        
    7169        """Logs a debug message if needed.""" 
    72         if self.debug : 
     70        if self.parent.options.debug : 
    7371            sys.stderr.write("%s\n" % message) 
    7472             
     
    8684        """    
    8785        if self.totiffcommands : 
    88             infname = self.filename 
     86            infname = self.parent.filename 
    8987            for totiffcommand in self.totiffcommands : 
    9088                error = False 
  • pkpgcounter/trunk/pkpgpdls/pil.py

    r518 r520  
    3535        """Returns True if data is an image format supported by PIL, else False."""    
    3636        try : 
    37             image = Image.open(self.filename) 
     37            image = Image.open(self.parent.filename) 
    3838        except (IOError, OverflowError) :     
    3939            return False 
     
    4545        """Counts pages in an image file.""" 
    4646        index = 0 
    47         image = Image.open(self.filename) 
     47        image = Image.open(self.parent.filename) 
    4848        try : 
    4949            while True : 
  • pkpgcounter/trunk/pkpgpdls/plain.py

    r495 r520  
    3939           If it's impossible to find one we consider it's not plain text. 
    4040        """    
    41         lines = self.firstblock.split("\r\n") 
     41        lines = self.parent.firstblock.split("\r\n") 
    4242        if len(lines) == 1 : 
    4343            lines = lines[0].split("\r") 
  • pkpgcounter/trunk/pkpgpdls/postscript.py

    r492 r520  
    3737    def isValid(self) :     
    3838        """Returns True if data is PostScript, else False.""" 
    39         if self.firstblock.startswith("%!") or \ 
    40            self.firstblock.startswith("\004%!") or \ 
    41            self.firstblock.startswith("\033%-12345X%!PS") or \ 
    42            ((self.firstblock[:128].find("\033%-12345X") != -1) and \ 
    43              ((self.firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \ 
    44               (self.firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \ 
    45               (self.firstblock.find("LANGUAGE = Postscript") != -1))) or \ 
    46               (self.firstblock.find("%!PS-Adobe") != -1) : 
     39        if self.parent.firstblock.startswith("%!") or \ 
     40           self.parent.firstblock.startswith("\004%!") or \ 
     41           self.parent.firstblock.startswith("\033%-12345X%!PS") or \ 
     42           ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 
     43             ((self.parent.firstblock.find("LANGUAGE=POSTSCRIPT") != -1) or \ 
     44              (self.parent.firstblock.find("LANGUAGE = POSTSCRIPT") != -1) or \ 
     45              (self.parent.firstblock.find("LANGUAGE = Postscript") != -1))) or \ 
     46              (self.parent.firstblock.find("%!PS-Adobe") != -1) : 
    4747            self.logdebug("DEBUG: Input file is in the PostScript format.") 
    4848            return True 
     
    5757        # we need to reopen the input file in binary mode again, just in case 
    5858        # otherwise we might break the original file's contents. 
    59         infile = open(self.filename, "rb") 
     59        infile = open(self.parent.filename, "rb") 
    6060        try : 
    6161            child = popen2.Popen4(command) 
  • pkpgcounter/trunk/pkpgpdls/qpdl.py

    r491 r520  
    7373    def isValid(self) :     
    7474        """Returns True if data is QPDL aka SPL2, else False.""" 
    75         if ((self.firstblock[:128].find("\033%-12345X") != -1) and \ 
    76              ((self.firstblock.find("LANGUAGE=QPDL") != -1) or \ 
    77               (self.firstblock.find("LANGUAGE = QPDL") != -1))) : 
     75        if ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 
     76             ((self.parent.firstblock.find("LANGUAGE=QPDL") != -1) or \ 
     77              (self.parent.firstblock.find("LANGUAGE = QPDL") != -1))) : 
    7878            self.logdebug("DEBUG: Input file is in the QPDL (aka SPL2) format.") 
    7979            return True 
  • pkpgcounter/trunk/pkpgpdls/spl1.py

    r493 r520  
    3737    def isValid(self) :     
    3838        """Returns True if data is SPL1, else False.""" 
    39         if ((self.firstblock[:128].find("\033%-12345X") != -1) and \ 
    40             (self.firstblock.find("$PJL ") != -1) and \ 
    41              ((self.firstblock.find("LANGUAGE=SMART") != -1) or \ 
    42               (self.firstblock.find("LANGUAGE = SMART") != -1))) : 
     39        if ((self.parent.firstblock[:128].find("\033%-12345X") != -1) and \ 
     40            (self.parent.firstblock.find("$PJL ") != -1) and \ 
     41             ((self.parent.firstblock.find("LANGUAGE=SMART") != -1) or \ 
     42              (self.parent.firstblock.find("LANGUAGE = SMART") != -1))) : 
    4343            self.logdebug("DEBUG: Input file is in the SPL1 format.") 
    4444            return True 
  • pkpgcounter/trunk/pkpgpdls/tiff.py

    r492 r520  
    3737        littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0) 
    3838        bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a) 
    39         if self.firstblock[:4] in (littleendian, bigendian) : 
     39        if self.parent.firstblock[:4] in (littleendian, bigendian) : 
    4040            self.logdebug("DEBUG: Input file is in the TIFF format.") 
    4141            return True 
  • pkpgcounter/trunk/pkpgpdls/zjstream.py

    r491 r520  
    3131    def isValid(self) :     
    3232        """Returns True if data is ZjStream, else False.""" 
    33         if self.firstblock[:4] == "ZJZJ" : 
     33        if self.parent.firstblock[:4] == "ZJZJ" : 
    3434            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") 
    3535            return self.littleEndian() 
    36         elif self.firstblock[:4] == "JZJZ" :     
     36        elif self.parent.firstblock[:4] == "JZJZ" :     
    3737            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") 
    3838            return self.bigEndian()