root / pykota / trunk / bin / pkprinters @ 1796

Revision 1796, 12.2 kB (checked in by jalet, 20 years ago)

Renders help translatable

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