Show
Ignore:
Timestamp:
08/14/06 00:21:51 (18 years ago)
Author:
jerome
Message:

Now accepts --colorspace bw | cmyk | cmy | all
and --resolution x (in DPI) on the command line, as a preliminary
before implementing the computation of ink usage.

Files:
1 modified

Legend:

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

    r348 r350  
    2222import sys 
    2323import tempfile 
    24 import optparse 
    2524 
    2625import version, pdlparser, postscript, pdf, pcl345, pclxl, \ 
     
    128127def main() :     
    129128    """Entry point for PDL Analyzer.""" 
    130     parser = optparse.OptionParser(usage="python analyzer.py [options] file1 [file2 ...]") 
     129    import optparse 
     130    from copy import copy 
     131     
     132    def check_cichoice(option, opt, value) : 
     133        """To add a CaseIgnore Choice option type.""" 
     134        valower = value.lower() 
     135        if valower in [v.lower() for v in option.cichoices] : 
     136            return valower 
     137        else :     
     138            choices = ", ".join(map(repr, option.cichoices)) 
     139            raise optparse.OptionValueError( 
     140                "option %s: invalid choice: %r (choose from %s)" 
     141                % (opt, value, choices)) 
     142     
     143    class MyOption(optparse.Option) : 
     144        """New Option class, with CaseIgnore Choice type.""" 
     145        TYPES = optparse.Option.TYPES + ("cichoice",) 
     146        ATTRS = optparse.Option.ATTRS + ["cichoices"] 
     147        TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER) 
     148        TYPE_CHECKER["cichoice"] = check_cichoice 
     149         
     150    parser = optparse.OptionParser(option_class=MyOption,  
     151                                   usage="python analyzer.py [options] file1 [file2 ...]") 
    131152    parser.add_option("-v", "--version",  
    132153                            action="store_true",  
    133154                            dest="version", 
    134                             help="show pkpgcounter's version number and exit.") 
     155                            help="Show pkpgcounter's version number and exit.") 
    135156    parser.add_option("-d", "--debug",  
    136157                            action="store_true",  
    137158                            dest="debug", 
    138                             help="activate debug mode.") 
     159                            help="Activate debug mode.") 
     160    parser.add_option("-c", "--colorspace",  
     161                            dest="colorspace", 
     162                            type="cichoice", 
     163                            cichoices=["bw", "cmyk", "cmy", "all"], 
     164                            help="Activate the computation of ink usage, and defines the colorspace to use. Supported values are 'BW', 'CMYK', 'CMY' and 'ALL'.") 
     165    parser.add_option("-r", "--resolution",  
     166                            type="int",  
     167                            default=150,  
     168                            dest="resolution", 
     169                            help="The resolution in DPI to use when checking ink usage. Lower resolution is faster. Default is 150.") 
    139170    (options, arguments) = parser.parse_args() 
    140171    if options.version : 
     
    144175            arguments.append("-") 
    145176        totalsize = 0     
    146         for arg in arguments : 
    147             try : 
    148                 parser = PDLAnalyzer(arg, options.debug) 
    149                 totalsize += parser.getJobSize() 
    150             except (IOError, pdlparser.PDLParserError), msg :     
    151                 sys.stderr.write("ERROR: %s\n" % msg) 
    152                 sys.stderr.flush() 
     177        try : 
     178            for arg in arguments : 
     179                try : 
     180                    parser = PDLAnalyzer(arg, options.debug) 
     181                    totalsize += parser.getJobSize() 
     182                except (IOError, pdlparser.PDLParserError), msg :     
     183                    sys.stderr.write("ERROR: %s\n" % msg) 
     184                    sys.stderr.flush() 
     185        except KeyboardInterrupt :             
     186            sys.stderr.write("WARN: Aborted at user's request.\n") 
     187            sys.stderr.flush() 
    153188        print "%s" % totalsize 
    154189