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