[2332] | 1 | #! /usr/bin/env python |
---|
[3411] | 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. |
---|
[3413] | 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. |
---|
[3413] | 16 | # |
---|
[2332] | 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] | 26 | import os |
---|
| 27 | import sys |
---|
| 28 | import pwd |
---|
| 29 | |
---|
[3294] | 30 | import pykota.appinit |
---|
[3361] | 31 | from pykota.utils import run |
---|
| 32 | from pykota.commandline import PyKotaOptionParser |
---|
[3288] | 33 | from pykota.errors import PyKotaCommandLineError |
---|
[3434] | 34 | from pykota.tool import PyKotaTool |
---|
[2765] | 35 | from pykota.storage import StorageBillingCode |
---|
[3434] | 36 | from pykota.progressbar import Percent |
---|
[2332] | 37 | |
---|
[3413] | 38 | class PKBcodes(PyKotaTool) : |
---|
[2337] | 39 | """A class for a billing codes manager.""" |
---|
[2765] | 40 | def modifyBillingCode(self, billingcode, reset, description) : |
---|
| 41 | """Modifies a billing code.""" |
---|
| 42 | if reset : |
---|
[3413] | 43 | billingcode.reset() |
---|
[2765] | 44 | if description is not None : # NB : "" is allowed ! |
---|
| 45 | billingcode.setDescription(description) |
---|
[3413] | 46 | |
---|
[2332] | 47 | def main(self, names, options) : |
---|
[2337] | 48 | """Manage billing codes.""" |
---|
[3361] | 49 | islist = (options.action == "list") |
---|
| 50 | isadd = (options.action == "add") |
---|
| 51 | isdelete = (options.action == "delete") |
---|
[3413] | 52 | |
---|
[3417] | 53 | if not islist : |
---|
| 54 | self.adminOnly() |
---|
| 55 | |
---|
[3361] | 56 | if not names : |
---|
| 57 | if isdelete or isadd : |
---|
| 58 | raise PyKotaCommandLineError, _("You must specify billing codes on the command line.") |
---|
[3413] | 59 | names = [u"*"] |
---|
| 60 | |
---|
[3361] | 61 | if not islist : |
---|
[2783] | 62 | percent = Percent(self) |
---|
[3413] | 63 | |
---|
[3361] | 64 | if not isadd : |
---|
| 65 | if not islist : |
---|
[2789] | 66 | percent.display("%s..." % _("Extracting datas")) |
---|
[3361] | 67 | if not names : |
---|
[2765] | 68 | names = ["*"] |
---|
| 69 | billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) |
---|
| 70 | if not billingcodes : |
---|
[3361] | 71 | if not islist : |
---|
[3052] | 72 | percent.display("\n") |
---|
[2765] | 73 | raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) |
---|
[3361] | 74 | if not islist : |
---|
[2783] | 75 | percent.setSize(len(billingcodes)) |
---|
[3413] | 76 | |
---|
[3361] | 77 | if islist : |
---|
[2768] | 78 | for billingcode in billingcodes : |
---|
[3361] | 79 | self.display("%s [%s] %s %s %s %.2f %s\n" % \ |
---|
[2768] | 80 | (billingcode.BillingCode, billingcode.Description, \ |
---|
| 81 | billingcode.PageCounter, \ |
---|
| 82 | _("pages"), \ |
---|
| 83 | _("and"), \ |
---|
| 84 | billingcode.Balance, \ |
---|
[3361] | 85 | _("credits"))) |
---|
[3413] | 86 | elif isdelete : |
---|
[2783] | 87 | percent.display("\n%s..." % _("Deletion")) |
---|
[2765] | 88 | self.storage.deleteManyBillingCodes(billingcodes) |
---|
[2782] | 89 | percent.display("\n") |
---|
[2768] | 90 | else : |
---|
[3361] | 91 | description = options.description |
---|
[2768] | 92 | if description : |
---|
[3361] | 93 | description = description.strip() |
---|
[3413] | 94 | |
---|
[2770] | 95 | self.storage.beginTransaction() |
---|
| 96 | try : |
---|
[3413] | 97 | if isadd : |
---|
[2783] | 98 | percent.display("%s...\n" % _("Creation")) |
---|
| 99 | percent.setSize(len(names)) |
---|
[2782] | 100 | for bname in names : |
---|
[2770] | 101 | billingcode = StorageBillingCode(self.storage, bname) |
---|
[3413] | 102 | self.modifyBillingCode(billingcode, |
---|
| 103 | options.reset, |
---|
[3361] | 104 | description) |
---|
[2770] | 105 | oldbillingcode = self.storage.addBillingCode(billingcode) |
---|
| 106 | if oldbillingcode is not None : |
---|
[3361] | 107 | if options.skipexisting : |
---|
| 108 | self.logdebug(_("Billing code '%(bname)s' already exists, skipping.") % locals()) |
---|
[3413] | 109 | else : |
---|
[3361] | 110 | self.logdebug(_("Billing code '%(bname)s' already exists, will be modified.") % locals()) |
---|
[3413] | 111 | self.modifyBillingCode(oldbillingcode, |
---|
| 112 | options.reset, |
---|
[3361] | 113 | description) |
---|
[2770] | 114 | oldbillingcode.save() |
---|
[2782] | 115 | percent.oneMore() |
---|
[3413] | 116 | else : |
---|
[2783] | 117 | percent.display("\n%s...\n" % _("Modification")) |
---|
[2782] | 118 | for billingcode in billingcodes : |
---|
[3413] | 119 | self.modifyBillingCode(billingcode, |
---|
| 120 | options.reset, |
---|
[3361] | 121 | description) |
---|
[3413] | 122 | billingcode.save() |
---|
[2782] | 123 | percent.oneMore() |
---|
[3413] | 124 | except : |
---|
[2770] | 125 | self.storage.rollbackTransaction() |
---|
| 126 | raise |
---|
[3413] | 127 | else : |
---|
[2770] | 128 | self.storage.commitTransaction() |
---|
[3413] | 129 | |
---|
[3361] | 130 | if not islist : |
---|
[2782] | 131 | percent.done() |
---|
[3413] | 132 | |
---|
| 133 | if __name__ == "__main__" : |
---|
[3361] | 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", |
---|
[3362] | 153 | help=_("Display detailed informations about the specified billing codes.")) |
---|
[3361] | 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.")) |
---|
[3413] | 162 | |
---|
[3361] | 163 | parser.add_example('-D "Financial Department" financial', |
---|
| 164 | _("Would create a billing code labelled 'financial' with the specified textual description.")) |
---|
[3413] | 165 | parser.add_example('--delete "fin*"', |
---|
[3361] | 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.")) |
---|
[3413] | 169 | |
---|
[3361] | 170 | (options, arguments) = parser.parse_args() |
---|
[3413] | 171 | run(parser, PKBcodes) |
---|