root / pykota / trunk / bin / pkprinters @ 1774

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

pykosd is now a very good tool

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