[1908] | 1 | #! /usr/bin/env python |
---|
[3260] | 2 | # -*- coding: UTF-8 -*- |
---|
[1908] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[1908] | 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[1908] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1908] | 10 | # (at your option) any later version. |
---|
[3260] | 11 | # |
---|
[1908] | 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 |
---|
[3260] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1908] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[2028] | 22 | # |
---|
[1908] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import os |
---|
[1923] | 26 | import time |
---|
[1911] | 27 | import cStringIO |
---|
[1918] | 28 | import popen2 |
---|
[1908] | 29 | |
---|
[1911] | 30 | try : |
---|
| 31 | from reportlab.pdfgen import canvas |
---|
| 32 | from reportlab.lib import pagesizes |
---|
| 33 | from reportlab.lib.units import cm |
---|
| 34 | except ImportError : |
---|
| 35 | hasRL = 0 |
---|
| 36 | else : |
---|
| 37 | hasRL = 1 |
---|
| 38 | |
---|
| 39 | try : |
---|
| 40 | import PIL.Image |
---|
| 41 | except ImportError : |
---|
| 42 | hasPIL = 0 |
---|
| 43 | else : |
---|
| 44 | hasPIL = 1 |
---|
| 45 | |
---|
[2512] | 46 | from pykota.tool import Tool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[1911] | 47 | |
---|
[2344] | 48 | __doc__ = N_("""pkbanner v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[1911] | 49 | |
---|
| 50 | Generates banners. |
---|
| 51 | |
---|
| 52 | command line usage : |
---|
| 53 | |
---|
[2193] | 54 | pkbanner [options] [more info] |
---|
[1911] | 55 | |
---|
| 56 | options : |
---|
| 57 | |
---|
| 58 | -v | --version Prints pkbanner's version number then exits. |
---|
| 59 | -h | --help Prints this message then exits. |
---|
| 60 | |
---|
[1923] | 61 | -l | --logo img Use the image as the banner's logo. The logo will |
---|
[1938] | 62 | be drawn at the center top of the page. The default |
---|
[1923] | 63 | logo is /usr/share/pykota/logos/pykota.jpeg |
---|
| 64 | |
---|
[1934] | 65 | -p | --pagesize sz Sets sz as the page size. Most well known |
---|
| 66 | page sizes are recognized, like 'A4' or 'Letter' |
---|
| 67 | to name a few. The default size is A4. |
---|
| 68 | |
---|
[1938] | 69 | -s | --savetoner s Sets the text luminosity factor to s%%. This can be |
---|
[1934] | 70 | used to save toner. The default value is 0, which |
---|
| 71 | means that no toner saving will be done. |
---|
| 72 | |
---|
[1923] | 73 | -u | --url u Uses u as an url to be written at the bottom of |
---|
| 74 | the banner page. The default url is : |
---|
[2909] | 75 | http://www.pykota.com/ |
---|
[1923] | 76 | |
---|
[1911] | 77 | examples : |
---|
| 78 | |
---|
[1923] | 79 | Using pkbanner directly from the command line is not recommended, |
---|
| 80 | excepted for testing purposes. You should use pkbanner in the |
---|
| 81 | 'startingbanner' or 'endingbanner' directives in pykota.conf |
---|
| 82 | |
---|
[1936] | 83 | startingbanner: /usr/bin/pkbanner --logo="" --savetoner=75 |
---|
| 84 | |
---|
| 85 | With such a setting in pykota.conf, all print jobs will be |
---|
| 86 | prefixed with an A4 banner with no logo, and text luminosity will |
---|
| 87 | be increased by 75%%. The PostScript output will be directly sent |
---|
| 88 | to your printer. |
---|
| 89 | |
---|
| 90 | You'll find more examples in the sample configuration file included |
---|
| 91 | in PyKota. |
---|
[2344] | 92 | """) |
---|
[1911] | 93 | |
---|
| 94 | class PyKotaBanner(Tool) : |
---|
| 95 | """A class for pkbanner.""" |
---|
| 96 | def getPageSize(self, pgsize) : |
---|
| 97 | """Returns the correct page size or None if not found.""" |
---|
| 98 | try : |
---|
| 99 | return getattr(pagesizes, pgsize.upper()) |
---|
| 100 | except AttributeError : |
---|
| 101 | try : |
---|
| 102 | return getattr(pagesizes, pgsize.lower()) |
---|
| 103 | except AttributeError : |
---|
| 104 | pass |
---|
| 105 | |
---|
[1923] | 106 | def getVar(self, varname) : |
---|
| 107 | """Extracts a variable from the environment and returns its value or 'Unknown' in the current locale.""" |
---|
| 108 | return os.environ.get(varname) or _("Unknown") |
---|
| 109 | |
---|
[1934] | 110 | def printVar(self, canvas, x, y, label, value, size, savetoner) : |
---|
[1923] | 111 | """Outputs a variable onto the PDF canvas. |
---|
| 112 | |
---|
| 113 | Returns the number of points to substract to current Y coordinate. |
---|
| 114 | """ |
---|
| 115 | canvas.saveState() |
---|
| 116 | canvas.setFont("Helvetica-Bold", size) |
---|
[1934] | 117 | (r, g, b) = [ color + (savetoner * (1.0 - color)) for color in (0, 0, 0) ] # Black * savetoner |
---|
[1923] | 118 | canvas.setFillColorRGB(r, g, b) |
---|
[1925] | 119 | message = "%s :" % _(label) |
---|
[3079] | 120 | canvas.drawRightString(x, y, self.userCharsetToUTF8(message)) |
---|
[1923] | 121 | canvas.setFont("Courier-Bold", size) |
---|
[1934] | 122 | (r, g, b) = [ color + (savetoner * (1.0 - color)) for color in (1, 0, 0) ] # Red * savetoner |
---|
[1923] | 123 | canvas.setFillColorRGB(r, g, b) |
---|
[3079] | 124 | canvas.drawString(x + 0.5*cm, y, self.userCharsetToUTF8(value)) |
---|
[1923] | 125 | canvas.restoreState() |
---|
| 126 | return (size + 4) |
---|
| 127 | |
---|
[2193] | 128 | def genPDF(self, pagesize, logo, url, text, savetoner) : |
---|
[1911] | 129 | """Generates the banner in PDF format, return the PDF document as a string.""" |
---|
| 130 | document = cStringIO.StringIO() |
---|
| 131 | c = canvas.Canvas(document, pagesize=pagesize, pageCompression=1) |
---|
| 132 | |
---|
| 133 | c.setAuthor("Jerome Alet") |
---|
| 134 | c.setTitle("PyKota generated Banner") |
---|
| 135 | c.setSubject("This is a print banner generated with PyKota") |
---|
| 136 | |
---|
| 137 | xcenter = pagesize[0] / 2.0 |
---|
| 138 | ycenter = pagesize[1] / 2.0 |
---|
| 139 | |
---|
| 140 | ypos = pagesize[1] - (2 * cm) |
---|
| 141 | |
---|
[1923] | 142 | if logo : |
---|
| 143 | try : |
---|
| 144 | imglogo = PIL.Image.open(logo) |
---|
| 145 | except : |
---|
| 146 | self.printInfo("Unable to open image %s" % logo, "warn") |
---|
| 147 | else : |
---|
| 148 | (width, height) = imglogo.size |
---|
| 149 | multi = float(width) / (8 * cm) |
---|
| 150 | width = float(width) / multi |
---|
| 151 | height = float(height) / multi |
---|
| 152 | xpos = xcenter - (width / 2.0) |
---|
| 153 | ypos -= height |
---|
| 154 | c.drawImage(logo, xpos, ypos, width, height) |
---|
| 155 | |
---|
[1911] | 156 | # New top |
---|
| 157 | xpos = pagesize[0] / 5.0 |
---|
| 158 | ypos -= (1 * cm) + 20 |
---|
| 159 | |
---|
[1923] | 160 | printername = self.getVar("PYKOTAPRINTERNAME") |
---|
| 161 | username = self.getVar("PYKOTAUSERNAME") |
---|
| 162 | accountbanner = self.config.getAccountBanner(printername) |
---|
| 163 | |
---|
| 164 | # Outputs the username |
---|
[1934] | 165 | ypos -= self.printVar(c, xcenter, ypos, _("Username"), username, 20, savetoner) |
---|
[1923] | 166 | |
---|
[2193] | 167 | # Text |
---|
| 168 | if text : |
---|
| 169 | ypos -= self.printVar(c, xcenter, ypos, _("More Info"), text, 20, savetoner) |
---|
| 170 | |
---|
[1923] | 171 | # Printer and Job Id |
---|
| 172 | job = "%s - %s" % (printername, self.getVar("PYKOTAJOBID")) |
---|
[1934] | 173 | ypos -= self.printVar(c, xcenter, ypos, _("Job"), job, 14, savetoner) |
---|
[1923] | 174 | |
---|
| 175 | # Current date (TODO : at the time the banner was printed ! Change this to job's submission date) |
---|
| 176 | datetime = time.strftime("%c", time.localtime()) |
---|
[1934] | 177 | ypos -= self.printVar(c, xcenter, ypos, _("Date"), datetime, 14, savetoner) |
---|
[1923] | 178 | |
---|
| 179 | # Result of the print job |
---|
| 180 | action = self.getVar("PYKOTAACTION") |
---|
| 181 | if action == "ALLOW" : |
---|
| 182 | action = _("Allowed") |
---|
| 183 | elif action == "DENY" : |
---|
| 184 | action = _("Denied") |
---|
| 185 | elif action == "WARN" : |
---|
| 186 | action = _("Allowed with Warning") |
---|
[2631] | 187 | elif action == "PROBLEM" : |
---|
| 188 | # should never occur |
---|
| 189 | action = _("Problem") |
---|
| 190 | elif action == "CANCEL" : |
---|
| 191 | # should never occur |
---|
| 192 | action = _("Cancelled") |
---|
[1934] | 193 | ypos -= self.printVar(c, xcenter, ypos, _("Result"), action, 14, savetoner) |
---|
[1923] | 194 | |
---|
| 195 | # skip some space |
---|
| 196 | ypos -= 20 |
---|
| 197 | |
---|
| 198 | # Outputs title and filename |
---|
[1926] | 199 | # We put them at x=0.25*pagewidth so that the line is long enough to hold them |
---|
[1923] | 200 | title = self.getVar("PYKOTATITLE") |
---|
[1934] | 201 | ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Title"), title, 10, savetoner) |
---|
[1923] | 202 | |
---|
| 203 | filename = self.getVar("PYKOTAFILENAME") |
---|
[1934] | 204 | ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Filename"), filename, 10, savetoner) |
---|
[1923] | 205 | |
---|
| 206 | # skip some space |
---|
| 207 | ypos -= 20 |
---|
| 208 | |
---|
| 209 | # Now outputs the user's account balance or page counter |
---|
[1934] | 210 | ypos -= self.printVar(c, xcenter, ypos, _("Pages printed so far on %s") % printername, self.getVar("PYKOTAPAGECOUNTER"), 14, savetoner) |
---|
[1923] | 211 | limitby = self.getVar("PYKOTALIMITBY") |
---|
| 212 | if limitby == "balance" : |
---|
[1934] | 213 | ypos -= self.printVar(c, xcenter, ypos, _("Account balance"), self.getVar("PYKOTABALANCE"), 14, savetoner) |
---|
[2627] | 214 | elif limitby == "quota" : |
---|
[1934] | 215 | ypos -= self.printVar(c, xcenter, ypos, _("Soft Limit"), self.getVar("PYKOTASOFTLIMIT"), 14, savetoner) |
---|
| 216 | ypos -= self.printVar(c, xcenter, ypos, _("Hard Limit"), self.getVar("PYKOTAHARDLIMIT"), 14, savetoner) |
---|
| 217 | ypos -= self.printVar(c, xcenter, ypos, _("Date Limit"), self.getVar("PYKOTADATELIMIT"), 14, savetoner) |
---|
[2627] | 218 | else : |
---|
| 219 | if limitby == "noquota" : |
---|
| 220 | msg = _("No Limit") |
---|
| 221 | elif limitby == "nochange" : |
---|
| 222 | msg = _("No Accounting") |
---|
| 223 | elif limitby == "noprint" : |
---|
| 224 | msg = _("Forbidden") |
---|
| 225 | else : |
---|
| 226 | msg = _("Unknown") |
---|
| 227 | ypos -= self.printVar(c, xcenter, ypos, _("Printing Mode"), msg, 14, savetoner) |
---|
[1923] | 228 | |
---|
| 229 | # URL |
---|
| 230 | if url : |
---|
[1911] | 231 | c.saveState() |
---|
[1923] | 232 | c.setFont("Courier-Bold", 16) |
---|
[1934] | 233 | (r, g, b) = [ color + (savetoner * (1.0 - color)) for color in (0, 0, 1) ] # Blue * savetoner |
---|
[1923] | 234 | c.setFillColorRGB(r, g, b) |
---|
| 235 | c.drawCentredString(xcenter, 2 * cm, url) |
---|
[1911] | 236 | c.restoreState() |
---|
| 237 | |
---|
| 238 | c.showPage() |
---|
| 239 | c.save() |
---|
| 240 | return document.getvalue() |
---|
[1923] | 241 | |
---|
[2193] | 242 | def main(self, arguments, options) : |
---|
[1911] | 243 | """Generates a banner.""" |
---|
| 244 | if not hasRL : |
---|
| 245 | raise PyKotaToolError, "The ReportLab module is missing. Download it from http://www.reportlab.org" |
---|
| 246 | if not hasPIL : |
---|
| 247 | raise PyKotaToolError, "The Python Imaging Library is missing. Download it from http://www.pythonware.com/downloads" |
---|
| 248 | |
---|
[1923] | 249 | try : |
---|
[1934] | 250 | savetoner = int(options["savetoner"]) |
---|
| 251 | if (savetoner < 0) or (savetoner > 99) : |
---|
| 252 | raise ValueError, _("Allowed range is (0..99)") |
---|
| 253 | savetoner /= 100.0 |
---|
[1923] | 254 | except (TypeError, ValueError), msg : |
---|
[1934] | 255 | self.printInfo(_("Invalid 'savetoner' option %s : %s") % (options["savetoner"], msg), "warn") |
---|
| 256 | savetoner = 0.0 |
---|
[1923] | 257 | |
---|
[1911] | 258 | pagesize = self.getPageSize(options["pagesize"]) |
---|
| 259 | if pagesize is None : |
---|
| 260 | pagesize = self.getPageSize("a4") |
---|
[1934] | 261 | self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options["pagesize"], "warn") |
---|
[1911] | 262 | |
---|
[1918] | 263 | self.logdebug("Generating the banner in PDF format...") |
---|
[2193] | 264 | doc = self.genPDF(pagesize, |
---|
| 265 | options["logo"].strip(), |
---|
| 266 | options["url"].strip(), |
---|
| 267 | " ".join(arguments).strip(), |
---|
| 268 | savetoner) |
---|
[1918] | 269 | |
---|
| 270 | self.logdebug("Converting the banner to PostScript...") |
---|
| 271 | os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "") |
---|
[2193] | 272 | child = popen2.Popen3("gs -q -dNOPAUSE -dBATCH -dPARANOIDSAFER -sDEVICE=pswrite -sOutputFile=- -") |
---|
| 273 | try : |
---|
| 274 | child.tochild.write(doc) |
---|
| 275 | child.tochild.close() |
---|
| 276 | sys.stdout.write(child.fromchild.read()) |
---|
| 277 | sys.stdout.flush() |
---|
| 278 | child.fromchild.close() |
---|
| 279 | except IOError, msg : |
---|
| 280 | self.printInfo("I/O Error %s" % msg, "error") |
---|
[1918] | 281 | status = child.wait() |
---|
| 282 | if os.WIFEXITED(status) : |
---|
| 283 | status = os.WEXITSTATUS(status) |
---|
| 284 | self.logdebug("PDF to PostScript converter exit code is %s" % str(status)) |
---|
| 285 | self.logdebug("Banner completed.") |
---|
| 286 | return status |
---|
[1911] | 287 | |
---|
[1908] | 288 | if __name__ == "__main__" : |
---|
[1909] | 289 | # TODO : --papertray : to print banners on a different paper (colored for example) |
---|
[1911] | 290 | retcode = 0 |
---|
| 291 | try : |
---|
| 292 | defaults = { \ |
---|
[1971] | 293 | "savetoner" : "0", \ |
---|
[1911] | 294 | "pagesize" : "a4", \ |
---|
| 295 | "logo" : "/usr/share/pykota/logos/pykota.jpeg", |
---|
[2909] | 296 | "url" : "http://www.pykota.com/", |
---|
[1911] | 297 | } |
---|
[1934] | 298 | short_options = "vhs:l:p:u:" |
---|
| 299 | long_options = ["help", "version", "savetoner=", "pagesize=", "logo=", "url="] |
---|
[1911] | 300 | |
---|
| 301 | # Initializes the command line tool |
---|
| 302 | banner = PyKotaBanner(doc=__doc__) |
---|
[2210] | 303 | banner.deferredInit() |
---|
[1911] | 304 | |
---|
| 305 | # parse and checks the command line |
---|
| 306 | (options, args) = banner.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 307 | |
---|
| 308 | # sets long options |
---|
| 309 | options["help"] = options["h"] or options["help"] |
---|
| 310 | options["version"] = options["v"] or options["version"] |
---|
[1934] | 311 | options["savetoner"] = options["s"] or options["savetoner"] or defaults["savetoner"] |
---|
[1911] | 312 | options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] |
---|
| 313 | options["url"] = options["u"] or options["url"] or defaults["url"] |
---|
| 314 | |
---|
[1934] | 315 | options["logo"] = options["l"] or options["logo"] |
---|
| 316 | if options["logo"] is None : # Allows --logo="" to disable the logo entirely |
---|
| 317 | options["logo"] = defaults["logo"] |
---|
| 318 | |
---|
[1911] | 319 | if options["help"] : |
---|
| 320 | banner.display_usage_and_quit() |
---|
| 321 | elif options["version"] : |
---|
| 322 | banner.display_version_and_quit() |
---|
| 323 | else : |
---|
| 324 | retcode = banner.main(args, options) |
---|
[2216] | 325 | except KeyboardInterrupt : |
---|
| 326 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 327 | retcode = -3 |
---|
[2512] | 328 | except PyKotaCommandLineError, msg : |
---|
| 329 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 330 | retcode = -2 |
---|
[1911] | 331 | except SystemExit : |
---|
| 332 | pass |
---|
| 333 | except : |
---|
| 334 | try : |
---|
| 335 | banner.crashed("pkbanner failed") |
---|
| 336 | except : |
---|
| 337 | crashed("pkbanner failed") |
---|
| 338 | retcode = -1 |
---|
| 339 | |
---|
| 340 | sys.exit(retcode) |
---|