root / pykota / trunk / bin / pkprinters @ 1452

Revision 1452, 10.5 kB (checked in by jalet, 20 years ago)

Documentation

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