[1057] | 1 | #! /usr/bin/env python |
---|
[3260] | 2 | # -*- coding: UTF-8 -*- |
---|
[1057] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[1057] | 5 | # |
---|
[3275] | 6 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[1057] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1057] | 10 | # (at your option) any later version. |
---|
[3260] | 11 | # |
---|
[1057] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1057] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[2028] | 22 | # |
---|
[1057] | 23 | |
---|
| 24 | import sys |
---|
| 25 | import os |
---|
| 26 | import pwd |
---|
| 27 | |
---|
[3294] | 28 | import pykota.appinit |
---|
| 29 | from pykota.utils import * |
---|
| 30 | |
---|
[3288] | 31 | from pykota.errors import PyKotaCommandLineError |
---|
[3295] | 32 | from pykota.tool import PyKotaTool |
---|
[3245] | 33 | from pykota.accounter import openAccounter |
---|
[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("-") |
---|
[1099] | 85 | |
---|
[2232] | 86 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 87 | if not printers : |
---|
[2512] | 88 | raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"] |
---|
[2232] | 89 | |
---|
| 90 | username = pwd.getpwuid(os.getuid())[0] |
---|
[1488] | 91 | user = self.storage.getUser(username) |
---|
| 92 | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
---|
| 93 | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
---|
| 94 | |
---|
[2232] | 95 | if user.Exists : |
---|
[3245] | 96 | sizeprinted = False |
---|
| 97 | done = {} |
---|
| 98 | for printer in printers : |
---|
| 99 | # Now fake some values. TODO : improve API to not need this anymore |
---|
| 100 | self.PrinterName = printer.Name |
---|
| 101 | self.JobSizeBytes = 1 |
---|
| 102 | self.preaccounter = openAccounter(self, ispreaccounter=1) |
---|
| 103 | key = self.preaccounter.name + self.preaccounter.arguments |
---|
| 104 | if not done.has_key(key) : |
---|
| 105 | totalsize = 0 |
---|
| 106 | inkusage = [] |
---|
| 107 | for filename in files : |
---|
| 108 | self.DataFile = filename |
---|
| 109 | self.preaccounter.beginJob(None) |
---|
| 110 | self.preaccounter.endJob(None) |
---|
| 111 | totalsize += self.preaccounter.getJobSize(None) |
---|
| 112 | inkusage.extend(self.preaccounter.inkUsage) |
---|
| 113 | done[key] = (totalsize, inkusage) |
---|
| 114 | (totalsize, inkusage) = done[key] |
---|
| 115 | if not sizeprinted : |
---|
| 116 | print _("Job size : %i pages") % totalsize |
---|
| 117 | sizeprinted = True |
---|
| 118 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 119 | if userpquota.Exists : |
---|
| 120 | if printer.MaxJobSize and (totalsize > printer.MaxJobSize) : |
---|
| 121 | print _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name |
---|
| 122 | else : |
---|
| 123 | cost = userpquota.computeJobPrice(totalsize, inkusage) |
---|
| 124 | msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
| 125 | if printer.PassThrough : |
---|
| 126 | msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) |
---|
| 127 | elif user.LimitBy == "nochange" : |
---|
| 128 | msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) |
---|
| 129 | print msg |
---|
[2524] | 130 | if user.LimitBy == "noprint" : |
---|
| 131 | print _("Your account settings forbid you to print at this time.") |
---|
[1488] | 132 | |
---|
[1057] | 133 | if __name__ == "__main__" : |
---|
[1113] | 134 | retcode = 0 |
---|
[1057] | 135 | try : |
---|
| 136 | defaults = { \ |
---|
| 137 | "printer" : "*", \ |
---|
| 138 | } |
---|
| 139 | short_options = "vhP:" |
---|
| 140 | long_options = ["help", "version", "printer="] |
---|
| 141 | |
---|
| 142 | # Initializes the command line tool |
---|
| 143 | sender = PyKotMe(doc=__doc__) |
---|
[2210] | 144 | sender.deferredInit() |
---|
[1057] | 145 | |
---|
| 146 | # parse and checks the command line |
---|
| 147 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 148 | |
---|
| 149 | # sets long options |
---|
| 150 | options["help"] = options["h"] or options["help"] |
---|
| 151 | options["version"] = options["v"] or options["version"] |
---|
| 152 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 153 | |
---|
| 154 | if options["help"] : |
---|
| 155 | sender.display_usage_and_quit() |
---|
| 156 | elif options["version"] : |
---|
| 157 | sender.display_version_and_quit() |
---|
| 158 | else : |
---|
[1113] | 159 | retcode = sender.main(args, options) |
---|
[2216] | 160 | except KeyboardInterrupt : |
---|
[3294] | 161 | logerr("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 162 | retcode = -3 |
---|
[2512] | 163 | except PyKotaCommandLineError, msg : |
---|
[3294] | 164 | logerr("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 165 | retcode = -2 |
---|
[1526] | 166 | except SystemExit : |
---|
| 167 | pass |
---|
[1517] | 168 | except : |
---|
| 169 | try : |
---|
| 170 | sender.crashed("pykotme failed") |
---|
| 171 | except : |
---|
[1546] | 172 | crashed("pykotme failed") |
---|
[1113] | 173 | retcode = -1 |
---|
[1057] | 174 | |
---|
[1113] | 175 | try : |
---|
| 176 | sender.storage.close() |
---|
| 177 | except (TypeError, NameError, AttributeError) : |
---|
| 178 | pass |
---|
| 179 | |
---|
| 180 | sys.exit(retcode) |
---|