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