root / pykota / trunk / bin / pkprinters @ 1410

Revision 1332, 9.0 kB (checked in by jalet, 21 years ago)

pkprinters can now remove printers from printers groups.

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