[2332] | 1 | #! /usr/bin/env python |
---|
[3260] | 2 | # -*- coding: UTF-8 -*- |
---|
[2332] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[2332] | 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 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 | |
---|
| 24 | import os |
---|
| 25 | import sys |
---|
| 26 | import pwd |
---|
| 27 | |
---|
[2829] | 28 | from pykota.tool import Percent, PyKotaTool, PyKotaCommandLineError, crashed, N_ |
---|
[2765] | 29 | from pykota.storage import StorageBillingCode |
---|
[2332] | 30 | |
---|
[2344] | 31 | __doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2332] | 32 | |
---|
| 33 | A billing codes Manager for PyKota. |
---|
| 34 | |
---|
| 35 | command line usage : |
---|
| 36 | |
---|
| 37 | pkbcodes [options] code1 code2 code3 ... codeN |
---|
| 38 | |
---|
| 39 | options : |
---|
| 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. |
---|
[2340] | 49 | NB : the history entries with this billing code |
---|
| 50 | are not deleted, voluntarily. |
---|
[2332] | 51 | |
---|
| 52 | -D | --description d Adds a textual description to billing codes. |
---|
| 53 | |
---|
| 54 | -l | --list List informations about the billing codes. |
---|
| 55 | |
---|
[2554] | 56 | -r | --reset Resets the billing codes' balance and page counters |
---|
[2332] | 57 | to 0. |
---|
| 58 | |
---|
| 59 | -s | --skipexisting In combination with the --add option above, tells |
---|
[2337] | 60 | pkbcodes to not modify existing billing codes. |
---|
[2332] | 61 | |
---|
| 62 | code1 through codeN can contain wildcards if the --add option |
---|
| 63 | is not set. |
---|
| 64 | |
---|
| 65 | examples : |
---|
| 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 | |
---|
[2344] | 79 | This will list all billing codes which name begins with 'my'. |
---|
| 80 | """) |
---|
[2332] | 81 | |
---|
| 82 | class PKBcodes(PyKotaTool) : |
---|
[2337] | 83 | """A class for a billing codes manager.""" |
---|
[2765] | 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 | |
---|
[2332] | 91 | def main(self, names, options) : |
---|
[2337] | 92 | """Manage billing codes.""" |
---|
[2332] | 93 | if (not self.config.isAdmin) and (not options["list"]) : |
---|
[2512] | 94 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
[2332] | 95 | |
---|
[2783] | 96 | if not options["list"] : |
---|
| 97 | percent = Percent(self) |
---|
| 98 | |
---|
[2765] | 99 | if not options["add"] : |
---|
| 100 | if not options["list"] : |
---|
[2789] | 101 | percent.display("%s..." % _("Extracting datas")) |
---|
[2765] | 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 : |
---|
[3052] | 106 | if not options["list"] : |
---|
| 107 | percent.display("\n") |
---|
[2765] | 108 | raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) |
---|
[2783] | 109 | if not options["list"] : |
---|
| 110 | percent.setSize(len(billingcodes)) |
---|
[2765] | 111 | |
---|
[2768] | 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"] : |
---|
[2783] | 122 | percent.display("\n%s..." % _("Deletion")) |
---|
[2765] | 123 | self.storage.deleteManyBillingCodes(billingcodes) |
---|
[2782] | 124 | percent.display("\n") |
---|
[2768] | 125 | else : |
---|
| 126 | reset = options["reset"] |
---|
| 127 | description = options["description"] |
---|
| 128 | if description : |
---|
| 129 | description = options["description"].strip() |
---|
| 130 | skipexisting = options["skipexisting"] |
---|
[2770] | 131 | |
---|
| 132 | self.storage.beginTransaction() |
---|
| 133 | try : |
---|
| 134 | if options["add"] : |
---|
[2783] | 135 | percent.display("%s...\n" % _("Creation")) |
---|
| 136 | percent.setSize(len(names)) |
---|
[2782] | 137 | for bname in names : |
---|
[2770] | 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() |
---|
[2782] | 148 | percent.oneMore() |
---|
[2770] | 149 | else : |
---|
[2783] | 150 | percent.display("\n%s...\n" % _("Modification")) |
---|
[2782] | 151 | for billingcode in billingcodes : |
---|
[2770] | 152 | self.modifyBillingCode(billingcode, reset, description) |
---|
| 153 | billingcode.save() |
---|
[2782] | 154 | percent.oneMore() |
---|
[2770] | 155 | except : |
---|
| 156 | self.storage.rollbackTransaction() |
---|
| 157 | raise |
---|
| 158 | else : |
---|
| 159 | self.storage.commitTransaction() |
---|
[2768] | 160 | |
---|
[2783] | 161 | if not options["list"] : |
---|
[2782] | 162 | percent.done() |
---|
[2332] | 163 | |
---|
| 164 | if __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"])) : |
---|
[2512] | 194 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
[2332] | 195 | elif (not args) and (options["add"] or options["delete"]) : |
---|
[2512] | 196 | raise PyKotaCommandLineError, _("You have to pass billing codes on the command line") |
---|
[2332] | 197 | else : |
---|
| 198 | retcode = manager.main(args, options) |
---|
| 199 | except KeyboardInterrupt : |
---|
| 200 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 201 | retcode = -3 |
---|
[2512] | 202 | except PyKotaCommandLineError, msg : |
---|
| 203 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 204 | retcode = -2 |
---|
[2332] | 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) |
---|