1 | #! /usr/bin/env python |
---|
2 | # -*- coding: UTF-8 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
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 |
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # |
---|
23 | |
---|
24 | """A billing codes manager for PyKota.""" |
---|
25 | |
---|
26 | import os |
---|
27 | import sys |
---|
28 | import pwd |
---|
29 | |
---|
30 | import pykota.appinit |
---|
31 | from pykota.utils import run |
---|
32 | from pykota.commandline import PyKotaOptionParser |
---|
33 | from pykota.errors import PyKotaCommandLineError |
---|
34 | from pykota.tool import Percent, PyKotaTool |
---|
35 | from pykota.storage import StorageBillingCode |
---|
36 | |
---|
37 | class PKBcodes(PyKotaTool) : |
---|
38 | """A class for a billing codes manager.""" |
---|
39 | def modifyBillingCode(self, billingcode, reset, description) : |
---|
40 | """Modifies a billing code.""" |
---|
41 | if reset : |
---|
42 | billingcode.reset() |
---|
43 | if description is not None : # NB : "" is allowed ! |
---|
44 | billingcode.setDescription(description) |
---|
45 | |
---|
46 | def main(self, names, options) : |
---|
47 | """Manage billing codes.""" |
---|
48 | if options.action != "list" : |
---|
49 | self.adminOnly() |
---|
50 | |
---|
51 | islist = (options.action == "list") |
---|
52 | isadd = (options.action == "add") |
---|
53 | isdelete = (options.action == "delete") |
---|
54 | |
---|
55 | if not names : |
---|
56 | if isdelete or isadd : |
---|
57 | raise PyKotaCommandLineError, _("You must specify billing codes on the command line.") |
---|
58 | names = [u"*"] |
---|
59 | |
---|
60 | if not islist : |
---|
61 | percent = Percent(self) |
---|
62 | |
---|
63 | if not isadd : |
---|
64 | if not islist : |
---|
65 | percent.display("%s..." % _("Extracting datas")) |
---|
66 | if not names : |
---|
67 | names = ["*"] |
---|
68 | billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) |
---|
69 | if not billingcodes : |
---|
70 | if not islist : |
---|
71 | percent.display("\n") |
---|
72 | raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) |
---|
73 | if not islist : |
---|
74 | percent.setSize(len(billingcodes)) |
---|
75 | |
---|
76 | if islist : |
---|
77 | for billingcode in billingcodes : |
---|
78 | self.display("%s [%s] %s %s %s %.2f %s\n" % \ |
---|
79 | (billingcode.BillingCode, billingcode.Description, \ |
---|
80 | billingcode.PageCounter, \ |
---|
81 | _("pages"), \ |
---|
82 | _("and"), \ |
---|
83 | billingcode.Balance, \ |
---|
84 | _("credits"))) |
---|
85 | elif isdelete : |
---|
86 | percent.display("\n%s..." % _("Deletion")) |
---|
87 | self.storage.deleteManyBillingCodes(billingcodes) |
---|
88 | percent.display("\n") |
---|
89 | else : |
---|
90 | description = options.description |
---|
91 | if description : |
---|
92 | description = description.strip() |
---|
93 | |
---|
94 | self.storage.beginTransaction() |
---|
95 | try : |
---|
96 | if isadd : |
---|
97 | percent.display("%s...\n" % _("Creation")) |
---|
98 | percent.setSize(len(names)) |
---|
99 | for bname in names : |
---|
100 | billingcode = StorageBillingCode(self.storage, bname) |
---|
101 | self.modifyBillingCode(billingcode, |
---|
102 | options.reset, |
---|
103 | description) |
---|
104 | oldbillingcode = self.storage.addBillingCode(billingcode) |
---|
105 | if oldbillingcode is not None : |
---|
106 | if options.skipexisting : |
---|
107 | self.logdebug(_("Billing code '%(bname)s' already exists, skipping.") % locals()) |
---|
108 | else : |
---|
109 | self.logdebug(_("Billing code '%(bname)s' already exists, will be modified.") % locals()) |
---|
110 | self.modifyBillingCode(oldbillingcode, |
---|
111 | options.reset, |
---|
112 | description) |
---|
113 | oldbillingcode.save() |
---|
114 | percent.oneMore() |
---|
115 | else : |
---|
116 | percent.display("\n%s...\n" % _("Modification")) |
---|
117 | for billingcode in billingcodes : |
---|
118 | self.modifyBillingCode(billingcode, |
---|
119 | options.reset, |
---|
120 | description) |
---|
121 | billingcode.save() |
---|
122 | percent.oneMore() |
---|
123 | except : |
---|
124 | self.storage.rollbackTransaction() |
---|
125 | raise |
---|
126 | else : |
---|
127 | self.storage.commitTransaction() |
---|
128 | |
---|
129 | if not islist : |
---|
130 | percent.done() |
---|
131 | |
---|
132 | if __name__ == "__main__" : |
---|
133 | parser = PyKotaOptionParser(description=_("A billing codes manager for PyKota."), |
---|
134 | usage="pkbcodes [options] code1 code2 ... codeN") |
---|
135 | parser.add_option("-a", "--add", |
---|
136 | action="store_const", |
---|
137 | const="add", |
---|
138 | dest="action", |
---|
139 | help=_("Add new, or modify existing, billing codes.")) |
---|
140 | parser.add_option("-d", "--delete", |
---|
141 | action="store_const", |
---|
142 | const="delete", |
---|
143 | dest="action", |
---|
144 | help=_("Deletes billing codes. Matching entries in the printing history are not deleted, on purpose.")) |
---|
145 | parser.add_option("-D", "--description", |
---|
146 | dest="description", |
---|
147 | help=_("Set a textual description for the specified billing codes.")) |
---|
148 | parser.add_option("-l", "--list", |
---|
149 | action="store_const", |
---|
150 | const="list", |
---|
151 | dest="action", |
---|
152 | help=_("Display detailed informations about the specified billing codes.")) |
---|
153 | parser.add_option("-r", "--reset", |
---|
154 | action="store_true", |
---|
155 | dest="reset", |
---|
156 | help=_("Reset the page count and amount spent for the specified billing codes.")) |
---|
157 | parser.add_option("-s", "--skipexisting", |
---|
158 | action="store_true", |
---|
159 | dest="skipexisting", |
---|
160 | help=_("If --add is used, ensure that existing billing codes won't be modified.")) |
---|
161 | |
---|
162 | parser.add_example('-D "Financial Department" financial', |
---|
163 | _("Would create a billing code labelled 'financial' with the specified textual description.")) |
---|
164 | parser.add_example('--delete "fin*"', |
---|
165 | _("Would delete all billing codes which label begins with 'fin'. Matching jobs in the printing history wouldn't be deleted though.")) |
---|
166 | parser.add_example("--list", |
---|
167 | _("Would display details about all existing billing codes.")) |
---|
168 | |
---|
169 | (options, arguments) = parser.parse_args() |
---|
170 | run(parser, PKBcodes) |
---|