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