root / pykota / trunk / bin / pkprinters @ 2024

Revision 1853, 12.8 kB (checked in by jalet, 20 years ago)

Fixed typo in help

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