root / pykota / trunk / bin / pkbcodes @ 3260

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

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

  • 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 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.tool import Percent, PyKotaTool, PyKotaCommandLineError, crashed, N_
29from pykota.storage import StorageBillingCode
30
31__doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s
32
33A billing codes Manager for PyKota.
34
35command line usage :
36
37  pkbcodes [options] code1 code2 code3 ... codeN
38
39options :
40
41  -v | --version       Prints pkbcodes version number then exits.
42  -h | --help          Prints this message then exits.
43 
44  -a | --add           Adds billing codes if they don't exist in PyKota's
45                       database. If they exist, they are modified
46                       unless -s|--skipexisting is also used.
47
48  -d | --delete        Deletes billing codes from PyKota's database.
49                       NB : the history entries with this billing code
50                       are not deleted, voluntarily.
51
52  -D | --description d Adds a textual description to billing codes.
53
54  -l | --list          List informations about the billing codes.
55
56  -r | --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 billing codes.
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""")
81       
82class PKBcodes(PyKotaTool) :       
83    """A class for a billing codes manager."""
84    def modifyBillingCode(self, billingcode, reset, description) :
85        """Modifies a billing code."""
86        if reset :
87            billingcode.reset()   
88        if description is not None : # NB : "" is allowed !
89            billingcode.setDescription(description)
90       
91    def main(self, names, options) :
92        """Manage billing codes."""
93        if (not self.config.isAdmin) and (not options["list"]) :
94            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
95           
96        if not options["list"] :   
97            percent = Percent(self)
98           
99        if not options["add"] :
100            if not options["list"] :
101                percent.display("%s..." % _("Extracting datas"))
102            if not names :      # NB : can't happen for --delete because it's catched earlier
103                names = ["*"]
104            billingcodes = self.storage.getMatchingBillingCodes(",".join(names))
105            if not billingcodes :
106                if not options["list"] :
107                    percent.display("\n")
108                raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names)
109            if not options["list"] :   
110                percent.setSize(len(billingcodes))
111                       
112        if options["list"] :
113            for billingcode in billingcodes :
114                print "%s [%s] %s %s %s %.2f %s" % \
115                      (billingcode.BillingCode, billingcode.Description, \
116                       billingcode.PageCounter, \
117                       _("pages"), \
118                       _("and"), \
119                       billingcode.Balance, \
120                       _("credits"))
121        elif options["delete"] :   
122            percent.display("\n%s..." % _("Deletion"))
123            self.storage.deleteManyBillingCodes(billingcodes)
124            percent.display("\n")
125        else :
126            reset = options["reset"]
127            description = options["description"]
128            if description :
129                description = options["description"].strip()
130            skipexisting = options["skipexisting"]       
131           
132            self.storage.beginTransaction()
133            try :
134                if options["add"] :   
135                    percent.display("%s...\n" % _("Creation"))
136                    percent.setSize(len(names))
137                    for bname in names :
138                        billingcode = StorageBillingCode(self.storage, bname)
139                        self.modifyBillingCode(billingcode, reset, description)
140                        oldbillingcode = self.storage.addBillingCode(billingcode)
141                        if oldbillingcode is not None :
142                            if skipexisting :
143                                self.logdebug(_("Billing code [%s] already exists, skipping.") % bname)
144                            else :   
145                                self.logdebug(_("Billing code [%s] already exists, will be modified.") % bname)
146                                self.modifyBillingCode(oldbillingcode, reset, description)
147                                oldbillingcode.save()
148                        percent.oneMore()
149                else :       
150                    percent.display("\n%s...\n" % _("Modification"))
151                    for billingcode in billingcodes :
152                        self.modifyBillingCode(billingcode, reset, description)
153                        billingcode.save()   
154                        percent.oneMore()
155            except :                   
156                self.storage.rollbackTransaction()
157                raise
158            else :   
159                self.storage.commitTransaction()
160                       
161        if not options["list"] :
162            percent.done()
163                     
164if __name__ == "__main__" : 
165    retcode = 0
166    try :
167        short_options = "hvaD:dlrs"
168        long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"]
169       
170        # Initializes the command line tool
171        manager = PKBcodes(doc=__doc__)
172        manager.deferredInit()
173       
174        # parse and checks the command line
175        (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options)
176       
177        # sets long options
178        options["help"] = options["h"] or options["help"]
179        options["version"] = options["v"] or options["version"]
180        options["add"] = options["a"] or options["add"]
181        options["description"] = options["D"] or options["description"]
182        options["delete"] = options["d"] or options["delete"] 
183        options["list"] = options["l"] or options["list"]
184        options["reset"] = options["r"] or options["reset"]
185        options["skipexisting"] = options["s"] or options["skipexisting"]
186       
187        if options["help"] :
188            manager.display_usage_and_quit()
189        elif options["version"] :
190            manager.display_version_and_quit()
191        elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \
192           or (options["skipexisting"] and not options["add"]) \
193           or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) :
194            raise PyKotaCommandLineError, _("incompatible options, see help.")
195        elif (not args) and (options["add"] or options["delete"]) :   
196            raise PyKotaCommandLineError, _("You have to pass billing codes on the command line")
197        else :
198            retcode = manager.main(args, options)
199    except KeyboardInterrupt :       
200        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
201        retcode = -3
202    except PyKotaCommandLineError, msg :   
203        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
204        retcode = -2
205    except SystemExit :       
206        pass
207    except :
208        try :
209            manager.crashed("pkbcodes failed")
210        except :   
211            crashed("pkbcodes failed")
212        retcode = -1
213
214    try :
215        manager.storage.close()
216    except (TypeError, NameError, AttributeError) :   
217        pass
218       
219    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.