root / pykota / trunk / bin / pkprinters @ 1447

Revision 1438, 10.0 kB (checked in by jalet, 21 years ago)

Better formatting

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