[1057] | 1 | #! /usr/bin/env python |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1057] | 3 | |
---|
| 4 | # PyKota Print Quota Quote sender |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
[2028] | 8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[1057] | 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$ |
---|
[2028] | 26 | # Revision 1.18 2005/01/17 08:44:24 jalet |
---|
| 27 | # Modified copyright years |
---|
| 28 | # |
---|
[1911] | 29 | # Revision 1.17 2004/11/12 23:46:44 jalet |
---|
| 30 | # Heavy work on pkbanner. Not finished yet though, but mostly works. |
---|
| 31 | # |
---|
[1803] | 32 | # Revision 1.16 2004/10/11 22:53:05 jalet |
---|
| 33 | # Postponed string interpolation to help message's output method |
---|
| 34 | # |
---|
[1796] | 35 | # Revision 1.15 2004/10/11 12:49:06 jalet |
---|
| 36 | # Renders help translatable |
---|
| 37 | # |
---|
[1785] | 38 | # Revision 1.14 2004/10/06 10:05:47 jalet |
---|
| 39 | # Minor changes to allow any PyKota administrator to launch enhanced versions |
---|
| 40 | # of the commands, and not only the root user. |
---|
| 41 | # |
---|
[1584] | 42 | # Revision 1.13 2004/07/01 19:56:42 jalet |
---|
| 43 | # Better dispatching of error messages |
---|
| 44 | # |
---|
[1546] | 45 | # Revision 1.12 2004/06/18 13:34:49 jalet |
---|
| 46 | # Now all tracebacks include PyKota's version number |
---|
| 47 | # |
---|
[1526] | 48 | # Revision 1.11 2004/06/07 18:43:40 jalet |
---|
| 49 | # Fixed over-verbose exits when displaying help or version number |
---|
| 50 | # |
---|
[1517] | 51 | # Revision 1.10 2004/06/03 21:50:34 jalet |
---|
| 52 | # Improved error logging. |
---|
| 53 | # crashrecipient directive added. |
---|
| 54 | # Now exports the job's size in bytes too. |
---|
| 55 | # |
---|
[1488] | 56 | # Revision 1.9 2004/05/21 20:53:34 jalet |
---|
| 57 | # Now pykotme doesn't spawn a new process anymore to compute job's size, but |
---|
| 58 | # use the PDLAnalyzer class directly |
---|
| 59 | # |
---|
[1463] | 60 | # Revision 1.8 2004/05/10 07:23:21 jalet |
---|
| 61 | # pykotme now uses pkpgcounter to compute the job's size. |
---|
| 62 | # |
---|
[1285] | 63 | # Revision 1.7 2004/01/12 22:43:40 jalet |
---|
| 64 | # New formula to compute a job's price |
---|
| 65 | # |
---|
[1257] | 66 | # Revision 1.6 2004/01/08 14:10:32 jalet |
---|
| 67 | # Copyright year changed. |
---|
| 68 | # |
---|
[1156] | 69 | # Revision 1.5 2003/10/09 21:25:25 jalet |
---|
| 70 | # Multiple printer names or wildcards can be passed on the command line |
---|
| 71 | # separated with commas. |
---|
| 72 | # Beta phase. |
---|
| 73 | # |
---|
[1144] | 74 | # Revision 1.4 2003/10/07 09:07:27 jalet |
---|
| 75 | # Character encoding added to please latest version of Python |
---|
| 76 | # |
---|
[1113] | 77 | # Revision 1.3 2003/07/29 20:55:17 jalet |
---|
| 78 | # 1.14 is out ! |
---|
| 79 | # |
---|
[1099] | 80 | # Revision 1.2 2003/07/25 10:41:29 jalet |
---|
| 81 | # Better documentation. |
---|
| 82 | # pykotme now displays the current user's account balance. |
---|
| 83 | # Some test changed in ldap module. |
---|
| 84 | # |
---|
[1057] | 85 | # Revision 1.1 2003/07/03 09:44:01 jalet |
---|
| 86 | # Now includes the pykotme utility |
---|
| 87 | # |
---|
| 88 | # |
---|
| 89 | # |
---|
| 90 | |
---|
| 91 | import sys |
---|
| 92 | import os |
---|
| 93 | import pwd |
---|
| 94 | |
---|
[1796] | 95 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1488] | 96 | from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError |
---|
[1057] | 97 | |
---|
[2028] | 98 | __doc__ = N_("""pykotme v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
[1057] | 99 | |
---|
| 100 | Gives print quotes to users. |
---|
| 101 | |
---|
| 102 | command line usage : |
---|
| 103 | |
---|
| 104 | pykotme [options] [files] |
---|
| 105 | |
---|
| 106 | options : |
---|
| 107 | |
---|
| 108 | -v | --version Prints pykotme's version number then exits. |
---|
| 109 | -h | --help Prints this message then exits. |
---|
| 110 | |
---|
| 111 | -P | --printer p Gives a quote for this printer only. Actually p can |
---|
| 112 | use wildcards characters to select only |
---|
| 113 | some printers. The default value is *, meaning |
---|
| 114 | all printers. |
---|
[1156] | 115 | You can specify several names or wildcards, |
---|
| 116 | by separating them with commas. |
---|
[1057] | 117 | |
---|
| 118 | examples : |
---|
| 119 | |
---|
| 120 | $ pykotme --printer apple file1.ps file2.ps |
---|
| 121 | |
---|
| 122 | This will give a print quote to the current user. The quote will show |
---|
| 123 | the price and size of a job consisting in file1.ps and file2.ps |
---|
| 124 | which would be sent to the apple printer. |
---|
[1156] | 125 | |
---|
| 126 | $ pykotme --printer apple,hplaser <file1.ps |
---|
| 127 | |
---|
| 128 | This will give a print quote to the current user. The quote will show |
---|
| 129 | the price and size of a job consisting in file1.ps as read from |
---|
| 130 | standard input, which would be sent to the apple or hplaser |
---|
| 131 | printer. |
---|
[1057] | 132 | |
---|
| 133 | $ pykotme |
---|
| 134 | |
---|
| 135 | This will give a quote for a job consisting of what is on standard |
---|
| 136 | input. The quote will list the job size, and the price the job |
---|
| 137 | would cost on each printer. |
---|
| 138 | |
---|
| 139 | This program is free software; you can redistribute it and/or modify |
---|
| 140 | it under the terms of the GNU General Public License as published by |
---|
| 141 | the Free Software Foundation; either version 2 of the License, or |
---|
| 142 | (at your option) any later version. |
---|
| 143 | |
---|
| 144 | This program is distributed in the hope that it will be useful, |
---|
| 145 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 146 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 147 | GNU General Public License for more details. |
---|
| 148 | |
---|
| 149 | You should have received a copy of the GNU General Public License |
---|
| 150 | along with this program; if not, write to the Free Software |
---|
| 151 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 152 | |
---|
[1803] | 153 | Please e-mail bugs to: %s""") |
---|
[1057] | 154 | |
---|
[1488] | 155 | |
---|
[1057] | 156 | class PyKotMe(PyKotaTool) : |
---|
| 157 | """A class for pykotme.""" |
---|
| 158 | def main(self, files, options) : |
---|
| 159 | """Gives print quotes.""" |
---|
[1463] | 160 | if (not sys.stdin.isatty()) and ("-" not in files) : |
---|
| 161 | files.append("-") |
---|
[1488] | 162 | totalsize = 0 |
---|
| 163 | for filename in files : |
---|
| 164 | try : |
---|
| 165 | parser = PDLAnalyzer(filename) |
---|
| 166 | totalsize += parser.getJobSize() |
---|
| 167 | except PDLAnalyzerError, msg : |
---|
[1584] | 168 | self.printInfo(msg) |
---|
[1099] | 169 | |
---|
[1488] | 170 | # get current user |
---|
[1785] | 171 | username = pwd.getpwuid(os.geteuid())[0] |
---|
[1488] | 172 | user = self.storage.getUser(username) |
---|
| 173 | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
---|
| 174 | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
---|
| 175 | |
---|
| 176 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 177 | if not printers : |
---|
| 178 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
| 179 | |
---|
| 180 | print _("Job size : %i pages") % totalsize |
---|
| 181 | for printer in printers : |
---|
| 182 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 183 | cost = userpquota.computeJobPrice(totalsize) |
---|
| 184 | print _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
| 185 | |
---|
[1057] | 186 | if __name__ == "__main__" : |
---|
[1113] | 187 | retcode = 0 |
---|
[1057] | 188 | try : |
---|
| 189 | defaults = { \ |
---|
| 190 | "printer" : "*", \ |
---|
| 191 | } |
---|
| 192 | short_options = "vhP:" |
---|
| 193 | long_options = ["help", "version", "printer="] |
---|
| 194 | |
---|
| 195 | # Initializes the command line tool |
---|
| 196 | sender = PyKotMe(doc=__doc__) |
---|
| 197 | |
---|
| 198 | # parse and checks the command line |
---|
| 199 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 200 | |
---|
| 201 | # sets long options |
---|
| 202 | options["help"] = options["h"] or options["help"] |
---|
| 203 | options["version"] = options["v"] or options["version"] |
---|
| 204 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 205 | |
---|
| 206 | if options["help"] : |
---|
| 207 | sender.display_usage_and_quit() |
---|
| 208 | elif options["version"] : |
---|
| 209 | sender.display_version_and_quit() |
---|
| 210 | else : |
---|
[1113] | 211 | retcode = sender.main(args, options) |
---|
[1526] | 212 | except SystemExit : |
---|
| 213 | pass |
---|
[1517] | 214 | except : |
---|
| 215 | try : |
---|
| 216 | sender.crashed("pykotme failed") |
---|
| 217 | except : |
---|
[1546] | 218 | crashed("pykotme failed") |
---|
[1113] | 219 | retcode = -1 |
---|
[1057] | 220 | |
---|
[1113] | 221 | try : |
---|
| 222 | sender.storage.close() |
---|
| 223 | except (TypeError, NameError, AttributeError) : |
---|
| 224 | pass |
---|
| 225 | |
---|
| 226 | sys.exit(retcode) |
---|