[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$ |
---|
[1451] | 26 | # Revision 1.6 2004/05/06 12:37:29 jalet |
---|
| 27 | # pkpgcounter : comments |
---|
| 28 | # pkprinters : when --add is used, existing printers are now skipped. |
---|
| 29 | # |
---|
[1438] | 30 | # Revision 1.5 2004/04/16 16:52:09 jalet |
---|
| 31 | # Better formatting |
---|
| 32 | # |
---|
[1437] | 33 | # Revision 1.4 2004/04/16 16:47:57 jalet |
---|
| 34 | # pkprinters now accept the --list command line option |
---|
| 35 | # |
---|
[1332] | 36 | # Revision 1.3 2004/02/04 13:24:41 jalet |
---|
| 37 | # pkprinters can now remove printers from printers groups. |
---|
| 38 | # |
---|
[1331] | 39 | # Revision 1.2 2004/02/04 12:52:37 jalet |
---|
| 40 | # pkprinters' help |
---|
| 41 | # |
---|
[1330] | 42 | # Revision 1.1 2004/02/04 11:16:59 jalet |
---|
| 43 | # pkprinters command line tool added. |
---|
| 44 | # |
---|
| 45 | # |
---|
| 46 | # |
---|
| 47 | |
---|
| 48 | import sys |
---|
| 49 | |
---|
| 50 | from pykota import version |
---|
| 51 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 52 | from pykota.config import PyKotaConfigError |
---|
| 53 | from pykota.storage import PyKotaStorageError |
---|
| 54 | |
---|
| 55 | __doc__ = """pkprinters v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
| 56 | A Printers Manager for PyKota. |
---|
| 57 | |
---|
| 58 | command line usage : |
---|
| 59 | |
---|
| 60 | pkprinters [options] printer1 printer2 printer3 ... printerN |
---|
| 61 | |
---|
| 62 | options : |
---|
| 63 | |
---|
| 64 | -v | --version Prints pkprinters's version number then exits. |
---|
| 65 | -h | --help Prints this message then exits. |
---|
| 66 | |
---|
[1331] | 67 | -a | --add Adds printers if they don't exist on the Quota |
---|
| 68 | Storage Server. |
---|
[1330] | 69 | |
---|
[1331] | 70 | -d | --delete Deletes printers from the quota storage. |
---|
[1330] | 71 | |
---|
| 72 | -c | --charge p[,j] Sets the price per page and per job to charge. |
---|
| 73 | Job price is optional. |
---|
| 74 | If both are to be set, separate them with a comma. |
---|
| 75 | Floating point and negative values are allowed. |
---|
| 76 | |
---|
[1332] | 77 | -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer |
---|
| 78 | groups pg1, pg2, etc... which must already exist. |
---|
[1330] | 79 | A printer group is just like a normal printer, |
---|
| 80 | only that it is usually unknown from the printing |
---|
| 81 | system. Create printer groups exactly the same |
---|
| 82 | way that you create printers, then add other |
---|
| 83 | printers to them with this option. |
---|
| 84 | Accounting is done on a printer and on all |
---|
| 85 | the printer groups it belongs to, quota checking |
---|
| 86 | is done on a printer and on all the printer groups |
---|
| 87 | it belongs to. |
---|
[1332] | 88 | If the --remove option below is not used, the |
---|
| 89 | default action is to add printers to the specified |
---|
| 90 | printer groups. |
---|
| 91 | |
---|
[1437] | 92 | -l | --list List informations about the printer(s) and the |
---|
| 93 | printers groups it is a member of. |
---|
| 94 | |
---|
[1332] | 95 | -r | --remove In combination with the --groups option above, |
---|
| 96 | remove printers from the specified printers groups. |
---|
[1330] | 97 | |
---|
| 98 | printer1 through printerN can contain wildcards if the --add option |
---|
| 99 | is not set. |
---|
| 100 | |
---|
| 101 | examples : |
---|
| 102 | |
---|
| 103 | $ pkprinters --add --charge 0.05,0.1 hp2100 hp2200 hp8000 |
---|
| 104 | |
---|
| 105 | Will create three printers named hp2100, hp2200 and hp8000 |
---|
| 106 | or modify them if they already exist. |
---|
| 107 | Their price per page will be set at 0.05 unit, and their price |
---|
| 108 | per job will be set at 0.1 unit. Units are in your own currency, |
---|
| 109 | or whatever you want them to mean. |
---|
| 110 | |
---|
| 111 | $ pkprinters --delete "*" |
---|
| 112 | |
---|
| 113 | This will completely delete all printers and associated quota information, |
---|
| 114 | as well as their job history. USE WITH CARE ! |
---|
| 115 | |
---|
| 116 | $ pkprinters --groups Laser,HP "hp*" |
---|
| 117 | |
---|
| 118 | This will put all printers which name matches "hp*" into printers groups |
---|
| 119 | Laser and HP, which MUST already exist. |
---|
| 120 | |
---|
[1332] | 121 | $ pkprinters --groups LexMark --remove hp2200 |
---|
| 122 | |
---|
| 123 | This will remove the hp2200 printer from the LexMark printer group. |
---|
| 124 | |
---|
[1330] | 125 | This program is free software; you can redistribute it and/or modify |
---|
| 126 | it under the terms of the GNU General Public License as published by |
---|
| 127 | the Free Software Foundation; either version 2 of the License, or |
---|
| 128 | (at your option) any later version. |
---|
| 129 | |
---|
| 130 | This program is distributed in the hope that it will be useful, |
---|
| 131 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 132 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 133 | GNU General Public License for more details. |
---|
| 134 | |
---|
| 135 | You should have received a copy of the GNU General Public License |
---|
| 136 | along with this program; if not, write to the Free Software |
---|
| 137 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 138 | |
---|
| 139 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 140 | |
---|
| 141 | class PKPrinters(PyKotaTool) : |
---|
| 142 | """A class for edpykota.""" |
---|
| 143 | def main(self, names, options) : |
---|
| 144 | """Manage printers.""" |
---|
[1437] | 145 | if options["list"] and not names : |
---|
| 146 | names = ["*"] |
---|
| 147 | |
---|
[1330] | 148 | if options["groups"] : |
---|
| 149 | printersgroups = self.storage.getMatchingPrinters(options["groups"]) |
---|
| 150 | if not printersgroups : |
---|
| 151 | raise PyKotaToolError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) |
---|
| 152 | |
---|
| 153 | if options["charge"] : |
---|
| 154 | try : |
---|
| 155 | charges = [float(part) for part in options["charge"].split(',', 1)] |
---|
| 156 | except ValueError : |
---|
| 157 | raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] |
---|
| 158 | else : |
---|
| 159 | if len(charges) > 2 : |
---|
| 160 | charges = charges[:2] |
---|
| 161 | if len(charges) != 2 : |
---|
| 162 | charges = [charges[0], None] |
---|
| 163 | (perpage, perjob) = charges |
---|
| 164 | |
---|
| 165 | if options["add"] : |
---|
| 166 | printers = [] |
---|
| 167 | for pname in names : |
---|
| 168 | printer = self.storage.getPrinter(pname) |
---|
[1451] | 169 | if printer.Exists : |
---|
| 170 | self.logger.log_message(_("Printer %s already exists, skipping.") % printer.Name, "warn") |
---|
| 171 | else : |
---|
[1330] | 172 | if self.isValidName(pname) : |
---|
| 173 | printer = self.storage.addPrinter(pname) |
---|
| 174 | if not printer.Exists : |
---|
| 175 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
---|
[1451] | 176 | else : |
---|
| 177 | printers.append(printer) |
---|
[1330] | 178 | else : |
---|
| 179 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
---|
| 180 | else : |
---|
| 181 | printers = self.storage.getMatchingPrinters(",".join(names)) |
---|
| 182 | if not printers : |
---|
| 183 | raise PyKotaToolError, _("There's no printer matching %s") % " ".join(names) |
---|
| 184 | |
---|
| 185 | for printer in printers : |
---|
| 186 | if options["delete"] : |
---|
| 187 | printer.delete() |
---|
[1437] | 188 | elif options["list"] : |
---|
| 189 | parents = ", ".join([p.Name for p in self.storage.getParentPrinters(printer)]) |
---|
| 190 | if parents : |
---|
| 191 | parents = "%s %s" % (_("in"), parents) |
---|
[1438] | 192 | print "%s (%s + #*%s) %s" % \ |
---|
[1437] | 193 | (printer.Name, printer.PricePerJob, \ |
---|
| 194 | printer.PricePerPage, parents) |
---|
[1330] | 195 | else : |
---|
| 196 | if options["charge"] : |
---|
| 197 | printer.setPrices(perpage, perjob) |
---|
| 198 | if options["groups"] : |
---|
| 199 | for pgroup in printersgroups : |
---|
[1332] | 200 | if options["remove"] : |
---|
| 201 | pgroup.delPrinterFromGroup(printer) |
---|
| 202 | else : |
---|
| 203 | pgroup.addPrinterToGroup(printer) |
---|
[1330] | 204 | |
---|
| 205 | if __name__ == "__main__" : |
---|
| 206 | retcode = 0 |
---|
| 207 | try : |
---|
[1437] | 208 | short_options = "hvac:dg:lr" |
---|
| 209 | long_options = ["help", "version", "add", "charge=", "delete", "groups=", "list", "remove"] |
---|
[1330] | 210 | |
---|
| 211 | # Initializes the command line tool |
---|
| 212 | manager = PKPrinters(doc=__doc__) |
---|
| 213 | |
---|
| 214 | # parse and checks the command line |
---|
| 215 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 216 | |
---|
| 217 | # sets long options |
---|
| 218 | options["help"] = options["h"] or options["help"] |
---|
| 219 | options["version"] = options["v"] or options["version"] |
---|
| 220 | options["add"] = options["a"] or options["add"] |
---|
| 221 | options["charge"] = options["c"] or options["charge"] |
---|
| 222 | options["delete"] = options["d"] or options["delete"] |
---|
| 223 | options["groups"] = options["g"] or options["groups"] |
---|
[1437] | 224 | options["list"] = options["l"] or options["list"] |
---|
[1332] | 225 | options["remove"] = options["r"] or options["remove"] |
---|
[1330] | 226 | |
---|
| 227 | if options["help"] : |
---|
| 228 | manager.display_usage_and_quit() |
---|
| 229 | elif options["version"] : |
---|
| 230 | manager.display_version_and_quit() |
---|
[1437] | 231 | elif (options["delete"] and (options["add"] or options["groups"] or options["charge"] or options["remove"])) \ |
---|
| 232 | or (options["list"] and (options["add"] or options["delete"] or options["groups"] or options["charge"] or options["remove"])) : |
---|
[1330] | 233 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[1332] | 234 | elif options["remove"] and not options["groups"] : |
---|
| 235 | raise PyKotaToolError, _("You have to pass printer groups names on the command line") |
---|
[1437] | 236 | elif (not args) and (not options["list"]) : |
---|
[1330] | 237 | raise PyKotaToolError, _("You have to pass printer names on the command line") |
---|
| 238 | else : |
---|
| 239 | retcode = manager.main(args, options) |
---|
| 240 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : |
---|
| 241 | sys.stderr.write("%s\n" % msg) |
---|
| 242 | sys.stderr.flush() |
---|
| 243 | retcode = -1 |
---|
| 244 | |
---|
| 245 | try : |
---|
| 246 | manager.storage.close() |
---|
| 247 | except (TypeError, NameError, AttributeError) : |
---|
| 248 | pass |
---|
| 249 | |
---|
| 250 | sys.exit(retcode) |
---|