Changeset 3413 for pykota/trunk/bin/pkbanner
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/pkbanner
r3411 r3413 9 9 # the Free Software Foundation, either version 3 of the License, or 10 10 # (at your option) any later version. 11 # 11 # 12 12 # This program is distributed in the hope that it will be useful, 13 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 22 22 # 23 23 24 """A banner generator for PyKota""" 24 """A banner generator for PyKota""" 25 25 26 26 import sys … … 34 34 import reportlab.lib 35 35 from reportlab.lib.units import cm 36 except ImportError : 36 except ImportError : 37 37 hasRL = False 38 else : 38 else : 39 39 hasRL = True 40 40 41 41 try : 42 import PIL.Image 43 except ImportError : 42 import PIL.Image 43 except ImportError : 44 44 hasPIL = False 45 else : 45 else : 46 46 hasPIL = True 47 47 48 48 import pykota.appinit 49 49 from pykota.utils import run … … 56 56 from pykota import version 57 57 58 class PyKotaBanner(Tool) : 58 class PyKotaBanner(Tool) : 59 59 """A class for pkbanner.""" 60 def getVar(self, varname) : 60 def getVar(self, varname) : 61 61 """Extracts a variable from the environment and returns its value or 'Unknown' in the current locale.""" 62 62 return os.environ.get(varname) or _("Unknown") 63 63 64 64 def printVar(self, canvas, x, y, label, value, size, savetoner) : 65 65 """Outputs a variable onto the PDF canvas. 66 66 67 67 Returns the number of points to substract to current Y coordinate. 68 """ 68 """ 69 69 canvas.saveState() 70 70 canvas.setFont("Helvetica-Bold", size) … … 79 79 canvas.restoreState() 80 80 return (size + 4) 81 81 82 82 def genPDF(self, pagesize, logo, url, text, savetoner) : 83 83 """Generates the banner in PDF format, return the PDF document as a string.""" 84 84 document = cStringIO.StringIO() 85 85 c = canvas.Canvas(document, pagesize=pagesize, pageCompression=1) 86 86 87 87 c.setAuthor(self.effectiveUserName) 88 88 c.setTitle(_("PyKota generated Banner")) 89 89 c.setSubject(_("This is a print banner generated with PyKota")) 90 90 91 91 xcenter = pagesize[0] / 2.0 92 92 ycenter = pagesize[1] / 2.0 93 94 ypos = pagesize[1] - (2 * cm) 95 93 94 ypos = pagesize[1] - (2 * cm) 95 96 96 if logo : 97 try : 97 try : 98 98 imglogo = PIL.Image.open(logo) 99 except : 99 except : 100 100 self.printInfo("Unable to open image %s" % logo, "warn") 101 101 else : 102 102 (width, height) = imglogo.size 103 multi = float(width) / (8 * cm) 103 multi = float(width) / (8 * cm) 104 104 width = float(width) / multi 105 105 height = float(height) / multi … … 107 107 ypos -= height 108 108 c.drawImage(logo, xpos, ypos, width, height) 109 109 110 110 # New top 111 111 xpos = pagesize[0] / 5.0 112 112 ypos -= (1 * cm) + 20 113 113 114 114 printername = self.getVar("PYKOTAPRINTERNAME") 115 115 username = self.getVar("PYKOTAUSERNAME") 116 116 accountbanner = self.config.getAccountBanner(printername) 117 117 118 118 # Outputs the username 119 ypos -= self.printVar(c, xcenter, ypos, _("Username"), username, 20, savetoner) 120 121 # Text 119 ypos -= self.printVar(c, xcenter, ypos, _("Username"), username, 20, savetoner) 120 121 # Text 122 122 if text : 123 ypos -= self.printVar(c, xcenter, ypos, _("More Info"), text, 20, savetoner) 124 123 ypos -= self.printVar(c, xcenter, ypos, _("More Info"), text, 20, savetoner) 124 125 125 # Printer and Job Id 126 126 job = "%s - %s" % (printername, self.getVar("PYKOTAJOBID")) 127 ypos -= self.printVar(c, xcenter, ypos, _("Job"), job, 14, savetoner) 128 127 ypos -= self.printVar(c, xcenter, ypos, _("Job"), job, 14, savetoner) 128 129 129 # Current date (TODO : at the time the banner was printed ! Change this to job's submission date) 130 130 datetime = time.strftime("%c", time.localtime()).decode(self.charset, "replace") 131 ypos -= self.printVar(c, xcenter, ypos, _("Date"), datetime, 14, savetoner) 132 131 ypos -= self.printVar(c, xcenter, ypos, _("Date"), datetime, 14, savetoner) 132 133 133 # Result of the print job 134 134 action = self.getVar("PYKOTAACTION") 135 135 if action == "ALLOW" : 136 136 action = _("Allowed") 137 elif action == "DENY" : 137 elif action == "DENY" : 138 138 action = _("Denied") 139 elif action == "WARN" : 139 elif action == "WARN" : 140 140 action = _("Allowed with Warning") 141 elif action == "PROBLEM" : 141 elif action == "PROBLEM" : 142 142 # should never occur 143 143 action = _("Problem") 144 elif action == "CANCEL" : 144 elif action == "CANCEL" : 145 145 # should never occur 146 146 action = _("Cancelled") 147 ypos -= self.printVar(c, xcenter, ypos, _("Result"), action, 14, savetoner) 148 147 ypos -= self.printVar(c, xcenter, ypos, _("Result"), action, 14, savetoner) 148 149 149 # skip some space 150 150 ypos -= 20 151 151 152 152 # Outputs title and filename 153 153 # We put them at x=0.25*pagewidth so that the line is long enough to hold them 154 154 title = self.getVar("PYKOTATITLE") 155 ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Title"), title, 10, savetoner) 156 155 ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Title"), title, 10, savetoner) 156 157 157 filename = self.getVar("PYKOTAFILENAME") 158 ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Filename"), filename, 10, savetoner) 159 158 ypos -= self.printVar(c, xcenter / 2.0, ypos, _("Filename"), filename, 10, savetoner) 159 160 160 # skip some space 161 161 ypos -= 20 162 162 163 163 # Now outputs the user's account balance or page counter 164 ypos -= self.printVar(c, xcenter, ypos, _("Pages printed so far on %s") % printername, self.getVar("PYKOTAPAGECOUNTER"), 14, savetoner) 164 ypos -= self.printVar(c, xcenter, ypos, _("Pages printed so far on %s") % printername, self.getVar("PYKOTAPAGECOUNTER"), 14, savetoner) 165 165 limitby = self.getVar("PYKOTALIMITBY") 166 if limitby == "balance" : 167 ypos -= self.printVar(c, xcenter, ypos, _("Account balance"), self.getVar("PYKOTABALANCE"), 14, savetoner) 166 if limitby == "balance" : 167 ypos -= self.printVar(c, xcenter, ypos, _("Account balance"), self.getVar("PYKOTABALANCE"), 14, savetoner) 168 168 elif limitby == "quota" : 169 ypos -= self.printVar(c, xcenter, ypos, _("Soft Limit"), self.getVar("PYKOTASOFTLIMIT"), 14, savetoner) 170 ypos -= self.printVar(c, xcenter, ypos, _("Hard Limit"), self.getVar("PYKOTAHARDLIMIT"), 14, savetoner) 171 ypos -= self.printVar(c, xcenter, ypos, _("Date Limit"), self.getVar("PYKOTADATELIMIT"), 14, savetoner) 169 ypos -= self.printVar(c, xcenter, ypos, _("Soft Limit"), self.getVar("PYKOTASOFTLIMIT"), 14, savetoner) 170 ypos -= self.printVar(c, xcenter, ypos, _("Hard Limit"), self.getVar("PYKOTAHARDLIMIT"), 14, savetoner) 171 ypos -= self.printVar(c, xcenter, ypos, _("Date Limit"), self.getVar("PYKOTADATELIMIT"), 14, savetoner) 172 172 else : 173 173 if limitby == "noquota" : 174 174 msg = _("No Limit") 175 elif limitby == "nochange" : 175 elif limitby == "nochange" : 176 176 msg = _("No Accounting") 177 elif limitby == "noprint" : 177 elif limitby == "noprint" : 178 178 msg = _("Forbidden") 179 else : 179 else : 180 180 msg = _("Unknown") 181 181 ypos -= self.printVar(c, xcenter, ypos, _("Printing Mode"), msg, 14, savetoner) 182 182 183 183 # URL 184 184 if url : … … 189 189 c.drawCentredString(xcenter, 2 * cm, url) 190 190 c.restoreState() 191 191 192 192 c.showPage() 193 193 c.save() 194 194 return document.getvalue() 195 195 196 196 def main(self, arguments, options) : 197 197 """Generates a banner.""" … … 200 200 if not hasPIL : 201 201 raise PyKotaToolError, "The Python Imaging Library is missing. Download it from http://www.pythonware.com/downloads" 202 203 self.logdebug("Generating the banner in PDF format...") 202 203 self.logdebug("Generating the banner in PDF format...") 204 204 doc = self.genPDF(getPageSize(options.pagesize), 205 options.logo.strip().encode(sys.getfilesystemencoding(), "replace"), 206 options.url.strip(), 207 " ".join(arguments).strip(), 205 options.logo.strip().encode(sys.getfilesystemencoding(), "replace"), 206 options.url.strip(), 207 " ".join(arguments).strip(), 208 208 options.savetoner / 100.0) 209 210 self.logdebug("Converting the banner to PostScript...") 209 210 self.logdebug("Converting the banner to PostScript...") 211 211 command = "gs -q -dNOPAUSE -dBATCH -dPARANOIDSAFER -sDEVICE=pswrite -sOutputFile=- -" 212 212 subpr = subprocess.Popen(command, … … 215 215 stdout=subprocess.PIPE, 216 216 stderr=subprocess.PIPE) 217 try : 217 try : 218 218 (out, err) = subpr.communicate(doc) 219 except OSError, msg : 219 except OSError, msg : 220 220 raise PyKotaToolError, _("Impossible to execute '%(command)s'") % locals() 221 221 status = subpr.wait() … … 255 255 parser.add_example('--logo="" --savetoner=75', 256 256 _("This would generate a banner in the default page size, with no logo, and text luminosity would be increased by 75%.")) 257 257 258 258 run(parser, PyKotaBanner)