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 ...]") |
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.") |
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() |