root / pykota / trunk / bin / pkprinters @ 1437

Revision 1437, 9.9 kB (checked in by jalet, 20 years ago)

pkprinters now accept the --list command line option

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