[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 | # |
---|
[2622] | 8 | # (c) 2003, 2004, 2005, 2006 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 |
---|
[2303] | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1057] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[1057] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import pwd |
---|
| 30 | |
---|
[2829] | 31 | from pykota.tool import PyKotaTool, PyKotaCommandLineError, crashed, N_ |
---|
[1057] | 32 | |
---|
[2909] | 33 | from pkpgpdls import analyzer, pdlparser |
---|
[2352] | 34 | |
---|
| 35 | |
---|
[2344] | 36 | __doc__ = N_("""pykotme v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[1057] | 37 | |
---|
| 38 | Gives print quotes to users. |
---|
| 39 | |
---|
| 40 | command line usage : |
---|
| 41 | |
---|
| 42 | pykotme [options] [files] |
---|
| 43 | |
---|
| 44 | options : |
---|
| 45 | |
---|
| 46 | -v | --version Prints pykotme's version number then exits. |
---|
| 47 | -h | --help Prints this message then exits. |
---|
| 48 | |
---|
| 49 | -P | --printer p Gives a quote for this printer only. Actually p can |
---|
| 50 | use wildcards characters to select only |
---|
| 51 | some printers. The default value is *, meaning |
---|
| 52 | all printers. |
---|
[1156] | 53 | You can specify several names or wildcards, |
---|
| 54 | by separating them with commas. |
---|
[1057] | 55 | |
---|
| 56 | examples : |
---|
| 57 | |
---|
| 58 | $ pykotme --printer apple file1.ps file2.ps |
---|
| 59 | |
---|
| 60 | This will give a print quote to the current user. The quote will show |
---|
| 61 | the price and size of a job consisting in file1.ps and file2.ps |
---|
| 62 | which would be sent to the apple printer. |
---|
[1156] | 63 | |
---|
| 64 | $ pykotme --printer apple,hplaser <file1.ps |
---|
| 65 | |
---|
| 66 | This will give a print quote to the current user. The quote will show |
---|
| 67 | the price and size of a job consisting in file1.ps as read from |
---|
| 68 | standard input, which would be sent to the apple or hplaser |
---|
| 69 | printer. |
---|
[1057] | 70 | |
---|
| 71 | $ pykotme |
---|
| 72 | |
---|
| 73 | This will give a quote for a job consisting of what is on standard |
---|
| 74 | input. The quote will list the job size, and the price the job |
---|
| 75 | would cost on each printer. |
---|
[2344] | 76 | """) |
---|
[1057] | 77 | |
---|
[1488] | 78 | |
---|
[1057] | 79 | class PyKotMe(PyKotaTool) : |
---|
| 80 | """A class for pykotme.""" |
---|
| 81 | def main(self, files, options) : |
---|
| 82 | """Gives print quotes.""" |
---|
[1463] | 83 | if (not sys.stdin.isatty()) and ("-" not in files) : |
---|
| 84 | files.append("-") |
---|
[1488] | 85 | totalsize = 0 |
---|
| 86 | for filename in files : |
---|
| 87 | try : |
---|
[2352] | 88 | parser = analyzer.PDLAnalyzer(filename) |
---|
[1488] | 89 | totalsize += parser.getJobSize() |
---|
[2975] | 90 | except (pdlparser.PDLParserError, IOError), msg : |
---|
[1584] | 91 | self.printInfo(msg) |
---|
[1099] | 92 | |
---|
[2232] | 93 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 94 | if not printers : |
---|
[2512] | 95 | raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"] |
---|
[2232] | 96 | |
---|
| 97 | username = pwd.getpwuid(os.getuid())[0] |
---|
[1488] | 98 | user = self.storage.getUser(username) |
---|
| 99 | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
---|
| 100 | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
---|
| 101 | |
---|
| 102 | print _("Job size : %i pages") % totalsize |
---|
[2232] | 103 | if user.Exists : |
---|
[2524] | 104 | if user.LimitBy == "noprint" : |
---|
| 105 | print _("Your account settings forbid you to print at this time.") |
---|
| 106 | else : |
---|
| 107 | for printer in printers : |
---|
| 108 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 109 | if userpquota.Exists : |
---|
| 110 | if printer.MaxJobSize and (totalsize > printer.MaxJobSize) : |
---|
| 111 | print _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name |
---|
| 112 | else : |
---|
| 113 | cost = userpquota.computeJobPrice(totalsize) |
---|
| 114 | msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
| 115 | if printer.PassThrough : |
---|
| 116 | msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) |
---|
| 117 | elif user.LimitBy == "nochange" : |
---|
| 118 | msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) |
---|
| 119 | print msg |
---|
[1488] | 120 | |
---|
[1057] | 121 | if __name__ == "__main__" : |
---|
[1113] | 122 | retcode = 0 |
---|
[1057] | 123 | try : |
---|
| 124 | defaults = { \ |
---|
| 125 | "printer" : "*", \ |
---|
| 126 | } |
---|
| 127 | short_options = "vhP:" |
---|
| 128 | long_options = ["help", "version", "printer="] |
---|
| 129 | |
---|
| 130 | # Initializes the command line tool |
---|
| 131 | sender = PyKotMe(doc=__doc__) |
---|
[2210] | 132 | sender.deferredInit() |
---|
[1057] | 133 | |
---|
| 134 | # parse and checks the command line |
---|
| 135 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 136 | |
---|
| 137 | # sets long options |
---|
| 138 | options["help"] = options["h"] or options["help"] |
---|
| 139 | options["version"] = options["v"] or options["version"] |
---|
| 140 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 141 | |
---|
| 142 | if options["help"] : |
---|
| 143 | sender.display_usage_and_quit() |
---|
| 144 | elif options["version"] : |
---|
| 145 | sender.display_version_and_quit() |
---|
| 146 | else : |
---|
[1113] | 147 | retcode = sender.main(args, options) |
---|
[2216] | 148 | except KeyboardInterrupt : |
---|
| 149 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 150 | retcode = -3 |
---|
[2512] | 151 | except PyKotaCommandLineError, msg : |
---|
| 152 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 153 | retcode = -2 |
---|
[1526] | 154 | except SystemExit : |
---|
| 155 | pass |
---|
[1517] | 156 | except : |
---|
| 157 | try : |
---|
| 158 | sender.crashed("pykotme failed") |
---|
| 159 | except : |
---|
[1546] | 160 | crashed("pykotme failed") |
---|
[1113] | 161 | retcode = -1 |
---|
[1057] | 162 | |
---|
[1113] | 163 | try : |
---|
| 164 | sender.storage.close() |
---|
| 165 | except (TypeError, NameError, AttributeError) : |
---|
| 166 | pass |
---|
| 167 | |
---|
| 168 | sys.exit(retcode) |
---|