#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKota Printers Manager # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003-2004 Jerome Alet # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # $Log$ # Revision 1.3 2004/02/04 13:24:41 jalet # pkprinters can now remove printers from printers groups. # # Revision 1.2 2004/02/04 12:52:37 jalet # pkprinters' help # # Revision 1.1 2004/02/04 11:16:59 jalet # pkprinters command line tool added. # # # import sys from pykota import version from pykota.tool import PyKotaTool, PyKotaToolError from pykota.config import PyKotaConfigError from pykota.storage import PyKotaStorageError __doc__ = """pkprinters v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres A Printers Manager for PyKota. command line usage : pkprinters [options] printer1 printer2 printer3 ... printerN options : -v | --version Prints pkprinters's version number then exits. -h | --help Prints this message then exits. -a | --add Adds printers if they don't exist on the Quota Storage Server. -d | --delete Deletes printers from the quota storage. -c | --charge p[,j] Sets the price per page and per job to charge. Job price is optional. If both are to be set, separate them with a comma. Floating point and negative values are allowed. -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer groups pg1, pg2, etc... which must already exist. A printer group is just like a normal printer, only that it is usually unknown from the printing system. Create printer groups exactly the same way that you create printers, then add other printers to them with this option. Accounting is done on a printer and on all the printer groups it belongs to, quota checking is done on a printer and on all the printer groups it belongs to. If the --remove option below is not used, the default action is to add printers to the specified printer groups. -r | --remove In combination with the --groups option above, remove printers from the specified printers groups. printer1 through printerN can contain wildcards if the --add option is not set. examples : $ pkprinters --add --charge 0.05,0.1 hp2100 hp2200 hp8000 Will create three printers named hp2100, hp2200 and hp8000 or modify them if they already exist. Their price per page will be set at 0.05 unit, and their price per job will be set at 0.1 unit. Units are in your own currency, or whatever you want them to mean. $ pkprinters --delete "*" This will completely delete all printers and associated quota information, as well as their job history. USE WITH CARE ! $ pkprinters --groups Laser,HP "hp*" This will put all printers which name matches "hp*" into printers groups Laser and HP, which MUST already exist. $ pkprinters --groups LexMark --remove hp2200 This will remove the hp2200 printer from the LexMark printer group. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Please e-mail bugs to: %s""" % (version.__version__, version.__author__) class PKPrinters(PyKotaTool) : """A class for edpykota.""" def main(self, names, options) : """Manage printers.""" if options["groups"] : printersgroups = self.storage.getMatchingPrinters(options["groups"]) if not printersgroups : raise PyKotaToolError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) if options["charge"] : try : charges = [float(part) for part in options["charge"].split(',', 1)] except ValueError : raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] else : if len(charges) > 2 : charges = charges[:2] if len(charges) != 2 : charges = [charges[0], None] (perpage, perjob) = charges if options["add"] : printers = [] for pname in names : printer = self.storage.getPrinter(pname) if not printer.Exists : if self.isValidName(pname) : printer = self.storage.addPrinter(pname) if not printer.Exists : raise PyKotaToolError, _("Impossible to add printer %s") % pname else : raise PyKotaToolError, _("Invalid printer name %s") % pname printers.append(printer) else : printers = self.storage.getMatchingPrinters(",".join(names)) if not printers : raise PyKotaToolError, _("There's no printer matching %s") % " ".join(names) for printer in printers : if options["delete"] : printer.delete() else : if options["charge"] : printer.setPrices(perpage, perjob) if options["groups"] : for pgroup in printersgroups : if options["remove"] : pgroup.delPrinterFromGroup(printer) else : pgroup.addPrinterToGroup(printer) if __name__ == "__main__" : retcode = 0 try : short_options = "hvac:dg:r" long_options = ["help", "version", "add", "charge=", "delete", "groups=", "remove"] # Initializes the command line tool manager = PKPrinters(doc=__doc__) # parse and checks the command line (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) # sets long options options["help"] = options["h"] or options["help"] options["version"] = options["v"] or options["version"] options["add"] = options["a"] or options["add"] options["charge"] = options["c"] or options["charge"] options["delete"] = options["d"] or options["delete"] options["groups"] = options["g"] or options["groups"] options["remove"] = options["r"] or options["remove"] if options["help"] : manager.display_usage_and_quit() elif options["version"] : manager.display_version_and_quit() elif options["delete"] and (options["add"] or options["groups"] or options["charge"] or options["remove"]) : raise PyKotaToolError, _("incompatible options, see help.") elif options["remove"] and not options["groups"] : raise PyKotaToolError, _("You have to pass printer groups names on the command line") elif not args : raise PyKotaToolError, _("You have to pass printer names on the command line") else : retcode = manager.main(args, options) except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : sys.stderr.write("%s\n" % msg) sys.stderr.flush() retcode = -1 try : manager.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(retcode)