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/pdlparser.py

    r211 r220  
    2121 
    2222import sys 
     23import psyco 
     24 
     25KILOBYTE = 1024     
     26MEGABYTE = 1024 * KILOBYTE     
     27FIRSTBLOCKSIZE = 16 * KILOBYTE 
     28LASTBLOCKSIZE = int(KILOBYTE / 4) 
    2329 
    2430class PDLParserError(Exception): 
     
    3339class PDLParser : 
    3440    """Generic PDL parser.""" 
    35     def __init__(self, infile, debug=0) : 
     41    def __init__(self, infile, debug=0, firstblock=None, lastblock=None) : 
    3642        """Initialize the generic parser.""" 
     43        self.infile = infile 
    3744        self.debug = debug 
    38         self.infile = infile 
    39                  
    40     def getJobSize(self) :             
    41         """Counts pages in the document.""" 
     45        if firstblock is None : 
     46            self.infile.seek(0) 
     47            firstblock = self.infile.read(FIRSTBLOCKSIZE) 
     48            try : 
     49                self.infile.seek(-LASTBLOCKSIZE, 2) 
     50                lastblock = self.infile.read(LASTBLOCKSIZE) 
     51            except IOError :     
     52                lastblock = "" 
     53            self.infile.seek(0) 
     54        self.firstblock = firstblock 
     55        self.lastblock = lastblock 
     56        if not self.isValid() : 
     57            raise PDLParserError, "Invalid file format !" 
     58        try : 
     59            import psyco  
     60        except ImportError :     
     61            sys.stderr.write("WARN: you should install psyco if possible, this would greatly speedup parsing.\n") 
     62            pass # Psyco is not installed 
     63        else :     
     64            # Psyco is installed, tell it to compile 
     65            # the CPU intensive methods : PCL and PCLXL 
     66            # parsing will greatly benefit from this,  
     67            # for PostScript and PDF the difference is 
     68            # barely noticeable since they are already 
     69            # almost optimal, and much more speedy anyway. 
     70            psyco.bind(self.getJobSize) 
     71             
     72    def isValid(self) :     
     73        """Returns 1 if data is in the expected format, else 0.""" 
    4274        raise RuntimeError, "Not implemented !" 
     75         
     76    def getJobSize(self) :     
     77        """Counts pages in a document.""" 
     78        raise RuntimeError, "Not implemented !"