[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) |
---|
| 49 | |
---|
[3304] | 50 | class PyKotaOptionParser(optparse.OptionParser) : |
---|
| 51 | """ |
---|
| 52 | This class to define additional methods, and different help |
---|
| 53 | formatting, from the traditional OptionParser. |
---|
| 54 | """ |
---|
| 55 | def __init__(self, *args, **kwargs) : |
---|
| 56 | """ |
---|
| 57 | Initializes our option parser with additional attributes. |
---|
| 58 | """ |
---|
| 59 | self.examples = [] |
---|
[3316] | 60 | kwargs["version"] = "%s (PyKota) %s" % (os.path.basename(sys.argv[0]), |
---|
| 61 | version.__version__) |
---|
[3304] | 62 | optparse.OptionParser.__init__(self, *args, **kwargs) |
---|
| 63 | self.disable_interspersed_args() |
---|
[3316] | 64 | self.remove_version_and_help() |
---|
[3315] | 65 | self.add_generic_options() |
---|
[3304] | 66 | |
---|
| 67 | def format_help(self, formatter=None) : |
---|
| 68 | """ |
---|
| 69 | Reformats help our way : adding examples and copyright |
---|
| 70 | message at the end. |
---|
| 71 | """ |
---|
| 72 | if formatter is None : |
---|
| 73 | formatter = self.formatter |
---|
| 74 | result = [] |
---|
| 75 | result.append(optparse.OptionParser.format_help(self, formatter) + "\n") |
---|
[3314] | 76 | result.append(self.format_examples()) |
---|
| 77 | result.append(self.format_copyright()) |
---|
[3304] | 78 | return "".join(result) |
---|
[3314] | 79 | |
---|
| 80 | # |
---|
| 81 | # Below are PyKota specific additions |
---|
| 82 | # |
---|
[3304] | 83 | def format_examples(self, formatter=None) : |
---|
| 84 | """Formats examples our way.""" |
---|
| 85 | if formatter is None : |
---|
| 86 | formatter = self.formatter |
---|
| 87 | result = [] |
---|
| 88 | if self.examples : |
---|
| 89 | result.append(formatter.format_heading(_("examples"))) |
---|
| 90 | formatter.indent() |
---|
| 91 | for (cmd, explanation) in self.examples : |
---|
| 92 | result.append(formatter.format_description(self.expand_prog_name(cmd))) |
---|
| 93 | result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n") |
---|
| 94 | formatter.dedent() |
---|
| 95 | return "".join(result) |
---|
| 96 | |
---|
| 97 | def format_copyright(self, formatter=None) : |
---|
| 98 | """Formats copyright message our way.""" |
---|
| 99 | if formatter is None : |
---|
| 100 | formatter = self.formatter |
---|
| 101 | result = [] |
---|
| 102 | result.append(formatter.format_heading(_("licensing terms"))) |
---|
| 103 | formatter.indent() |
---|
| 104 | result.append(formatter.format_description("(c) %s %s\n" \ |
---|
| 105 | % (version.__years__, \ |
---|
| 106 | version.__author__))) |
---|
| 107 | for part in version.__gplblurb__.split("\n\n") : |
---|
| 108 | result.append(formatter.format_description(part) + "\n") |
---|
| 109 | formatter.dedent() |
---|
| 110 | return "".join(result) |
---|
| 111 | |
---|
| 112 | def add_example(self, command, doc) : |
---|
| 113 | """Adds an usage example.""" |
---|
| 114 | self.examples.append(("%prog " + command, doc)) |
---|
[3312] | 115 | |
---|
[3316] | 116 | def remove_version_and_help(self) : |
---|
| 117 | """Removes the default definitions for options version and help.""" |
---|
| 118 | for o in ("-h", "-help", "--help", "-v", "-version", "--version") : |
---|
| 119 | try : |
---|
| 120 | self.remove_option(o) |
---|
| 121 | except ValueError : |
---|
| 122 | pass |
---|
| 123 | |
---|
[3315] | 124 | def add_generic_options(self) : |
---|
| 125 | """Adds options which are common to all PyKota command line tools.""" |
---|
[3316] | 126 | self.add_option("-h", "--help", |
---|
| 127 | action="help", |
---|
| 128 | help=_("show this help message and exit")) |
---|
[3315] | 129 | self.add_option("-v", "--version", |
---|
[3316] | 130 | action="version", |
---|
[3315] | 131 | help=_("show the version number and exit")) |
---|