[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_ |
---|
[2332] | 32 | |
---|
[2344] | 33 | __doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2332] | 34 | |
---|
| 35 | A billing codes Manager for PyKota. |
---|
| 36 | |
---|
| 37 | command line usage : |
---|
| 38 | |
---|
| 39 | pkbcodes [options] code1 code2 code3 ... codeN |
---|
| 40 | |
---|
| 41 | options : |
---|
| 42 | |
---|
| 43 | -v | --version Prints pkbcodes version number then exits. |
---|
| 44 | -h | --help Prints this message then exits. |
---|
| 45 | |
---|
| 46 | -a | --add Adds billing codes if they don't exist in PyKota's |
---|
| 47 | database. If they exist, they are modified |
---|
| 48 | unless -s|--skipexisting is also used. |
---|
| 49 | |
---|
| 50 | -d | --delete Deletes billing codes from PyKota's database. |
---|
[2340] | 51 | NB : the history entries with this billing code |
---|
| 52 | are not deleted, voluntarily. |
---|
[2332] | 53 | |
---|
| 54 | -D | --description d Adds a textual description to billing codes. |
---|
| 55 | |
---|
| 56 | -l | --list List informations about the billing codes. |
---|
| 57 | |
---|
[2554] | 58 | -r | --reset Resets the billing codes' balance and page counters |
---|
[2332] | 59 | to 0. |
---|
| 60 | |
---|
| 61 | -s | --skipexisting In combination with the --add option above, tells |
---|
[2337] | 62 | pkbcodes to not modify existing billing codes. |
---|
[2332] | 63 | |
---|
| 64 | code1 through codeN can contain wildcards if the --add option |
---|
| 65 | is not set. |
---|
| 66 | |
---|
| 67 | examples : |
---|
| 68 | |
---|
| 69 | $ pkbcodes --add -D "My project" myproj |
---|
| 70 | |
---|
| 71 | Will create the myproj billing code with "My project" |
---|
| 72 | as the description. |
---|
| 73 | |
---|
| 74 | $ pkbcodes --delete "*" |
---|
| 75 | |
---|
| 76 | This will completely delete all the billing codes, but without |
---|
| 77 | removing any matching job from the history. USE WITH CARE ANYWAY ! |
---|
| 78 | |
---|
| 79 | $ pkbcodes --list "my*" |
---|
| 80 | |
---|
[2344] | 81 | This will list all billing codes which name begins with 'my'. |
---|
| 82 | """) |
---|
[2332] | 83 | |
---|
| 84 | class PKBcodes(PyKotaTool) : |
---|
[2337] | 85 | """A class for a billing codes manager.""" |
---|
[2332] | 86 | def main(self, names, options) : |
---|
[2337] | 87 | """Manage billing codes.""" |
---|
[2332] | 88 | if (not self.config.isAdmin) and (not options["list"]) : |
---|
[2512] | 89 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
[2332] | 90 | |
---|
[2657] | 91 | if options["delete"] : |
---|
[2671] | 92 | self.display("%s...\n" % _("Deletion")) |
---|
[2657] | 93 | todelete = self.storage.getMatchingBillingCodes(",".join(names)) |
---|
| 94 | nbtotal = len(todelete) |
---|
| 95 | for i in range(nbtotal) : |
---|
| 96 | todelete[i].delete() |
---|
| 97 | percent = 100.0 * float(i) / float(nbtotal) |
---|
| 98 | self.display("\r%.02f%%" % percent) |
---|
| 99 | else : |
---|
| 100 | if options["add"] : |
---|
[2671] | 101 | self.display("%s...\n" % _("Creation")) |
---|
[2657] | 102 | billingcodes = [] |
---|
[2671] | 103 | nbtotal = len(names) |
---|
| 104 | for i in range(nbtotal) : |
---|
| 105 | bname = names[i] |
---|
[2657] | 106 | billingcode = self.storage.getBillingCode(bname) |
---|
| 107 | if billingcode.Exists : |
---|
| 108 | if options["skipexisting"] : |
---|
| 109 | self.printInfo(_("Billing code [%s] already exists, skipping.") % billingcode.BillingCode) |
---|
| 110 | else : |
---|
| 111 | self.printInfo(_("Billing code [%s] already exists, will be modified.") % billingcode.BillingCode) |
---|
| 112 | billingcodes.append(billingcode) |
---|
| 113 | else : |
---|
| 114 | billingcode = self.storage.addBillingCode(bname) |
---|
| 115 | if not billingcode.Exists : |
---|
| 116 | raise PyKotaToolError, _("Impossible to add billingcode %s") % bname |
---|
| 117 | else : |
---|
| 118 | billingcodes.append(billingcode) |
---|
[2671] | 119 | percent = 100.0 * float(i) / float(nbtotal) |
---|
| 120 | self.display("\r%.02f%%" % percent) |
---|
| 121 | self.display("\r100.00%%\r \r%s\n" % _("Done.")) |
---|
[2657] | 122 | else : |
---|
[2671] | 123 | if not names : |
---|
| 124 | names = ["*"] |
---|
[2657] | 125 | billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) |
---|
| 126 | if not billingcodes : |
---|
| 127 | raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) |
---|
| 128 | |
---|
[2671] | 129 | nbtotal = len(billingcodes) |
---|
| 130 | if not options["list"] : |
---|
[2672] | 131 | self.display("%s...\n" % _("Modification")) |
---|
[2671] | 132 | for i in range(nbtotal) : |
---|
| 133 | billingcode = billingcodes[i] |
---|
[2657] | 134 | if options["list"] : |
---|
| 135 | print "%s [%s] %s %s %s %.2f %s" % \ |
---|
| 136 | (billingcode.BillingCode, billingcode.Description, \ |
---|
| 137 | billingcode.PageCounter, \ |
---|
| 138 | _("pages"), \ |
---|
| 139 | _("and"), \ |
---|
| 140 | billingcode.Balance, \ |
---|
| 141 | _("credits")) |
---|
| 142 | else : |
---|
| 143 | if options["reset"] : |
---|
| 144 | billingcode.reset() |
---|
| 145 | if options["description"] is not None : |
---|
| 146 | billingcode.setDescription(options["description"].strip()) |
---|
[2671] | 147 | percent = 100.0 * float(i) / float(nbtotal) |
---|
| 148 | self.display("\r%.02f%%" % percent) |
---|
| 149 | |
---|
| 150 | if not options["list"] : |
---|
| 151 | self.display("\r100.00%%\r \r%s\n" % _("Done.")) |
---|
[2332] | 152 | |
---|
| 153 | if __name__ == "__main__" : |
---|
| 154 | retcode = 0 |
---|
| 155 | try : |
---|
| 156 | short_options = "hvaD:dlrs" |
---|
| 157 | long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"] |
---|
| 158 | |
---|
| 159 | # Initializes the command line tool |
---|
| 160 | manager = PKBcodes(doc=__doc__) |
---|
| 161 | manager.deferredInit() |
---|
| 162 | |
---|
| 163 | # parse and checks the command line |
---|
| 164 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 165 | |
---|
| 166 | # sets long options |
---|
| 167 | options["help"] = options["h"] or options["help"] |
---|
| 168 | options["version"] = options["v"] or options["version"] |
---|
| 169 | options["add"] = options["a"] or options["add"] |
---|
| 170 | options["description"] = options["D"] or options["description"] |
---|
| 171 | options["delete"] = options["d"] or options["delete"] |
---|
| 172 | options["list"] = options["l"] or options["list"] |
---|
| 173 | options["reset"] = options["r"] or options["reset"] |
---|
| 174 | options["skipexisting"] = options["s"] or options["skipexisting"] |
---|
| 175 | |
---|
| 176 | if options["help"] : |
---|
| 177 | manager.display_usage_and_quit() |
---|
| 178 | elif options["version"] : |
---|
| 179 | manager.display_version_and_quit() |
---|
| 180 | elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \ |
---|
| 181 | or (options["skipexisting"] and not options["add"]) \ |
---|
| 182 | or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) : |
---|
[2512] | 183 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
[2332] | 184 | elif (not args) and (options["add"] or options["delete"]) : |
---|
[2512] | 185 | raise PyKotaCommandLineError, _("You have to pass billing codes on the command line") |
---|
[2332] | 186 | else : |
---|
| 187 | retcode = manager.main(args, options) |
---|
| 188 | except KeyboardInterrupt : |
---|
| 189 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 190 | retcode = -3 |
---|
[2512] | 191 | except PyKotaCommandLineError, msg : |
---|
| 192 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 193 | retcode = -2 |
---|
[2332] | 194 | except SystemExit : |
---|
| 195 | pass |
---|
| 196 | except : |
---|
| 197 | try : |
---|
| 198 | manager.crashed("pkbcodes failed") |
---|
| 199 | except : |
---|
| 200 | crashed("pkbcodes failed") |
---|
| 201 | retcode = -1 |
---|
| 202 | |
---|
| 203 | try : |
---|
| 204 | manager.storage.close() |
---|
| 205 | except (TypeError, NameError, AttributeError) : |
---|
| 206 | pass |
---|
| 207 | |
---|
| 208 | sys.exit(retcode) |
---|