root / pykota / trunk / bin / pkprinters @ 1330

Revision 1330, 8.0 kB (checked in by jalet, 20 years ago)

pkprinters command line tool added.

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