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