[1330] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # PyKota Printers Manager |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # $Log$ |
---|
[1331] | 26 | # Revision 1.2 2004/02/04 12:52:37 jalet |
---|
| 27 | # pkprinters' help |
---|
| 28 | # |
---|
[1330] | 29 | # Revision 1.1 2004/02/04 11:16:59 jalet |
---|
| 30 | # pkprinters command line tool added. |
---|
| 31 | # |
---|
| 32 | # |
---|
| 33 | # |
---|
| 34 | |
---|
| 35 | import sys |
---|
| 36 | |
---|
| 37 | from pykota import version |
---|
| 38 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 39 | from pykota.config import PyKotaConfigError |
---|
| 40 | from pykota.storage import PyKotaStorageError |
---|
| 41 | |
---|
| 42 | __doc__ = """pkprinters v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
| 43 | A Printers Manager for PyKota. |
---|
| 44 | |
---|
| 45 | command line usage : |
---|
| 46 | |
---|
| 47 | pkprinters [options] printer1 printer2 printer3 ... printerN |
---|
| 48 | |
---|
| 49 | options : |
---|
| 50 | |
---|
| 51 | -v | --version Prints pkprinters's version number then exits. |
---|
| 52 | -h | --help Prints this message then exits. |
---|
| 53 | |
---|
[1331] | 54 | -a | --add Adds printers if they don't exist on the Quota |
---|
| 55 | Storage Server. |
---|
[1330] | 56 | |
---|
[1331] | 57 | -d | --delete Deletes printers from the quota storage. |
---|
[1330] | 58 | |
---|
| 59 | -c | --charge p[,j] Sets the price per page and per job to charge. |
---|
| 60 | Job price is optional. |
---|
| 61 | If both are to be set, separate them with a comma. |
---|
| 62 | Floating point and negative values are allowed. |
---|
| 63 | |
---|
| 64 | -g | --groups pg1[,pg2...] Adds the printer(s) to the printer groups |
---|
| 65 | pg1, pg2, etc... which must already exist. |
---|
| 66 | A printer group is just like a normal printer, |
---|
| 67 | only that it is usually unknown from the printing |
---|
| 68 | system. Create printer groups exactly the same |
---|
| 69 | way that you create printers, then add other |
---|
| 70 | printers to them with this option. |
---|
| 71 | Accounting is done on a printer and on all |
---|
| 72 | the printer groups it belongs to, quota checking |
---|
| 73 | is done on a printer and on all the printer groups |
---|
| 74 | it belongs to. |
---|
| 75 | |
---|
| 76 | printer1 through printerN can contain wildcards if the --add option |
---|
| 77 | is not set. |
---|
| 78 | |
---|
| 79 | examples : |
---|
| 80 | |
---|
| 81 | $ pkprinters --add --charge 0.05,0.1 hp2100 hp2200 hp8000 |
---|
| 82 | |
---|
| 83 | Will create three printers named hp2100, hp2200 and hp8000 |
---|
| 84 | or modify them if they already exist. |
---|
| 85 | Their price per page will be set at 0.05 unit, and their price |
---|
| 86 | per job will be set at 0.1 unit. Units are in your own currency, |
---|
| 87 | or whatever you want them to mean. |
---|
| 88 | |
---|
| 89 | $ pkprinters --delete "*" |
---|
| 90 | |
---|
| 91 | This will completely delete all printers and associated quota information, |
---|
| 92 | as well as their job history. USE WITH CARE ! |
---|
| 93 | |
---|
| 94 | $ pkprinters --groups Laser,HP "hp*" |
---|
| 95 | |
---|
| 96 | This will put all printers which name matches "hp*" into printers groups |
---|
| 97 | Laser and HP, which MUST already exist. |
---|
| 98 | |
---|
| 99 | This program is free software; you can redistribute it and/or modify |
---|
| 100 | it under the terms of the GNU General Public License as published by |
---|
| 101 | the Free Software Foundation; either version 2 of the License, or |
---|
| 102 | (at your option) any later version. |
---|
| 103 | |
---|
| 104 | This program is distributed in the hope that it will be useful, |
---|
| 105 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 106 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 107 | GNU General Public License for more details. |
---|
| 108 | |
---|
| 109 | You should have received a copy of the GNU General Public License |
---|
| 110 | along with this program; if not, write to the Free Software |
---|
| 111 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 112 | |
---|
| 113 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 114 | |
---|
| 115 | class PKPrinters(PyKotaTool) : |
---|
| 116 | """A class for edpykota.""" |
---|
| 117 | def main(self, names, options) : |
---|
| 118 | """Manage printers.""" |
---|
| 119 | if options["groups"] : |
---|
| 120 | printersgroups = self.storage.getMatchingPrinters(options["groups"]) |
---|
| 121 | if not printersgroups : |
---|
| 122 | raise PyKotaToolError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) |
---|
| 123 | |
---|
| 124 | if options["charge"] : |
---|
| 125 | try : |
---|
| 126 | charges = [float(part) for part in options["charge"].split(',', 1)] |
---|
| 127 | except ValueError : |
---|
| 128 | raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] |
---|
| 129 | else : |
---|
| 130 | if len(charges) > 2 : |
---|
| 131 | charges = charges[:2] |
---|
| 132 | if len(charges) != 2 : |
---|
| 133 | charges = [charges[0], None] |
---|
| 134 | (perpage, perjob) = charges |
---|
| 135 | |
---|
| 136 | if options["add"] : |
---|
| 137 | printers = [] |
---|
| 138 | for pname in names : |
---|
| 139 | printer = self.storage.getPrinter(pname) |
---|
| 140 | if not printer.Exists : |
---|
| 141 | if self.isValidName(pname) : |
---|
| 142 | printer = self.storage.addPrinter(pname) |
---|
| 143 | if not printer.Exists : |
---|
| 144 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
---|
| 145 | else : |
---|
| 146 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
---|
| 147 | printers.append(printer) |
---|
| 148 | else : |
---|
| 149 | printers = self.storage.getMatchingPrinters(",".join(names)) |
---|
| 150 | if not printers : |
---|
| 151 | raise PyKotaToolError, _("There's no printer matching %s") % " ".join(names) |
---|
| 152 | |
---|
| 153 | for printer in printers : |
---|
| 154 | if options["delete"] : |
---|
| 155 | printer.delete() |
---|
| 156 | else : |
---|
| 157 | if options["charge"] : |
---|
| 158 | printer.setPrices(perpage, perjob) |
---|
| 159 | if options["groups"] : |
---|
| 160 | for pgroup in printersgroups : |
---|
| 161 | pgroup.addPrinterToGroup(printer) |
---|
| 162 | |
---|
| 163 | if __name__ == "__main__" : |
---|
| 164 | retcode = 0 |
---|
| 165 | try : |
---|
| 166 | short_options = "hvac:dg:" |
---|
| 167 | long_options = ["help", "version", "add", "charge=", "delete", "groups="] |
---|
| 168 | |
---|
| 169 | # Initializes the command line tool |
---|
| 170 | manager = PKPrinters(doc=__doc__) |
---|
| 171 | |
---|
| 172 | # parse and checks the command line |
---|
| 173 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 174 | |
---|
| 175 | # sets long options |
---|
| 176 | options["help"] = options["h"] or options["help"] |
---|
| 177 | options["version"] = options["v"] or options["version"] |
---|
| 178 | options["add"] = options["a"] or options["add"] |
---|
| 179 | options["charge"] = options["c"] or options["charge"] |
---|
| 180 | options["delete"] = options["d"] or options["delete"] |
---|
| 181 | options["groups"] = options["g"] or options["groups"] |
---|
| 182 | |
---|
| 183 | if options["help"] : |
---|
| 184 | manager.display_usage_and_quit() |
---|
| 185 | elif options["version"] : |
---|
| 186 | manager.display_version_and_quit() |
---|
| 187 | elif options["delete"] and (options["add"] or options["groups"] or options["charge"]) : |
---|
| 188 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
| 189 | elif not args : |
---|
| 190 | raise PyKotaToolError, _("You have to pass printer names on the command line") |
---|
| 191 | else : |
---|
| 192 | retcode = manager.main(args, options) |
---|
| 193 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : |
---|
| 194 | sys.stderr.write("%s\n" % msg) |
---|
| 195 | sys.stderr.flush() |
---|
| 196 | retcode = -1 |
---|
| 197 | |
---|
| 198 | try : |
---|
| 199 | manager.storage.close() |
---|
| 200 | except (TypeError, NameError, AttributeError) : |
---|
| 201 | pass |
---|
| 202 | |
---|
| 203 | sys.exit(retcode) |
---|