root / pykota / trunk / bin / pkbcodes @ 2332

Revision 2332, 8.8 kB (checked in by jerome, 19 years ago)

Added the skeleton for the new pkbcodes command, to manage
billing codes.

  • 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 Billing Codes manager
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import os
28import sys
29import pwd
30
31from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
32
33__doc__ = N_("""pkbcodes v%s (c) %s %s
34
35A billing codes Manager for PyKota.
36
37command line usage :
38
39  pkbcodes [options] code1 code2 code3 ... codeN
40
41options :
42
43  -v | --version       Prints pkbcodes version number then exits.
44  -h | --help          Prints this message then exits.
45 
46  -a | --add           Adds billing codes if they don't exist in PyKota's
47                       database. If they exist, they are modified
48                       unless -s|--skipexisting is also used.
49
50  -d | --delete        Deletes billing codes from PyKota's database.
51
52  -D | --description d Adds a textual description to billing codes.
53
54  -l | --list          List informations about the billing codes.
55
56  -s | --reset         Resets the billing codes' balance and page counters
57                       to 0.
58
59  -s | --skipexisting  In combination with the --add option above, tells
60                       pkbcodes to not modify existing printers.
61
62  code1 through codeN can contain wildcards if the --add option
63  is not set.
64
65examples :                             
66
67  $ pkbcodes --add -D "My project" myproj
68
69  Will create the myproj billing code with "My project"
70  as the description.
71
72  $ pkbcodes --delete "*"
73
74  This will completely delete all the billing codes, but without
75  removing any matching job from the history. USE WITH CARE ANYWAY !
76 
77  $ pkbcodes --list "my*"
78 
79  This will list all billing codes which name begins with "my".
80 
81This program is free software; you can redistribute it and/or modify
82it under the terms of the GNU General Public License as published by
83the Free Software Foundation; either version 2 of the License, or
84(at your option) any later version.
85
86This program is distributed in the hope that it will be useful,
87but WITHOUT ANY WARRANTY; without even the implied warranty of
88MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
89GNU General Public License for more details.
90
91You should have received a copy of the GNU General Public License
92along with this program; if not, write to the Free Software
93Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
94
95Please e-mail bugs to: %s""")
96       
97class PKBcodes(PyKotaTool) :       
98    """A class for edpykota."""
99    def main(self, names, options) :
100        """Manage printers."""
101        raise "Not Implemented yet !!! Please be patient !!!"
102       
103        if (not self.config.isAdmin) and (not options["list"]) :
104            raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
105           
106        if options["list"] and not names :
107            names = ["*"]
108           
109        if options["groups"] :       
110            printersgroups = self.storage.getMatchingPrinters(options["groups"])
111            if not printersgroups :
112                raise PyKotaToolError, _("There's no printer matching %s") % " ".join(options["groups"].split(','))
113           
114        if options["charge"] :
115            try :
116                charges = [float(part) for part in options["charge"].split(',', 1)]
117            except ValueError :   
118                raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"]
119            else :   
120                if len(charges) > 2 :
121                    charges = charges[:2]
122                if len(charges) != 2 :
123                    charges = [charges[0], None]
124                (perpage, perjob) = charges
125               
126        if options["add"] :   
127            printers = []
128            for pname in names :
129                printer = self.storage.getPrinter(pname)
130                if printer.Exists :
131                    if options["skipexisting"] :
132                        self.printInfo(_("Printer %s already exists, skipping.") % printer.Name)
133                    else :   
134                        self.printInfo(_("Printer %s already exists, will be modified.") % printer.Name)
135                        printers.append(printer)
136                else :
137                    if self.isValidName(pname) :
138                        printer = self.storage.addPrinter(pname)
139                        if not printer.Exists :
140                            raise PyKotaToolError, _("Impossible to add printer %s") % pname
141                        else :   
142                            printers.append(printer)
143                    else :   
144                        raise PyKotaToolError, _("Invalid printer name %s") % pname
145        else :       
146            printers = self.storage.getMatchingPrinters(",".join(names))
147            if not printers :
148                raise PyKotaToolError, _("There's no printer matching %s") % " ".join(names)
149                   
150        for printer in printers :       
151            if options["delete"] :
152                printer.delete()
153            elif options["list"] :   
154                parents = ", ".join([p.Name for p in self.storage.getParentPrinters(printer)])
155                if parents : 
156                    parents = "%s %s" % (_("in"), parents)
157                print "%s [%s] (%s + #*%s) %s" % \
158                      (printer.Name, printer.Description, printer.PricePerJob, \
159                       printer.PricePerPage, parents)
160            else :   
161                if options["charge"] :
162                    printer.setPrices(perpage, perjob)   
163                if options["description"] is not None :
164                    printer.setDescription(options["description"].strip())
165                if options["groups"] :   
166                    for pgroup in printersgroups :
167                        if options["remove"] :
168                            pgroup.delPrinterFromGroup(printer)
169                        else :
170                            pgroup.addPrinterToGroup(printer)   
171                     
172if __name__ == "__main__" : 
173    retcode = 0
174    try :
175        short_options = "hvaD:dlrs"
176        long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"]
177       
178        # Initializes the command line tool
179        manager = PKBcodes(doc=__doc__)
180        manager.deferredInit()
181       
182        # parse and checks the command line
183        (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options)
184       
185        # sets long options
186        options["help"] = options["h"] or options["help"]
187        options["version"] = options["v"] or options["version"]
188        options["add"] = options["a"] or options["add"]
189        options["description"] = options["D"] or options["description"]
190        options["delete"] = options["d"] or options["delete"] 
191        options["list"] = options["l"] or options["list"]
192        options["reset"] = options["r"] or options["reset"]
193        options["skipexisting"] = options["s"] or options["skipexisting"]
194       
195        if options["help"] :
196            manager.display_usage_and_quit()
197        elif options["version"] :
198            manager.display_version_and_quit()
199        elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \
200           or (options["skipexisting"] and not options["add"]) \
201           or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) :
202            raise PyKotaToolError, _("incompatible options, see help.")
203        elif (not args) and (options["add"] or options["delete"]) :   
204            raise PyKotaToolError, _("You have to pass billing codes on the command line")
205        else :
206            retcode = manager.main(args, options)
207    except KeyboardInterrupt :       
208        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
209    except SystemExit :       
210        pass
211    except :
212        try :
213            manager.crashed("pkbcodes failed")
214        except :   
215            crashed("pkbcodes failed")
216        retcode = -1
217
218    try :
219        manager.storage.close()
220    except (TypeError, NameError, AttributeError) :   
221        pass
222       
223    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.