Show
Ignore:
Timestamp:
10/06/08 00:22:07 (16 years ago)
Author:
jerome
Message:

Removed spaces at EOL.

Files:
1 modified

Legend:

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

    r3410 r3436  
    88# the Free Software Foundation, either version 3 of the License, or 
    99# (at your option) any later version. 
    10 #  
     10# 
    1111# This program is distributed in the hope that it will be useful, 
    1212# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414# GNU General Public License for more details. 
    15 #  
     15# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    4343        self.colorspace = colorspace 
    4444        self.resolution = resolution 
    45      
    46      
    47 class PDLAnalyzer :     
     45 
     46 
     47class PDLAnalyzer : 
    4848    """Class for PDL autodetection.""" 
    4949    def __init__(self, filename, options=AnalyzerOptions()) : 
    5050        """Initializes the PDL analyzer. 
    51          
     51 
    5252           filename is the name of the file or '-' for stdin. 
    53            filename can also be a file-like object which  
     53           filename can also be a file-like object which 
    5454           supports read() and seek(). 
    5555        """ 
    5656        self.options = options 
    5757        self.filename = filename 
    58         self.workfile = None  
     58        self.workfile = None 
    5959        self.mustclose = None 
    60          
    61     def getJobSize(self) :     
     60 
     61    def getJobSize(self) : 
    6262        """Returns the job's size.""" 
    6363        size = 0 
     
    6767                pdlhandler = self.detectPDLHandler() 
    6868                size = pdlhandler.getJobSize() 
    69             except pdlparser.PDLParserError, msg :     
     69            except pdlparser.PDLParserError, msg : 
    7070                raise pdlparser.PDLParserError, "Unsupported file format for %s (%s)" % (self.filename, msg) 
    71         finally :     
     71        finally : 
    7272            self.closeFile() 
    7373        return size 
    74              
     74 
    7575    def getInkCoverage(self, colorspace=None, resolution=None) : 
    7676        """Extracts the percents of ink coverage from the input file.""" 
     
    8989                    pdlhandler.convertToTiffMultiPage24NC(filename, self.options.resolution) 
    9090                    result = inkcoverage.getInkCoverage(filename, cspace) 
    91                 finally :     
     91                finally : 
    9292                    dummyfile.close() 
    93             except pdlparser.PDLParserError, msg :     
     93            except pdlparser.PDLParserError, msg : 
    9494                raise pdlparser.PDLParserError, "Unsupported file format for %s (%s)" % (self.filename, msg) 
    9595        finally : 
    9696            self.closeFile() 
    9797        return result 
    98          
    99     def openFile(self) :     
     98 
     99    def openFile(self) : 
    100100        """Opens the job's data stream for reading.""" 
    101101        self.mustclose = False  # by default we don't want to close the file when finished 
    102102        if hasattr(self.filename, "read") and hasattr(self.filename, "seek") : 
    103             # filename is in fact a file-like object  
     103            # filename is in fact a file-like object 
    104104            infile = self.filename 
    105105        elif self.filename == "-" : 
    106106            # we must read from stdin 
    107107            infile = sys.stdin 
    108         else :     
     108        else : 
    109109            # normal file 
    110110            self.workfile = open(self.filename, "rb") 
    111111            self.mustclose = True 
    112112            return 
    113              
     113 
    114114        # Use a temporary file, always seekable contrary to standard input. 
    115115        self.workfile = tempfile.NamedTemporaryFile(mode="w+b") 
    116116        self.filename = self.workfile.name 
    117117        while True : 
    118             data = infile.read(pdlparser.MEGABYTE)  
     118            data = infile.read(pdlparser.MEGABYTE) 
    119119            if not data : 
    120120                break 
    121121            self.workfile.write(data) 
    122         self.workfile.flush()     
     122        self.workfile.flush() 
    123123        self.workfile.seek(0) 
    124              
    125     def closeFile(self) :         
     124 
     125    def closeFile(self) : 
    126126        """Closes the job's data stream if we have to.""" 
    127127        if self.mustclose : 
    128             self.workfile.close()     
    129          
     128            self.workfile.close() 
     129 
    130130    def readFirstAndLastBlocks(self, inputfile) : 
    131131        """Reads the first and last blocks of data.""" 
     
    136136            inputfile.seek(-pdlparser.LASTBLOCKSIZE, 2) 
    137137            lastblock = inputfile.read(pdlparser.LASTBLOCKSIZE) 
    138         except IOError :     
     138        except IOError : 
    139139            lastblock = "" 
    140         return (firstblock, lastblock)      
    141              
    142     def detectPDLHandler(self) :     
     140        return (firstblock, lastblock) 
     141 
     142    def detectPDLHandler(self) : 
    143143        """Tries to autodetect the document format. 
    144          
     144 
    145145           Returns the correct PDL handler class or None if format is unknown 
    146         """    
     146        """ 
    147147        if not os.stat(self.filename).st_size : 
    148148            raise pdlparser.PDLParserError, "input file %s is empty !" % str(self.filename) 
    149149        (firstblock, lastblock) = self.readFirstAndLastBlocks(self.workfile) 
    150              
     150 
    151151        # IMPORTANT : the order is important below. FIXME. 
    152152        for module in (postscript, \ 
     
    170170                       mscrap, \ 
    171171                       plain) :     # IMPORTANT : don't move this one up ! 
    172             try :                
    173                 return module.Parser(self, self.filename,  
     172            try : 
     173                return module.Parser(self, self.filename, 
    174174                                           (firstblock, lastblock)) 
    175175            except pdlparser.PDLParserError : 
    176176                pass # try next parser 
    177177        raise pdlparser.PDLParserError, "Analysis of first data block failed." 
    178              
    179 def main() :     
     178 
     179def main() : 
    180180    """Entry point for PDL Analyzer.""" 
    181181    import optparse 
    182182    from copy import copy 
    183      
     183 
    184184    def check_cichoice(option, opt, value) : 
    185185        """To add a CaseIgnore Choice option type.""" 
     
    187187        if valower in [v.lower() for v in option.cichoices] : 
    188188            return valower 
    189         else :     
     189        else : 
    190190            choices = ", ".join([repr(o) for o in option.cichoices]) 
    191191            raise optparse.OptionValueError( 
    192192                "option %s: invalid choice: %r (choose from %s)" 
    193193                % (opt, value, choices)) 
    194      
     194 
    195195    class MyOption(optparse.Option) : 
    196196        """New Option class, with CaseIgnore Choice type.""" 
     
    199199        TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER) 
    200200        TYPE_CHECKER["cichoice"] = check_cichoice 
    201          
    202     parser = optparse.OptionParser(option_class=MyOption,  
     201 
     202    parser = optparse.OptionParser(option_class=MyOption, 
    203203                                   usage="python analyzer.py [options] file1 [file2 ...]") 
    204     parser.add_option("-v", "--version",  
    205                             action="store_true",  
     204    parser.add_option("-v", "--version", 
     205                            action="store_true", 
    206206                            dest="version", 
    207207                            help="Show pkpgcounter's version number and exit.") 
    208     parser.add_option("-d", "--debug",  
    209                             action="store_true",  
     208    parser.add_option("-d", "--debug", 
     209                            action="store_true", 
    210210                            dest="debug", 
    211211                            help="Activate debug mode.") 
    212     parser.add_option("-c", "--colorspace",  
     212    parser.add_option("-c", "--colorspace", 
    213213                            dest="colorspace", 
    214214                            type="cichoice", 
    215215                            cichoices=["bw", "rgb", "cmyk", "cmy", "gc"], 
    216216                            help="Activate the computation of ink usage, and defines the colorspace to use. Supported values are 'BW', 'RGB', 'CMYK', 'CMY', and 'GC'.") 
    217     parser.add_option("-r", "--resolution",  
    218                             type="int",  
    219                             default=72,  
     217    parser.add_option("-r", "--resolution", 
     218                            type="int", 
     219                            default=72, 
    220220                            dest="resolution", 
    221221                            help="The resolution in DPI to use when checking ink usage. Lower resolution is faster but less accurate. Default is 72 dpi.") 
     
    223223    if options.version : 
    224224        print "%s" % version.__version__ 
    225     elif not (72 <= options.resolution <= 1200) :     
     225    elif not (72 <= options.resolution <= 1200) : 
    226226        sys.stderr.write("ERROR: the argument to the --resolution command line option must be between 72 and 1200.\n") 
    227227        sys.stderr.flush() 
     
    229229        if (not arguments) or ((not sys.stdin.isatty()) and ("-" not in arguments)) : 
    230230            arguments.append("-") 
    231         totalsize = 0     
     231        totalsize = 0 
    232232        lines = [] 
    233233        try : 
     
    246246                                except KeyError : 
    247247                                    pass 
    248                             lines.append("      ".join(lineparts))      
    249                 except (IOError, pdlparser.PDLParserError), msg :     
     248                            lines.append("      ".join(lineparts)) 
     249                except (IOError, pdlparser.PDLParserError), msg : 
    250250                    sys.stderr.write("ERROR: %s\n" % msg) 
    251251                    sys.stderr.flush() 
    252         except KeyboardInterrupt :             
     252        except KeyboardInterrupt : 
    253253            sys.stderr.write("WARN: Aborted at user's request.\n") 
    254254            sys.stderr.flush() 
    255         if not options.colorspace :     
     255        if not options.colorspace : 
    256256            print "%i" % totalsize 
    257         else :     
     257        else : 
    258258            print "\n".join(lines) 
    259      
    260 if __name__ == "__main__" :     
     259 
     260if __name__ == "__main__" : 
    261261    main()