1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Invoice generator |
---|
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 sys |
---|
28 | import os |
---|
29 | import pwd |
---|
30 | import grp |
---|
31 | from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
32 | from pykota.config import PyKotaConfigError |
---|
33 | from pykota.storage import PyKotaStorageError |
---|
34 | |
---|
35 | __doc__ = N_("""pkinvoice v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
36 | |
---|
37 | An invoice generator for PyKota. |
---|
38 | |
---|
39 | command line usage : |
---|
40 | |
---|
41 | pkinvoice [options] user1 user2 ... userN |
---|
42 | |
---|
43 | options : |
---|
44 | |
---|
45 | -v | --version Prints edpykota's version number then exits. |
---|
46 | -h | --help Prints this message then exits. |
---|
47 | |
---|
48 | -g | --groups Generate invoices for users groups instead of users. |
---|
49 | |
---|
50 | -r | --reference v Uses v as the initial reference value : an invoice |
---|
51 | will be generated for any account balance's value |
---|
52 | lower or equal to the reference value. The |
---|
53 | default reference value is of course 0.0 credits. |
---|
54 | |
---|
55 | -o | --output f.pdf Defines the name of the invoice file which will |
---|
56 | be generated as a PDF document. If not set or |
---|
57 | set to '-', the PDF document is sent to standard |
---|
58 | output. |
---|
59 | |
---|
60 | -u | --unit u Defines the name of the unit to use on the invoice. |
---|
61 | The default unit is 'Credits', optionally translated |
---|
62 | to your native language if it is supported by PyKota. |
---|
63 | |
---|
64 | -V | --vat p Sets the percent value of the applicable VAT. |
---|
65 | The default is 0.0, meaning no VAT information |
---|
66 | will be included. |
---|
67 | |
---|
68 | user1 through userN and group1 through groupN can use wildcards if |
---|
69 | needed. If no user argument is used, a wildcard of '*' is assumed, |
---|
70 | meaning include all users or groups. |
---|
71 | |
---|
72 | examples : |
---|
73 | |
---|
74 | $ pkinvoice --reference 15.0 --unit EURO --output invoices.pdf |
---|
75 | |
---|
76 | Will generate a PDF document containing invoices for all users whose |
---|
77 | account balance is below 15.0 credits. For each user the amount due |
---|
78 | will be (15.0 - AccountBalance) EURO. No VAT information will be |
---|
79 | included. |
---|
80 | """) |
---|
81 | |
---|
82 | class PKInvoice(PyKotaTool) : |
---|
83 | """A class for pkinvoice.""" |
---|
84 | def main(self, names, options) : |
---|
85 | """Generate invoices.""" |
---|
86 | if not self.config.isAdmin : |
---|
87 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
88 | |
---|
89 | self.display("%s...\n" % _("Processing")) |
---|
90 | if not names : |
---|
91 | names = [ "*" ] |
---|
92 | |
---|
93 | suffix = (options["groups"] and "Group") or "User" |
---|
94 | entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) |
---|
95 | nbtotal = len(entries) |
---|
96 | self.printInfo("Not implemented yet !", "warn") |
---|
97 | for i in range(nbtotal) : |
---|
98 | # TODO |
---|
99 | percent = 100.0 * float(i) / float(nbtotal) |
---|
100 | self.display("\r%.02f%%" % percent) |
---|
101 | self.display("\r100.00%%\r ") |
---|
102 | self.display("\r%s\n" % _("Done.")) |
---|
103 | |
---|
104 | if __name__ == "__main__" : |
---|
105 | retcode = 0 |
---|
106 | try : |
---|
107 | defaults = { "reference" : "0.0", |
---|
108 | "vat" : "0.0", |
---|
109 | "unit" : _("Credits"), |
---|
110 | "output" : "-", |
---|
111 | } |
---|
112 | short_options = "vho:gr:u:V:" |
---|
113 | long_options = ["help", "version", \ |
---|
114 | "groups", "reference=", "unit=", "output=", \ |
---|
115 | "vat=", ] |
---|
116 | |
---|
117 | # Initializes the command line tool |
---|
118 | invoiceGenerator = PKInvoice(doc=__doc__) |
---|
119 | invoiceGenerator.deferredInit() |
---|
120 | |
---|
121 | # parse and checks the command line |
---|
122 | (options, args) = invoiceGenerator.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
123 | |
---|
124 | # sets long options |
---|
125 | options["help"] = options["h"] or options["help"] |
---|
126 | options["version"] = options["v"] or options["version"] |
---|
127 | |
---|
128 | options["groups"] = options["g"] or options["groups"] |
---|
129 | options["reference"] = options["r"] or options["reference"] or defaults["reference"] |
---|
130 | options["vat"] = options["V"] or options["vat"] or defaults["vat"] |
---|
131 | options["unit"] = options["u"] or options["unit"] or defaults["unit"] |
---|
132 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
133 | |
---|
134 | if options["help"] : |
---|
135 | invoiceGenerator.display_usage_and_quit() |
---|
136 | elif options["version"] : |
---|
137 | invoiceGenerator.display_version_and_quit() |
---|
138 | else : |
---|
139 | retcode = invoiceGenerator.main(args, options) |
---|
140 | except KeyboardInterrupt : |
---|
141 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
142 | retcode = -3 |
---|
143 | except PyKotaCommandLineError, msg : |
---|
144 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
145 | retcode = -2 |
---|
146 | except SystemExit : |
---|
147 | pass |
---|
148 | except : |
---|
149 | try : |
---|
150 | invoiceGenerator.crashed("pkinvoice failed") |
---|
151 | except : |
---|
152 | crashed("pkinvoice failed") |
---|
153 | retcode = -1 |
---|
154 | |
---|
155 | try : |
---|
156 | invoiceGenerator.storage.close() |
---|
157 | except (TypeError, NameError, AttributeError) : |
---|
158 | pass |
---|
159 | |
---|
160 | sys.exit(retcode) |
---|