root / pykota / trunk / bin / pkprinters @ 1803

Revision 1803, 12.3 kB (checked in by jalet, 20 years ago)

Postponed string interpolation to help message's output method

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