root / pykota / trunk / bin / pkbcodes @ 3288

Revision 3288, 8.7 kB (checked in by jerome, 16 years ago)

Moved all exceptions definitions to a dedicated module.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: UTF-8 -*-
3#
4# PyKota : Print Quotas for CUPS
5#
6# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22#
23
24import os
25import sys
26import pwd
27
28from pykota.errors import PyKotaCommandLineError
29from pykota.tool import Percent, PyKotaTool, crashed, N_
30from pykota.storage import StorageBillingCode
31
32__doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s
33
34A billing codes Manager for PyKota.
35
36command line usage :
37
38  pkbcodes [options] code1 code2 code3 ... codeN
39
40options :
41
42  -v | --version       Prints pkbcodes version number then exits.
43  -h | --help          Prints this message then exits.
44 
45  -a | --add           Adds billing codes if they don't exist in PyKota's
46                       database. If they exist, they are modified
47                       unless -s|--skipexisting is also used.
48
49  -d | --delete        Deletes billing codes from PyKota's database.
50                       NB : the history entries with this billing code
51                       are not deleted, voluntarily.
52
53  -D | --description d Adds a textual description to billing codes.
54
55  -l | --list          List informations about the billing codes.
56
57  -r | --reset         Resets the billing codes' balance and page counters
58                       to 0.
59
60  -s | --skipexisting  In combination with the --add option above, tells
61                       pkbcodes to not modify existing billing codes.
62
63  code1 through codeN can contain wildcards if the --add option
64  is not set.
65
66examples :                             
67
68  $ pkbcodes --add -D "My project" myproj
69
70  Will create the myproj billing code with "My project"
71  as the description.
72
73  $ pkbcodes --delete "*"
74
75  This will completely delete all the billing codes, but without
76  removing any matching job from the history. USE WITH CARE ANYWAY !
77 
78  $ pkbcodes --list "my*"
79 
80  This will list all billing codes which name begins with 'my'.
81""")
82       
83class PKBcodes(PyKotaTool) :       
84    """A class for a billing codes manager."""
85    def modifyBillingCode(self, billingcode, reset, description) :
86        """Modifies a billing code."""
87        if reset :
88            billingcode.reset()   
89        if description is not None : # NB : "" is allowed !
90            billingcode.setDescription(description)
91       
92    def main(self, names, options) :
93        """Manage billing codes."""
94        if (not self.config.isAdmin) and (not options["list"]) :
95            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
96           
97        if not options["list"] :   
98            percent = Percent(self)
99           
100        if not options["add"] :
101            if not options["list"] :
102                percent.display("%s..." % _("Extracting datas"))
103            if not names :      # NB : can't happen for --delete because it's catched earlier
104                names = ["*"]
105            billingcodes = self.storage.getMatchingBillingCodes(",".join(names))
106            if not billingcodes :
107                if not options["list"] :
108                    percent.display("\n")
109                raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names)
110            if not options["list"] :   
111                percent.setSize(len(billingcodes))
112                       
113        if options["list"] :
114            for billingcode in billingcodes :
115                print "%s [%s] %s %s %s %.2f %s" % \
116                      (billingcode.BillingCode, billingcode.Description, \
117                       billingcode.PageCounter, \
118                       _("pages"), \
119                       _("and"), \
120                       billingcode.Balance, \
121                       _("credits"))
122        elif options["delete"] :   
123            percent.display("\n%s..." % _("Deletion"))
124            self.storage.deleteManyBillingCodes(billingcodes)
125            percent.display("\n")
126        else :
127            reset = options["reset"]
128            description = options["description"]
129            if description :
130                description = options["description"].strip()
131            skipexisting = options["skipexisting"]       
132           
133            self.storage.beginTransaction()
134            try :
135                if options["add"] :   
136                    percent.display("%s...\n" % _("Creation"))
137                    percent.setSize(len(names))
138                    for bname in names :
139                        billingcode = StorageBillingCode(self.storage, bname)
140                        self.modifyBillingCode(billingcode, reset, description)
141                        oldbillingcode = self.storage.addBillingCode(billingcode)
142                        if oldbillingcode is not None :
143                            if skipexisting :
144                                self.logdebug(_("Billing code [%s] already exists, skipping.") % bname)
145                            else :   
146                                self.logdebug(_("Billing code [%s] already exists, will be modified.") % bname)
147                                self.modifyBillingCode(oldbillingcode, reset, description)
148                                oldbillingcode.save()
149                        percent.oneMore()
150                else :       
151                    percent.display("\n%s...\n" % _("Modification"))
152                    for billingcode in billingcodes :
153                        self.modifyBillingCode(billingcode, reset, description)
154                        billingcode.save()   
155                        percent.oneMore()
156            except :                   
157                self.storage.rollbackTransaction()
158                raise
159            else :   
160                self.storage.commitTransaction()
161                       
162        if not options["list"] :
163            percent.done()
164                     
165if __name__ == "__main__" : 
166    retcode = 0
167    try :
168        short_options = "hvaD:dlrs"
169        long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"]
170       
171        # Initializes the command line tool
172        manager = PKBcodes(doc=__doc__)
173        manager.deferredInit()
174       
175        # parse and checks the command line
176        (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options)
177       
178        # sets long options
179        options["help"] = options["h"] or options["help"]
180        options["version"] = options["v"] or options["version"]
181        options["add"] = options["a"] or options["add"]
182        options["description"] = options["D"] or options["description"]
183        options["delete"] = options["d"] or options["delete"] 
184        options["list"] = options["l"] or options["list"]
185        options["reset"] = options["r"] or options["reset"]
186        options["skipexisting"] = options["s"] or options["skipexisting"]
187       
188        if options["help"] :
189            manager.display_usage_and_quit()
190        elif options["version"] :
191            manager.display_version_and_quit()
192        elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \
193           or (options["skipexisting"] and not options["add"]) \
194           or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) :
195            raise PyKotaCommandLineError, _("incompatible options, see help.")
196        elif (not args) and (options["add"] or options["delete"]) :   
197            raise PyKotaCommandLineError, _("You have to pass billing codes on the command line")
198        else :
199            retcode = manager.main(args, options)
200    except KeyboardInterrupt :       
201        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
202        retcode = -3
203    except PyKotaCommandLineError, msg :   
204        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
205        retcode = -2
206    except SystemExit :       
207        pass
208    except :
209        try :
210            manager.crashed("pkbcodes failed")
211        except :   
212            crashed("pkbcodes failed")
213        retcode = -1
214
215    try :
216        manager.storage.close()
217    except (TypeError, NameError, AttributeError) :   
218        pass
219       
220    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.