[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 | # |
---|
[2028] | 25 | # |
---|
[1057] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import pwd |
---|
| 30 | |
---|
[1796] | 31 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1488] | 32 | from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError |
---|
[1057] | 33 | |
---|
[2267] | 34 | __doc__ = N_("""pykotme v%s (c) %s %s |
---|
[1057] | 35 | |
---|
| 36 | Gives print quotes to users. |
---|
| 37 | |
---|
| 38 | command line usage : |
---|
| 39 | |
---|
| 40 | pykotme [options] [files] |
---|
| 41 | |
---|
| 42 | options : |
---|
| 43 | |
---|
| 44 | -v | --version Prints pykotme's version number then exits. |
---|
| 45 | -h | --help Prints this message then exits. |
---|
| 46 | |
---|
| 47 | -P | --printer p Gives a quote for this printer only. Actually p can |
---|
| 48 | use wildcards characters to select only |
---|
| 49 | some printers. The default value is *, meaning |
---|
| 50 | all printers. |
---|
[1156] | 51 | You can specify several names or wildcards, |
---|
| 52 | by separating them with commas. |
---|
[1057] | 53 | |
---|
| 54 | examples : |
---|
| 55 | |
---|
| 56 | $ pykotme --printer apple file1.ps file2.ps |
---|
| 57 | |
---|
| 58 | This will give a print quote to the current user. The quote will show |
---|
| 59 | the price and size of a job consisting in file1.ps and file2.ps |
---|
| 60 | which would be sent to the apple printer. |
---|
[1156] | 61 | |
---|
| 62 | $ pykotme --printer apple,hplaser <file1.ps |
---|
| 63 | |
---|
| 64 | This will give a print quote to the current user. The quote will show |
---|
| 65 | the price and size of a job consisting in file1.ps as read from |
---|
| 66 | standard input, which would be sent to the apple or hplaser |
---|
| 67 | printer. |
---|
[1057] | 68 | |
---|
| 69 | $ pykotme |
---|
| 70 | |
---|
| 71 | This will give a quote for a job consisting of what is on standard |
---|
| 72 | input. The quote will list the job size, and the price the job |
---|
| 73 | would cost on each printer. |
---|
| 74 | |
---|
| 75 | This program is free software; you can redistribute it and/or modify |
---|
| 76 | it under the terms of the GNU General Public License as published by |
---|
| 77 | the Free Software Foundation; either version 2 of the License, or |
---|
| 78 | (at your option) any later version. |
---|
| 79 | |
---|
| 80 | This program is distributed in the hope that it will be useful, |
---|
| 81 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 82 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 83 | GNU General Public License for more details. |
---|
| 84 | |
---|
| 85 | You should have received a copy of the GNU General Public License |
---|
| 86 | along with this program; if not, write to the Free Software |
---|
| 87 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 88 | |
---|
[1803] | 89 | Please e-mail bugs to: %s""") |
---|
[1057] | 90 | |
---|
[1488] | 91 | |
---|
[1057] | 92 | class PyKotMe(PyKotaTool) : |
---|
| 93 | """A class for pykotme.""" |
---|
| 94 | def main(self, files, options) : |
---|
| 95 | """Gives print quotes.""" |
---|
[1463] | 96 | if (not sys.stdin.isatty()) and ("-" not in files) : |
---|
| 97 | files.append("-") |
---|
[1488] | 98 | totalsize = 0 |
---|
| 99 | for filename in files : |
---|
| 100 | try : |
---|
| 101 | parser = PDLAnalyzer(filename) |
---|
| 102 | totalsize += parser.getJobSize() |
---|
| 103 | except PDLAnalyzerError, msg : |
---|
[1584] | 104 | self.printInfo(msg) |
---|
[1099] | 105 | |
---|
[2232] | 106 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 107 | if not printers : |
---|
| 108 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
| 109 | |
---|
| 110 | username = pwd.getpwuid(os.getuid())[0] |
---|
[1488] | 111 | user = self.storage.getUser(username) |
---|
| 112 | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
---|
| 113 | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
---|
| 114 | |
---|
| 115 | print _("Job size : %i pages") % totalsize |
---|
[2232] | 116 | if user.Exists : |
---|
| 117 | for printer in printers : |
---|
| 118 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 119 | if userpquota.Exists : |
---|
| 120 | cost = userpquota.computeJobPrice(totalsize) |
---|
| 121 | print _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
[1488] | 122 | |
---|
[1057] | 123 | if __name__ == "__main__" : |
---|
[1113] | 124 | retcode = 0 |
---|
[1057] | 125 | try : |
---|
| 126 | defaults = { \ |
---|
| 127 | "printer" : "*", \ |
---|
| 128 | } |
---|
| 129 | short_options = "vhP:" |
---|
| 130 | long_options = ["help", "version", "printer="] |
---|
| 131 | |
---|
| 132 | # Initializes the command line tool |
---|
| 133 | sender = PyKotMe(doc=__doc__) |
---|
[2210] | 134 | sender.deferredInit() |
---|
[1057] | 135 | |
---|
| 136 | # parse and checks the command line |
---|
| 137 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 138 | |
---|
| 139 | # sets long options |
---|
| 140 | options["help"] = options["h"] or options["help"] |
---|
| 141 | options["version"] = options["v"] or options["version"] |
---|
| 142 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 143 | |
---|
| 144 | if options["help"] : |
---|
| 145 | sender.display_usage_and_quit() |
---|
| 146 | elif options["version"] : |
---|
| 147 | sender.display_version_and_quit() |
---|
| 148 | else : |
---|
[1113] | 149 | retcode = sender.main(args, options) |
---|
[2216] | 150 | except KeyboardInterrupt : |
---|
| 151 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[1526] | 152 | except SystemExit : |
---|
| 153 | pass |
---|
[1517] | 154 | except : |
---|
| 155 | try : |
---|
| 156 | sender.crashed("pykotme failed") |
---|
| 157 | except : |
---|
[1546] | 158 | crashed("pykotme failed") |
---|
[1113] | 159 | retcode = -1 |
---|
[1057] | 160 | |
---|
[1113] | 161 | try : |
---|
| 162 | sender.storage.close() |
---|
| 163 | except (TypeError, NameError, AttributeError) : |
---|
| 164 | pass |
---|
| 165 | |
---|
| 166 | sys.exit(retcode) |
---|