Show
Ignore:
Timestamp:
07/02/05 15:41:30 (19 years ago)
Author:
jerome
Message:

Big improvements on readability + maintainability

Files:
1 modified

Legend:

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

    r217 r220  
    2525from pdlanalyzer import version, pdlparser, postscript, pdf, pcl345, pclxl, escp2, dvi, tiff 
    2626 
    27 KILOBYTE = 1024     
    28 MEGABYTE = 1024 * KILOBYTE     
    29 LASTBLOCKSIZE = int(KILOBYTE / 4) 
    30  
    3127class PDLAnalyzer :     
    3228    """Class for PDL autodetection.""" 
     
    4036        self.debug = debug 
    4137        self.filename = filename 
    42         try : 
    43             import psyco  
    44         except ImportError :     
    45             sys.stderr.write("pkpgcounter : you should install psyco if possible, this would greatly speedup parsing.\n") 
    46             pass # Psyco is not installed 
    47         else :     
    48             # Psyco is installed, tell it to compile 
    49             # the CPU intensive methods : PCL and PCLXL 
    50             # parsing will greatly benefit from this,  
    51             # for PostScript and PDF the difference is 
    52             # barely noticeable since they are already 
    53             # almost optimal, and much more speedy anyway. 
    54             psyco.bind(postscript.PostScriptParser.getJobSize) 
    55             psyco.bind(pdf.PDFParser.getJobSize) 
    56             psyco.bind(escp2.ESCP2Parser.getJobSize) 
    57             psyco.bind(pcl345.PCL345Parser.getJobSize) 
    58             psyco.bind(pclxl.PCLXLParser.getJobSize) 
    59             psyco.bind(dvi.DVIParser.getJobSize) 
    60             psyco.bind(tiff.TIFFParser.getJobSize) 
    6138         
    6239    def getJobSize(self) :     
     
    6744        except pdlparser.PDLParserError, msg :     
    6845            self.closeFile() 
    69             raise pdlparser.PDLParserError, "ERROR : Unknown file format for %s (%s)" % (self.filename, msg) 
     46            raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg) 
    7047        else : 
    7148            try : 
    72                 size = pdlhandler(self.infile, self.debug).getJobSize() 
     49                size = pdlhandler.getJobSize() 
    7350            finally :     
    7451                self.closeFile() 
     
    9370        self.infile = tempfile.TemporaryFile(mode="w+b") 
    9471        while 1 : 
    95             data = infile.read(MEGABYTE)  
     72            data = infile.read(pdlparser.MEGABYTE)  
    9673            if not data : 
    9774                break 
     
    11491                pass    # probably stdin, which is not seekable 
    11592         
    116     def isPostScript(self, sdata, edata) :     
    117         """Returns 1 if data is PostScript, else 0.""" 
    118         if sdata.startswith("%!") or \ 
    119            sdata.startswith("\004%!") or \ 
    120            sdata.startswith("\033%-12345X%!PS") or \ 
    121            ((sdata[:128].find("\033%-12345X") != -1) and \ 
    122              ((sdata.find("LANGUAGE=POSTSCRIPT") != -1) or \ 
    123               (sdata.find("LANGUAGE = POSTSCRIPT") != -1) or \ 
    124               (sdata.find("LANGUAGE = Postscript") != -1))) or \ 
    125               (sdata.find("%!PS-Adobe") != -1) : 
    126             if self.debug :   
    127                 sys.stderr.write("%s is a PostScript file\n" % str(self.filename)) 
    128             return 1 
    129         else :     
    130             return 0 
    131          
    132     def isPDF(self, sdata, edata) :     
    133         """Returns 1 if data is PDF, else 0.""" 
    134         if sdata.startswith("%PDF-") or \ 
    135            sdata.startswith("\033%-12345X%PDF-") or \ 
    136            ((sdata[:128].find("\033%-12345X") != -1) and (sdata.upper().find("LANGUAGE=PDF") != -1)) or \ 
    137            (sdata.find("%PDF-") != -1) : 
    138             if self.debug :   
    139                 sys.stderr.write("%s is a PDF file\n" % str(self.filename)) 
    140             return 1 
    141         else :     
    142             return 0 
    143          
    144     def isPCL(self, sdata, edata) :     
    145         """Returns 1 if data is PCL, else 0.""" 
    146         if sdata.startswith("\033E\033") or \ 
    147            (sdata.startswith("\033*rbC") and (not edata[-3:] == "\f\033@")) or \ 
    148            sdata.startswith("\033%8\033") or \ 
    149            (sdata.find("\033%-12345X") != -1) : 
    150             if self.debug :   
    151                 sys.stderr.write("%s is a PCL3/4/5 file\n" % str(self.filename)) 
    152             return 1 
    153         else :     
    154             return 0 
    155          
    156     def isPCLXL(self, sdata, edata) :     
    157         """Returns 1 if data is PCLXL aka PCL6, else 0.""" 
    158         if ((sdata[:128].find("\033%-12345X") != -1) and \ 
    159              (sdata.find(" HP-PCL XL;") != -1) and \ 
    160              ((sdata.find("LANGUAGE=PCLXL") != -1) or \ 
    161               (sdata.find("LANGUAGE = PCLXL") != -1))) : 
    162             if self.debug :   
    163                 sys.stderr.write("%s is a PCLXL (aka PCL6) file\n" % str(self.filename)) 
    164             return 1 
    165         else :     
    166             return 0 
    167              
    168     def isESCP2(self, sdata, edata) :         
    169         """Returns 1 if data is ESC/P2, else 0.""" 
    170         if sdata.startswith("\033@") or \ 
    171            sdata.startswith("\033*") or \ 
    172            sdata.startswith("\n\033@") or \ 
    173            sdata.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 
    174             if self.debug :   
    175                 sys.stderr.write("%s is an ESC/P2 file\n" % str(self.filename)) 
    176             return 1 
    177         else :     
    178             return 0 
    179              
    180     def isDVI(self, sdata, edata) :         
    181         """Returns 1 if data is DVI, else 0.""" 
    182         if (ord(sdata[0]) == 0xf7) and (ord(edata[-1]) == 0xdf) : 
    183             if self.debug :   
    184                 sys.stderr.write("%s is a DVI file\n" % str(self.filename)) 
    185             return 1 
    186         else :     
    187             return 0 
    188              
    189     def isTIFF(self, sdata, edata) :         
    190         """Returns 1 if data is TIFF, else 0.""" 
    191         littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0) 
    192         bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a) 
    193         if sdata[:4] in (littleendian, bigendian) : 
    194             if self.debug :   
    195                 sys.stderr.write("%s is a TIFF file\n" % str(self.filename)) 
    196             return 1 
    197         else :     
    198             return 0 
    199      
    20093    def detectPDLHandler(self) :     
    20194        """Tries to autodetect the document format. 
     
    20396           Returns the correct PDL handler class or None if format is unknown 
    20497        """    
    205         # Try to detect file type by reading first block of datas     
     98        # Try to detect file type by reading first and last blocks of datas     
     99        # Each parser can read them automatically, but here we do this only once. 
    206100        self.infile.seek(0) 
    207         firstblock = self.infile.read(16 * KILOBYTE) 
     101        firstblock = self.infile.read(pdlparser.FIRSTBLOCKSIZE) 
    208102        try : 
    209             self.infile.seek(-LASTBLOCKSIZE, 2) 
    210             lastblock = self.infile.read(LASTBLOCKSIZE) 
     103            self.infile.seek(-pdlparser.LASTBLOCKSIZE, 2) 
     104            lastblock = self.infile.read(pdlparser.LASTBLOCKSIZE) 
    211105        except IOError :     
    212106            lastblock = "" 
    213107        self.infile.seek(0) 
    214108        if not firstblock : 
    215             sys.stderr.write("ERROR: input file %s is empty !\n" % str(self.filename)) 
     109            raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) 
    216110        else :     
    217             if self.isPostScript(firstblock, lastblock) : 
    218                 return postscript.PostScriptParser 
    219             elif self.isPCLXL(firstblock, lastblock) :     
    220                 return pclxl.PCLXLParser 
    221             elif self.isPDF(firstblock, lastblock) :     
    222                 return pdf.PDFParser 
    223             elif self.isPCL(firstblock, lastblock) :     
    224                 return pcl345.PCL345Parser 
    225             elif self.isESCP2(firstblock, lastblock) :     
    226                 return escp2.ESCP2Parser 
    227             elif self.isDVI(firstblock, lastblock) :     
    228                 return dvi.DVIParser 
    229             elif self.isTIFF(firstblock, lastblock) : 
    230                 return tiff.TIFFParser 
     111            for module in (postscript, \ 
     112                           pclxl, \ 
     113                           pdf, \ 
     114                           pcl345, \ 
     115                           escp2, \ 
     116                           dvi, \ 
     117                           tiff) : 
     118                try :                
     119                    return getattr(module, "Parser")(self.infile, self.debug, firstblock, lastblock) 
     120                except pdlparser.PDLParserError : 
     121                    pass # try next parser 
    231122        raise pdlparser.PDLParserError, "Analysis of first data block failed." 
    232123