1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # A banner generator for PyKota |
---|
5 | # |
---|
6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
7 | # |
---|
8 | # (c) 2003-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | # |
---|
23 | # $Id$ |
---|
24 | # |
---|
25 | # $Log$ |
---|
26 | # Revision 1.3 2004/11/12 23:46:44 jalet |
---|
27 | # Heavy work on pkbanner. Not finished yet though, but mostly works. |
---|
28 | # |
---|
29 | # Revision 1.2 2004/11/11 14:25:48 jalet |
---|
30 | # Added some TODO comments |
---|
31 | # |
---|
32 | # Revision 1.1 2004/11/10 22:48:47 jalet |
---|
33 | # Banner generator's skeleton added |
---|
34 | # |
---|
35 | # |
---|
36 | # |
---|
37 | |
---|
38 | import sys |
---|
39 | import os |
---|
40 | import cStringIO |
---|
41 | |
---|
42 | try : |
---|
43 | from reportlab.pdfgen import canvas |
---|
44 | from reportlab.lib import pagesizes |
---|
45 | from reportlab.lib.units import cm |
---|
46 | except ImportError : |
---|
47 | hasRL = 0 |
---|
48 | else : |
---|
49 | hasRL = 1 |
---|
50 | |
---|
51 | try : |
---|
52 | import PIL.Image |
---|
53 | except ImportError : |
---|
54 | hasPIL = 0 |
---|
55 | else : |
---|
56 | hasPIL = 1 |
---|
57 | |
---|
58 | from pykota.tool import Tool, PyKotaToolError, crashed, N_ |
---|
59 | |
---|
60 | __doc__ = N_("""pkbanner v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
61 | |
---|
62 | Generates banners. |
---|
63 | |
---|
64 | command line usage : |
---|
65 | |
---|
66 | pkbanner [options] [files] |
---|
67 | |
---|
68 | options : |
---|
69 | |
---|
70 | -v | --version Prints pkbanner's version number then exits. |
---|
71 | -h | --help Prints this message then exits. |
---|
72 | |
---|
73 | ... TODO... |
---|
74 | |
---|
75 | examples : |
---|
76 | |
---|
77 | ... TODO... |
---|
78 | |
---|
79 | This program is free software; you can redistribute it and/or modify |
---|
80 | it under the terms of the GNU General Public License as published by |
---|
81 | the Free Software Foundation; either version 2 of the License, or |
---|
82 | (at your option) any later version. |
---|
83 | |
---|
84 | This program is distributed in the hope that it will be useful, |
---|
85 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
86 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
87 | GNU General Public License for more details. |
---|
88 | |
---|
89 | You should have received a copy of the GNU General Public License |
---|
90 | along with this program; if not, write to the Free Software |
---|
91 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
92 | |
---|
93 | Please e-mail bugs to: %s""") |
---|
94 | |
---|
95 | class PyKotaBanner(Tool) : |
---|
96 | """A class for pkbanner.""" |
---|
97 | primaryfields = [ |
---|
98 | ("PRINTERNAME", N_("Printer")), |
---|
99 | ("USERNAME", N_("User")), |
---|
100 | ("JOBID", N_("JobId")), |
---|
101 | ("JOBORIGINATINGHOSTNAME", N_("Client host")), |
---|
102 | ("TITLE", N_("Title")), |
---|
103 | ("FILENAME", N_("Filename")), |
---|
104 | ("COPIES", N_("Copies")), |
---|
105 | ("JOBSIZEBYTES", N_("Job size in bytes")), |
---|
106 | ("PRECOMPUTEDJOBSIZE", N_("Estimated job size")), |
---|
107 | ("PRECOMPUTEDJOBPRICE", N_("Estimated job price")), |
---|
108 | ] |
---|
109 | |
---|
110 | secondaryfields = [ |
---|
111 | ("LIMITBY", N_("Account limited by")), |
---|
112 | ("BALANCE", N_("Account balance")), |
---|
113 | ("PAGECOUNTER", N_("Number of pages on this printer")), |
---|
114 | ("SOFTLIMIT", N_("Soft limit")), |
---|
115 | ("HARDLIMIT", N_("Hard limit")), |
---|
116 | ("DATELIMIT", N_("Date limit")), |
---|
117 | ] |
---|
118 | |
---|
119 | tertiaryfields = [ |
---|
120 | ("ACTION", N_("Action taken for current job")), |
---|
121 | ] |
---|
122 | |
---|
123 | def getPageSize(self, pgsize) : |
---|
124 | """Returns the correct page size or None if not found.""" |
---|
125 | try : |
---|
126 | return getattr(pagesizes, pgsize.upper()) |
---|
127 | except AttributeError : |
---|
128 | try : |
---|
129 | return getattr(pagesizes, pgsize.lower()) |
---|
130 | except AttributeError : |
---|
131 | pass |
---|
132 | |
---|
133 | def genPDF(self, pagesize, logo, url) : |
---|
134 | """Generates the banner in PDF format, return the PDF document as a string.""" |
---|
135 | document = cStringIO.StringIO() |
---|
136 | c = canvas.Canvas(document, pagesize=pagesize, pageCompression=1) |
---|
137 | |
---|
138 | c.setAuthor("Jerome Alet") |
---|
139 | c.setTitle("PyKota generated Banner") |
---|
140 | c.setSubject("This is a print banner generated with PyKota") |
---|
141 | |
---|
142 | xcenter = pagesize[0] / 2.0 |
---|
143 | ycenter = pagesize[1] / 2.0 |
---|
144 | |
---|
145 | ypos = pagesize[1] - (2 * cm) |
---|
146 | try : |
---|
147 | imglogo = PIL.Image.open(logo) |
---|
148 | except : |
---|
149 | self.printInfo("Unable to open image %s" % logo, "warn") |
---|
150 | else : |
---|
151 | (width, height) = imglogo.size |
---|
152 | multi = float(width) / (8 * cm) |
---|
153 | width = float(width) / multi |
---|
154 | height = float(height) / multi |
---|
155 | xpos = xcenter - (width / 2.0) |
---|
156 | ypos -= height |
---|
157 | c.drawImage(logo, xpos, ypos, width, height) |
---|
158 | |
---|
159 | # New top |
---|
160 | xpos = pagesize[0] / 5.0 |
---|
161 | ypos -= (1 * cm) + 20 |
---|
162 | |
---|
163 | for fieldsgroup in [ self.primaryfields, self.secondaryfields, self.tertiaryfields] : |
---|
164 | c.saveState() |
---|
165 | for (varname, label) in fieldsgroup : |
---|
166 | c.setFont("Helvetica-Bold", 14) |
---|
167 | c.setFillColorRGB(0, 0, 0) |
---|
168 | message = "%s :" % _(label).title() |
---|
169 | c.drawRightString(xcenter, ypos, message) |
---|
170 | c.setFont("Courier-Bold", 14) |
---|
171 | c.setFillColorRGB(1, 0, 0) |
---|
172 | c.drawString(xcenter + 0.5*cm, ypos, os.environ.get("PYKOTA%s" % varname, _("Unknown"))) |
---|
173 | ypos -= 18 # next line |
---|
174 | c.restoreState() |
---|
175 | ypos -= 18 # skip an additionnal line |
---|
176 | |
---|
177 | # URL |
---|
178 | c.saveState() |
---|
179 | c.setFont("Courier-Bold", 16) |
---|
180 | c.setFillColorRGB(0, 0, 1) |
---|
181 | c.drawCentredString(xcenter, 2 * cm, url) |
---|
182 | c.restoreState() |
---|
183 | |
---|
184 | c.showPage() |
---|
185 | c.save() |
---|
186 | return document.getvalue() |
---|
187 | |
---|
188 | def main(self, files, options) : |
---|
189 | """Generates a banner.""" |
---|
190 | if not hasRL : |
---|
191 | raise PyKotaToolError, "The ReportLab module is missing. Download it from http://www.reportlab.org" |
---|
192 | if not hasPIL : |
---|
193 | raise PyKotaToolError, "The Python Imaging Library is missing. Download it from http://www.pythonware.com/downloads" |
---|
194 | |
---|
195 | pagesize = self.getPageSize(options["pagesize"]) |
---|
196 | if pagesize is None : |
---|
197 | pagesize = self.getPageSize("a4") |
---|
198 | self.printInfo("Unknown page size %s, defaulting to A4." % options["pagesize"], "warn") |
---|
199 | |
---|
200 | doc = self.genPDF(pagesize, options["logo"], options["url"].strip()) |
---|
201 | print doc |
---|
202 | |
---|
203 | def getInfo(name) : |
---|
204 | """Extracts some information from the environment.""" |
---|
205 | return os.environ.get(name, _("Unknown")) |
---|
206 | |
---|
207 | if __name__ == "__main__" : |
---|
208 | # TODO : --papertray : to print banners on a different paper (colored for example) |
---|
209 | retcode = 0 |
---|
210 | try : |
---|
211 | defaults = { \ |
---|
212 | "pagesize" : "a4", \ |
---|
213 | "logo" : "/usr/share/pykota/logos/pykota.jpeg", |
---|
214 | "url" : "http://www.librelogiciel.com/software/", |
---|
215 | } |
---|
216 | short_options = "vhl:p:u:" |
---|
217 | long_options = ["help", "version", "pagesize=", "logo=", "url="] |
---|
218 | |
---|
219 | # Initializes the command line tool |
---|
220 | banner = PyKotaBanner(doc=__doc__) |
---|
221 | |
---|
222 | # parse and checks the command line |
---|
223 | (options, args) = banner.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
224 | |
---|
225 | # sets long options |
---|
226 | options["help"] = options["h"] or options["help"] |
---|
227 | options["version"] = options["v"] or options["version"] |
---|
228 | options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] |
---|
229 | options["logo"] = options["l"] or options["logo"] or defaults["logo"] |
---|
230 | options["url"] = options["u"] or options["url"] or defaults["url"] |
---|
231 | |
---|
232 | if options["help"] : |
---|
233 | banner.display_usage_and_quit() |
---|
234 | elif options["version"] : |
---|
235 | banner.display_version_and_quit() |
---|
236 | else : |
---|
237 | retcode = banner.main(args, options) |
---|
238 | except SystemExit : |
---|
239 | pass |
---|
240 | except : |
---|
241 | try : |
---|
242 | banner.crashed("pkbanner failed") |
---|
243 | except : |
---|
244 | crashed("pkbanner failed") |
---|
245 | retcode = -1 |
---|
246 | |
---|
247 | sys.exit(retcode) |
---|