root / pykota / trunk / bin / pkbcodes @ 3489

Revision 3489, 7.3 kB (checked in by jerome, 15 years ago)

Removed bad copy and paste artifact.

  • 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-2009 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
24"""A billing codes manager for PyKota."""
25
26import os
27import sys
28import pwd
29
30import pykota.appinit
31from pykota.utils import run
32from pykota.commandline import PyKotaOptionParser
33from pykota.errors import PyKotaCommandLineError
34from pykota.tool import PyKotaTool
35from pykota.storage import StorageBillingCode
36from pykota.progressbar import Percent
37
38class PKBcodes(PyKotaTool) :
39    """A class for a billing codes manager."""
40    def modifyBillingCode(self, billingcode, reset, description) :
41        """Modifies a billing code."""
42        if reset :
43            billingcode.reset()
44        if description is not None : # NB : "" is allowed !
45            billingcode.setDescription(description)
46
47    def main(self, names, options) :
48        """Manage billing codes."""
49        islist = (options.action == "list")
50        isadd = (options.action == "add")
51        isdelete = (options.action == "delete")
52
53        if not islist :
54            self.adminOnly()
55
56        if not names :
57            if isdelete or isadd :
58                raise PyKotaCommandLineError, _("You must specify billing codes on the command line.")
59            names = [u"*"]
60
61        if not islist :
62            percent = Percent(self)
63
64        if not isadd :
65            if not islist :
66                percent.display("%s..." % _("Extracting datas"))
67            if not names :
68                names = ["*"]
69            billingcodes = self.storage.getMatchingBillingCodes(",".join(names))
70            if not billingcodes :
71                if not islist :
72                    percent.display("\n")
73                raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names)
74            if not islist :
75                percent.setSize(len(billingcodes))
76
77        if islist :
78            for billingcode in billingcodes :
79                self.display("%s [%s] %s %s %s %.2f %s\n" % \
80                      (billingcode.BillingCode, billingcode.Description, \
81                       billingcode.PageCounter, \
82                       _("pages"), \
83                       _("and"), \
84                       billingcode.Balance, \
85                       _("credits")))
86        elif isdelete :
87            percent.display("\n%s..." % _("Deletion"))
88            self.storage.deleteManyBillingCodes(billingcodes)
89            percent.display("\n")
90        else :
91            description = options.description
92            if description :
93                description = description.strip()
94
95            self.storage.beginTransaction()
96            try :
97                if isadd :
98                    percent.display("%s...\n" % _("Creation"))
99                    percent.setSize(len(names))
100                    for bname in names :
101                        billingcode = StorageBillingCode(self.storage, bname)
102                        self.modifyBillingCode(billingcode,
103                                               options.reset,
104                                               description)
105                        oldbillingcode = self.storage.addBillingCode(billingcode)
106                        if oldbillingcode is not None :
107                            if options.skipexisting :
108                                self.logdebug(_("Billing code '%(bname)s' already exists, skipping.") % locals())
109                            else :
110                                self.logdebug(_("Billing code '%(bname)s' already exists, will be modified.") % locals())
111                                self.modifyBillingCode(oldbillingcode,
112                                                       options.reset,
113                                                       description)
114                                oldbillingcode.save()
115                        percent.oneMore()
116                else :
117                    percent.display("\n%s...\n" % _("Modification"))
118                    for billingcode in billingcodes :
119                        self.modifyBillingCode(billingcode,
120                                               options.reset,
121                                               description)
122                        billingcode.save()
123                        percent.oneMore()
124            except :
125                self.storage.rollbackTransaction()
126                raise
127            else :
128                self.storage.commitTransaction()
129
130        if not islist :
131            percent.done()
132
133if __name__ == "__main__" :
134    parser = PyKotaOptionParser(description=_("A billing codes manager for PyKota."),
135                                usage="pkbcodes [options] code1 code2 ... codeN")
136    parser.add_option("-a", "--add",
137                            action="store_const",
138                            const="add",
139                            dest="action",
140                            help=_("Add new, or modify existing, billing codes."))
141    parser.add_option("-d", "--delete",
142                            action="store_const",
143                            const="delete",
144                            dest="action",
145                            help=_("Deletes billing codes. Matching entries in the printing history are not deleted, on purpose."))
146    parser.add_option("-D", "--description",
147                            dest="description",
148                            help=_("Set a textual description for the specified billing codes."))
149    parser.add_option("-l", "--list",
150                            action="store_const",
151                            const="list",
152                            dest="action",
153                            help=_("Display detailed informations about the specified billing codes."))
154    parser.add_option("-r", "--reset",
155                            action="store_true",
156                            dest="reset",
157                            help=_("Reset the page count and amount spent for the specified billing codes."))
158    parser.add_option("-s", "--skipexisting",
159                            action="store_true",
160                            dest="skipexisting",
161                            help=_("If --add is used, ensure that existing billing codes won't be modified."))
162
163    parser.add_example('-D "Financial Department" financial',
164                       _("Would create a billing code labelled 'financial' with the specified textual description."))
165    parser.add_example('--delete "fin*"',
166                       _("Would delete all billing codes which label begins with 'fin'. Matching jobs in the printing history wouldn't be deleted though."))
167    parser.add_example("--list",
168                       _("Would display details about all existing billing codes."))
169
170    (options, arguments) = parser.parse_args()
171    run(parser, PKBcodes)
Note: See TracBrowser for help on using the browser.