root / pykota / trunk / bin / pkprinters @ 1451

Revision 1451, 10.3 kB (checked in by jalet, 20 years ago)

pkpgcounter : comments
pkprinters : when --add is used, existing printers are now skipped.

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