[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 | # |
---|
| 8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
| 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 | |
---|
| 31 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
| 32 | |
---|
| 33 | __doc__ = N_("""pkbcodes v%s (c) %s %s |
---|
| 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 | |
---|
| 58 | -s | --reset Resets the billing codes' balance and page counters |
---|
| 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 | |
---|
| 81 | This will list all billing codes which name begins with "my". |
---|
| 82 | |
---|
| 83 | This program is free software; you can redistribute it and/or modify |
---|
| 84 | it under the terms of the GNU General Public License as published by |
---|
| 85 | the Free Software Foundation; either version 2 of the License, or |
---|
| 86 | (at your option) any later version. |
---|
| 87 | |
---|
| 88 | This program is distributed in the hope that it will be useful, |
---|
| 89 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 90 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 91 | GNU General Public License for more details. |
---|
| 92 | |
---|
| 93 | You should have received a copy of the GNU General Public License |
---|
| 94 | along with this program; if not, write to the Free Software |
---|
| 95 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 96 | |
---|
| 97 | Please e-mail bugs to: %s""") |
---|
| 98 | |
---|
| 99 | class PKBcodes(PyKotaTool) : |
---|
[2337] | 100 | """A class for a billing codes manager.""" |
---|
[2332] | 101 | def main(self, names, options) : |
---|
[2337] | 102 | """Manage billing codes.""" |
---|
[2332] | 103 | if (not self.config.isAdmin) and (not options["list"]) : |
---|
| 104 | raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
| 105 | |
---|
[2342] | 106 | if (options["list"] or options["reset"]) and not names : |
---|
[2332] | 107 | names = ["*"] |
---|
| 108 | |
---|
| 109 | if options["add"] : |
---|
[2337] | 110 | billingcodes = [] |
---|
| 111 | for bname in names : |
---|
| 112 | billingcode = self.storage.getBillingCode(bname) |
---|
| 113 | if billingcode.Exists : |
---|
[2332] | 114 | if options["skipexisting"] : |
---|
[2342] | 115 | self.printInfo(_("Billing code [%s] already exists, skipping.") % billingcode.BillingCode) |
---|
[2332] | 116 | else : |
---|
[2342] | 117 | self.printInfo(_("Billing code [%s] already exists, will be modified.") % billingcode.BillingCode) |
---|
[2337] | 118 | billingcodes.append(billingcode) |
---|
[2332] | 119 | else : |
---|
[2337] | 120 | billingcode = self.storage.addBillingCode(bname) |
---|
| 121 | if not billingcode.Exists : |
---|
| 122 | raise PyKotaToolError, _("Impossible to add billingcode %s") % bname |
---|
| 123 | else : |
---|
| 124 | billingcodes.append(billingcode) |
---|
[2332] | 125 | else : |
---|
[2337] | 126 | billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) |
---|
| 127 | if not billingcodes : |
---|
| 128 | raise PyKotaToolError, _("There's no billingcode matching %s") % " ".join(names) |
---|
[2332] | 129 | |
---|
[2337] | 130 | for billingcode in billingcodes : |
---|
[2332] | 131 | if options["delete"] : |
---|
[2337] | 132 | billingcode.delete() |
---|
[2332] | 133 | elif options["list"] : |
---|
[2337] | 134 | print "%s [%s] %s %s %s %.2f %s" % \ |
---|
[2342] | 135 | (billingcode.BillingCode, billingcode.Description, \ |
---|
[2337] | 136 | billingcode.PageCounter, \ |
---|
| 137 | _("pages"), \ |
---|
| 138 | _("and"), \ |
---|
| 139 | billingcode.Balance, \ |
---|
| 140 | _("credits")) |
---|
[2332] | 141 | else : |
---|
[2337] | 142 | if options["reset"] : |
---|
| 143 | billingcode.reset() |
---|
[2332] | 144 | if options["description"] is not None : |
---|
[2337] | 145 | billingcode.setDescription(options["description"].strip()) |
---|
[2332] | 146 | |
---|
| 147 | if __name__ == "__main__" : |
---|
| 148 | retcode = 0 |
---|
| 149 | try : |
---|
| 150 | short_options = "hvaD:dlrs" |
---|
| 151 | long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"] |
---|
| 152 | |
---|
| 153 | # Initializes the command line tool |
---|
| 154 | manager = PKBcodes(doc=__doc__) |
---|
| 155 | manager.deferredInit() |
---|
| 156 | |
---|
| 157 | # parse and checks the command line |
---|
| 158 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 159 | |
---|
| 160 | # sets long options |
---|
| 161 | options["help"] = options["h"] or options["help"] |
---|
| 162 | options["version"] = options["v"] or options["version"] |
---|
| 163 | options["add"] = options["a"] or options["add"] |
---|
| 164 | options["description"] = options["D"] or options["description"] |
---|
| 165 | options["delete"] = options["d"] or options["delete"] |
---|
| 166 | options["list"] = options["l"] or options["list"] |
---|
| 167 | options["reset"] = options["r"] or options["reset"] |
---|
| 168 | options["skipexisting"] = options["s"] or options["skipexisting"] |
---|
| 169 | |
---|
| 170 | if options["help"] : |
---|
| 171 | manager.display_usage_and_quit() |
---|
| 172 | elif options["version"] : |
---|
| 173 | manager.display_version_and_quit() |
---|
| 174 | elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \ |
---|
| 175 | or (options["skipexisting"] and not options["add"]) \ |
---|
| 176 | or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) : |
---|
| 177 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
| 178 | elif (not args) and (options["add"] or options["delete"]) : |
---|
| 179 | raise PyKotaToolError, _("You have to pass billing codes on the command line") |
---|
| 180 | else : |
---|
| 181 | retcode = manager.main(args, options) |
---|
| 182 | except KeyboardInterrupt : |
---|
| 183 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
| 184 | except SystemExit : |
---|
| 185 | pass |
---|
| 186 | except : |
---|
| 187 | try : |
---|
| 188 | manager.crashed("pkbcodes failed") |
---|
| 189 | except : |
---|
| 190 | crashed("pkbcodes failed") |
---|
| 191 | retcode = -1 |
---|
| 192 | |
---|
| 193 | try : |
---|
| 194 | manager.storage.close() |
---|
| 195 | except (TypeError, NameError, AttributeError) : |
---|
| 196 | pass |
---|
| 197 | |
---|
| 198 | sys.exit(retcode) |
---|