[3260] | 1 | # -*- coding: UTF-8 -*- |
---|
[695] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[695] | 4 | # |
---|
[3275] | 5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[873] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[873] | 9 | # (at your option) any later version. |
---|
[3260] | 10 | # |
---|
[873] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[695] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[3304] | 21 | |
---|
| 22 | """This modules defines a command line options parser for PyKota's command line tools.""" |
---|
| 23 | |
---|
[3312] | 24 | import sys |
---|
| 25 | import os |
---|
[3304] | 26 | import optparse |
---|
| 27 | from gettext import gettext as _ |
---|
| 28 | |
---|
| 29 | from pykota import version |
---|
[3335] | 30 | from pykota.utils import loginvalidparam |
---|
[3304] | 31 | |
---|
[3335] | 32 | def checkandset_pagesize(option, opt, value, optionparser) : |
---|
| 33 | """Checks and sets the page size.""" |
---|
| 34 | from pykota.pdfutils import getPageSize |
---|
| 35 | if getPageSize(value) is None : |
---|
| 36 | loginvalidparam(opt, value, option.default) |
---|
| 37 | setattr(optionparser.values, option.dest, option.default) |
---|
| 38 | else : |
---|
| 39 | setattr(optionparser.values, option.dest, value) |
---|
| 40 | |
---|
| 41 | def checkandset_savetoner(option, opt, value, optionparser) : |
---|
| 42 | """Checks and sets the save toner value.""" |
---|
| 43 | if (value < 0.0) or (value > 99.0) : |
---|
| 44 | loginvalidparam(opt, value, option.default, \ |
---|
| 45 | _("Allowed range is (0..99)")) |
---|
| 46 | setattr(optionparser.values, option.dest, option.default) |
---|
| 47 | else : |
---|
| 48 | setattr(optionparser.values, option.dest, value) |
---|
[3337] | 49 | |
---|
| 50 | def checkandset_positiveint(option, opt, value, optionparser) : |
---|
| 51 | """Checks if an option argument is a positive integer and validates the option if it is the case.""" |
---|
| 52 | if not (value >= 0) : |
---|
| 53 | loginvalidparam(opt, value, option.default, \ |
---|
| 54 | _("Value must be positive")) |
---|
| 55 | setattr(optionparser.values, option.dest, option.default) |
---|
| 56 | else : |
---|
| 57 | setattr(optionparser.values, option.dest, value) |
---|
| 58 | |
---|
| 59 | def checkandset_positivefloat(option, opt, value, optionparser) : |
---|
| 60 | """Checks if an option argument is a positive integer and validates the option if it is the case.""" |
---|
| 61 | if not (value >= 0.0) : |
---|
| 62 | loginvalidparam(opt, value, option.default, \ |
---|
| 63 | _("Value must be positive")) |
---|
| 64 | setattr(optionparser.values, option.dest, option.default) |
---|
| 65 | else : |
---|
| 66 | setattr(optionparser.values, option.dest, value) |
---|
[3335] | 67 | |
---|
[3304] | 68 | class PyKotaOptionParser(optparse.OptionParser) : |
---|
| 69 | """ |
---|
| 70 | This class to define additional methods, and different help |
---|
| 71 | formatting, from the traditional OptionParser. |
---|
| 72 | """ |
---|
| 73 | def __init__(self, *args, **kwargs) : |
---|
| 74 | """ |
---|
| 75 | Initializes our option parser with additional attributes. |
---|
| 76 | """ |
---|
| 77 | self.examples = [] |
---|
[3316] | 78 | kwargs["version"] = "%s (PyKota) %s" % (os.path.basename(sys.argv[0]), |
---|
| 79 | version.__version__) |
---|
[3304] | 80 | optparse.OptionParser.__init__(self, *args, **kwargs) |
---|
| 81 | self.disable_interspersed_args() |
---|
[3316] | 82 | self.remove_version_and_help() |
---|
[3315] | 83 | self.add_generic_options() |
---|
[3304] | 84 | |
---|
| 85 | def format_help(self, formatter=None) : |
---|
| 86 | """ |
---|
| 87 | Reformats help our way : adding examples and copyright |
---|
| 88 | message at the end. |
---|
| 89 | """ |
---|
| 90 | if formatter is None : |
---|
| 91 | formatter = self.formatter |
---|
| 92 | result = [] |
---|
| 93 | result.append(optparse.OptionParser.format_help(self, formatter) + "\n") |
---|
[3314] | 94 | result.append(self.format_examples()) |
---|
| 95 | result.append(self.format_copyright()) |
---|
[3304] | 96 | return "".join(result) |
---|
[3314] | 97 | |
---|
| 98 | # |
---|
| 99 | # Below are PyKota specific additions |
---|
| 100 | # |
---|
[3304] | 101 | def format_examples(self, formatter=None) : |
---|
| 102 | """Formats examples our way.""" |
---|
| 103 | if formatter is None : |
---|
| 104 | formatter = self.formatter |
---|
| 105 | result = [] |
---|
| 106 | if self.examples : |
---|
| 107 | result.append(formatter.format_heading(_("examples"))) |
---|
| 108 | formatter.indent() |
---|
| 109 | for (cmd, explanation) in self.examples : |
---|
| 110 | result.append(formatter.format_description(self.expand_prog_name(cmd))) |
---|
| 111 | result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n") |
---|
| 112 | formatter.dedent() |
---|
| 113 | return "".join(result) |
---|
| 114 | |
---|
| 115 | def format_copyright(self, formatter=None) : |
---|
| 116 | """Formats copyright message our way.""" |
---|
| 117 | if formatter is None : |
---|
| 118 | formatter = self.formatter |
---|
| 119 | result = [] |
---|
| 120 | result.append(formatter.format_heading(_("licensing terms"))) |
---|
| 121 | formatter.indent() |
---|
| 122 | result.append(formatter.format_description("(c) %s %s\n" \ |
---|
| 123 | % (version.__years__, \ |
---|
| 124 | version.__author__))) |
---|
| 125 | for part in version.__gplblurb__.split("\n\n") : |
---|
| 126 | result.append(formatter.format_description(part) + "\n") |
---|
| 127 | formatter.dedent() |
---|
| 128 | return "".join(result) |
---|
| 129 | |
---|
| 130 | def add_example(self, command, doc) : |
---|
| 131 | """Adds an usage example.""" |
---|
| 132 | self.examples.append(("%prog " + command, doc)) |
---|
[3312] | 133 | |
---|
[3316] | 134 | def remove_version_and_help(self) : |
---|
| 135 | """Removes the default definitions for options version and help.""" |
---|
| 136 | for o in ("-h", "-help", "--help", "-v", "-version", "--version") : |
---|
| 137 | try : |
---|
| 138 | self.remove_option(o) |
---|
| 139 | except ValueError : |
---|
| 140 | pass |
---|
| 141 | |
---|
[3315] | 142 | def add_generic_options(self) : |
---|
| 143 | """Adds options which are common to all PyKota command line tools.""" |
---|
[3316] | 144 | self.add_option("-h", "--help", |
---|
| 145 | action="help", |
---|
| 146 | help=_("show this help message and exit")) |
---|
[3315] | 147 | self.add_option("-v", "--version", |
---|
[3316] | 148 | action="version", |
---|
[3315] | 149 | help=_("show the version number and exit")) |
---|