root / pykota / trunk / bin / pkprinters @ 1526

Revision 1526, 11.4 kB (checked in by jalet, 20 years ago)

Fixed over-verbose exits when displaying help or version number

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