root / pykota / trunk / bin / pkprinters @ 1582

Revision 1582, 12.1 kB (checked in by jalet, 20 years ago)

Added code to handle the description field for printers

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