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, 2006 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 Percent, PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
32 | from pykota.storage import StorageBillingCode |
---|
33 | |
---|
34 | __doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
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. |
---|
52 | NB : the history entries with this billing code |
---|
53 | are not deleted, voluntarily. |
---|
54 | |
---|
55 | -D | --description d Adds a textual description to billing codes. |
---|
56 | |
---|
57 | -l | --list List informations about the billing codes. |
---|
58 | |
---|
59 | -r | --reset Resets the billing codes' balance and page counters |
---|
60 | to 0. |
---|
61 | |
---|
62 | -s | --skipexisting In combination with the --add option above, tells |
---|
63 | pkbcodes to not modify existing billing codes. |
---|
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 | |
---|
82 | This will list all billing codes which name begins with 'my'. |
---|
83 | """) |
---|
84 | |
---|
85 | class PKBcodes(PyKotaTool) : |
---|
86 | """A class for a billing codes manager.""" |
---|
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 | |
---|
94 | def main(self, names, options) : |
---|
95 | """Manage billing codes.""" |
---|
96 | if (not self.config.isAdmin) and (not options["list"]) : |
---|
97 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
98 | |
---|
99 | if not options["list"] : |
---|
100 | percent = Percent(self) |
---|
101 | |
---|
102 | if not options["add"] : |
---|
103 | if not options["list"] : |
---|
104 | percent.display("%s..." % _("Extracting datas")) |
---|
105 | if not names : # NB : can't happen for --delete because it's catched earlier |
---|
106 | names = ["*"] |
---|
107 | billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) |
---|
108 | if not billingcodes : |
---|
109 | raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) |
---|
110 | if not options["list"] : |
---|
111 | percent.setSize(len(billingcodes)) |
---|
112 | |
---|
113 | if options["list"] : |
---|
114 | for billingcode in billingcodes : |
---|
115 | print "%s [%s] %s %s %s %.2f %s" % \ |
---|
116 | (billingcode.BillingCode, billingcode.Description, \ |
---|
117 | billingcode.PageCounter, \ |
---|
118 | _("pages"), \ |
---|
119 | _("and"), \ |
---|
120 | billingcode.Balance, \ |
---|
121 | _("credits")) |
---|
122 | elif options["delete"] : |
---|
123 | percent.display("\n%s..." % _("Deletion")) |
---|
124 | self.storage.deleteManyBillingCodes(billingcodes) |
---|
125 | percent.display("\n") |
---|
126 | else : |
---|
127 | reset = options["reset"] |
---|
128 | description = options["description"] |
---|
129 | if description : |
---|
130 | description = options["description"].strip() |
---|
131 | skipexisting = options["skipexisting"] |
---|
132 | |
---|
133 | self.storage.beginTransaction() |
---|
134 | try : |
---|
135 | if options["add"] : |
---|
136 | percent.display("%s...\n" % _("Creation")) |
---|
137 | percent.setSize(len(names)) |
---|
138 | for bname in names : |
---|
139 | billingcode = StorageBillingCode(self.storage, bname) |
---|
140 | self.modifyBillingCode(billingcode, reset, description) |
---|
141 | oldbillingcode = self.storage.addBillingCode(billingcode) |
---|
142 | if oldbillingcode is not None : |
---|
143 | if skipexisting : |
---|
144 | self.logdebug(_("Billing code [%s] already exists, skipping.") % bname) |
---|
145 | else : |
---|
146 | self.logdebug(_("Billing code [%s] already exists, will be modified.") % bname) |
---|
147 | self.modifyBillingCode(oldbillingcode, reset, description) |
---|
148 | oldbillingcode.save() |
---|
149 | percent.oneMore() |
---|
150 | else : |
---|
151 | percent.display("\n%s...\n" % _("Modification")) |
---|
152 | for billingcode in billingcodes : |
---|
153 | self.modifyBillingCode(billingcode, reset, description) |
---|
154 | billingcode.save() |
---|
155 | percent.oneMore() |
---|
156 | except : |
---|
157 | self.storage.rollbackTransaction() |
---|
158 | raise |
---|
159 | else : |
---|
160 | self.storage.commitTransaction() |
---|
161 | |
---|
162 | if not options["list"] : |
---|
163 | percent.done() |
---|
164 | |
---|
165 | if __name__ == "__main__" : |
---|
166 | retcode = 0 |
---|
167 | try : |
---|
168 | short_options = "hvaD:dlrs" |
---|
169 | long_options = ["help", "version", "add", "description=", "delete", "list", "reset", "skipexisting"] |
---|
170 | |
---|
171 | # Initializes the command line tool |
---|
172 | manager = PKBcodes(doc=__doc__) |
---|
173 | manager.deferredInit() |
---|
174 | |
---|
175 | # parse and checks the command line |
---|
176 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
177 | |
---|
178 | # sets long options |
---|
179 | options["help"] = options["h"] or options["help"] |
---|
180 | options["version"] = options["v"] or options["version"] |
---|
181 | options["add"] = options["a"] or options["add"] |
---|
182 | options["description"] = options["D"] or options["description"] |
---|
183 | options["delete"] = options["d"] or options["delete"] |
---|
184 | options["list"] = options["l"] or options["list"] |
---|
185 | options["reset"] = options["r"] or options["reset"] |
---|
186 | options["skipexisting"] = options["s"] or options["skipexisting"] |
---|
187 | |
---|
188 | if options["help"] : |
---|
189 | manager.display_usage_and_quit() |
---|
190 | elif options["version"] : |
---|
191 | manager.display_version_and_quit() |
---|
192 | elif (options["delete"] and (options["add"] or options["reset"] or options["description"])) \ |
---|
193 | or (options["skipexisting"] and not options["add"]) \ |
---|
194 | or (options["list"] and (options["add"] or options["delete"] or options["reset"] or options["description"])) : |
---|
195 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
196 | elif (not args) and (options["add"] or options["delete"]) : |
---|
197 | raise PyKotaCommandLineError, _("You have to pass billing codes on the command line") |
---|
198 | else : |
---|
199 | retcode = manager.main(args, options) |
---|
200 | except KeyboardInterrupt : |
---|
201 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
202 | retcode = -3 |
---|
203 | except PyKotaCommandLineError, msg : |
---|
204 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
205 | retcode = -2 |
---|
206 | except SystemExit : |
---|
207 | pass |
---|
208 | except : |
---|
209 | try : |
---|
210 | manager.crashed("pkbcodes failed") |
---|
211 | except : |
---|
212 | crashed("pkbcodes failed") |
---|
213 | retcode = -1 |
---|
214 | |
---|
215 | try : |
---|
216 | manager.storage.close() |
---|
217 | except (TypeError, NameError, AttributeError) : |
---|
218 | pass |
---|
219 | |
---|
220 | sys.exit(retcode) |
---|