root / pykota / trunk / bin / pkbcodes @ 3361

Revision 3361, 7.7 kB (checked in by jerome, 16 years ago)

pkbcodes converted to new style.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2332]1#! /usr/bin/env python
[3260]2# -*- coding: UTF-8 -*-
[2332]3#
[3260]4# PyKota : Print Quotas for CUPS
[2332]5#
[3275]6# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
[3260]7# This program is free software: you can redistribute it and/or modify
[2332]8# it under the terms of the GNU General Public License as published by
[3260]9# the Free Software Foundation, either version 3 of the License, or
[2332]10# (at your option) any later version.
[3260]11#
[2332]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
[3260]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[2332]19#
20# $Id$
21#
22#
23
[3361]24"""A billing codes manager for PyKota."""
25
[2332]26import os
27import sys
28import pwd
29
[3294]30import pykota.appinit
[3361]31from pykota.utils import run
32from pykota.commandline import PyKotaOptionParser
[3288]33from pykota.errors import PyKotaCommandLineError
[3295]34from pykota.tool import Percent, PyKotaTool
[2765]35from pykota.storage import StorageBillingCode
[2332]36
37class PKBcodes(PyKotaTool) :       
[2337]38    """A class for a billing codes manager."""
[2765]39    def modifyBillingCode(self, billingcode, reset, description) :
40        """Modifies a billing code."""
41        if reset :
42            billingcode.reset()   
43        if description is not None : # NB : "" is allowed !
44            billingcode.setDescription(description)
45       
[2332]46    def main(self, names, options) :
[2337]47        """Manage billing codes."""
[3361]48        if (not self.config.isAdmin) and (options.action != "list") :
[2512]49            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
[3361]50           
51        islist = (options.action == "list")
52        isadd = (options.action == "add")
53        isdelete = (options.action == "delete")
54       
55        if not names :
56            if isdelete or isadd :
57                raise PyKotaCommandLineError, _("You must specify billing codes on the command line.")
58            names = [u"*"]   
[2332]59           
[3361]60        if not islist :
[2783]61            percent = Percent(self)
62           
[3361]63        if not isadd :
64            if not islist :
[2789]65                percent.display("%s..." % _("Extracting datas"))
[3361]66            if not names :
[2765]67                names = ["*"]
68            billingcodes = self.storage.getMatchingBillingCodes(",".join(names))
69            if not billingcodes :
[3361]70                if not islist :
[3052]71                    percent.display("\n")
[2765]72                raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names)
[3361]73            if not islist :
[2783]74                percent.setSize(len(billingcodes))
[2765]75                       
[3361]76        if islist :
[2768]77            for billingcode in billingcodes :
[3361]78                self.display("%s [%s] %s %s %s %.2f %s\n" % \
[2768]79                      (billingcode.BillingCode, billingcode.Description, \
80                       billingcode.PageCounter, \
81                       _("pages"), \
82                       _("and"), \
83                       billingcode.Balance, \
[3361]84                       _("credits")))
85        elif isdelete :   
[2783]86            percent.display("\n%s..." % _("Deletion"))
[2765]87            self.storage.deleteManyBillingCodes(billingcodes)
[2782]88            percent.display("\n")
[2768]89        else :
[3361]90            description = options.description
[2768]91            if description :
[3361]92                description = description.strip()
[2770]93           
94            self.storage.beginTransaction()
95            try :
[3361]96                if isadd :   
[2783]97                    percent.display("%s...\n" % _("Creation"))
98                    percent.setSize(len(names))
[2782]99                    for bname in names :
[2770]100                        billingcode = StorageBillingCode(self.storage, bname)
[3361]101                        self.modifyBillingCode(billingcode, 
102                                               options.reset, 
103                                               description)
[2770]104                        oldbillingcode = self.storage.addBillingCode(billingcode)
105                        if oldbillingcode is not None :
[3361]106                            if options.skipexisting :
107                                self.logdebug(_("Billing code '%(bname)s' already exists, skipping.") % locals())
[2770]108                            else :   
[3361]109                                self.logdebug(_("Billing code '%(bname)s' already exists, will be modified.") % locals())
110                                self.modifyBillingCode(oldbillingcode, 
111                                                       options.reset, 
112                                                       description)
[2770]113                                oldbillingcode.save()
[2782]114                        percent.oneMore()
[2770]115                else :       
[2783]116                    percent.display("\n%s...\n" % _("Modification"))
[2782]117                    for billingcode in billingcodes :
[3361]118                        self.modifyBillingCode(billingcode, 
119                                               options.reset, 
120                                               description)
[2770]121                        billingcode.save()   
[2782]122                        percent.oneMore()
[2770]123            except :                   
124                self.storage.rollbackTransaction()
125                raise
126            else :   
127                self.storage.commitTransaction()
[2768]128                       
[3361]129        if not islist :
[2782]130            percent.done()
[2332]131                     
132if __name__ == "__main__" : 
[3361]133    parser = PyKotaOptionParser(description=_("A billing codes manager for PyKota."),
134                                usage="pkbcodes [options] code1 code2 ... codeN")
135    parser.add_option("-a", "--add",
136                            action="store_const",
137                            const="add",
138                            dest="action",
139                            help=_("Add new, or modify existing, billing codes."))
140    parser.add_option("-d", "--delete",
141                            action="store_const",
142                            const="delete",
143                            dest="action",
144                            help=_("Deletes billing codes. Matching entries in the printing history are not deleted, on purpose."))
145    parser.add_option("-D", "--description",
146                            dest="description",
147                            help=_("Set a textual description for the specified billing codes."))
148    parser.add_option("-l", "--list",
149                            action="store_const",
150                            const="list",
151                            dest="action",
152                            help=_("Display information about the specified billing codes."))
153    parser.add_option("-r", "--reset",
154                            action="store_true",
155                            dest="reset",
156                            help=_("Reset the page count and amount spent for the specified billing codes."))
157    parser.add_option("-s", "--skipexisting",
158                            action="store_true",
159                            dest="skipexisting",
160                            help=_("If --add is used, ensure that existing billing codes won't be modified."))
161                           
162    parser.add_example('-D "Financial Department" financial',
163                       _("Would create a billing code labelled 'financial' with the specified textual description."))
164    parser.add_example('--delete "fin*"', 
165                       _("Would delete all billing codes which label begins with 'fin'. Matching jobs in the printing history wouldn't be deleted though."))
166    parser.add_example("--list",
167                       _("Would display details about all existing billing codes."))
168                       
169    (options, arguments) = parser.parse_args()
170    run(parser, PKBcodes)                   
Note: See TracBrowser for help on using the browser.