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