#! /usr/bin/env python # -*- coding: utf-8 -*- # # PyKota : Print Quotas for CUPS # # (c) 2003-2013 Jerome Alet # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # $Id: pykotme 3561 2013-01-04 22:34:24Z jerome $ # # """A print quote generator for PyKota""" import sys import os import pwd import pykota.appinit from pykota.utils import run from pykota.commandline import PyKotaOptionParser from pykota.errors import PyKotaCommandLineError from pykota.tool import PyKotaTool from pykota.accounter import openAccounter class PyKotMe(PyKotaTool) : """A class for pykotme.""" def main(self, files, options) : """Gives print quotes.""" if (not sys.stdin.isatty()) and ("-" not in files) : # TODO : create a named temporary file for standard input # TODO : because if there are several printers on the command # TODO : line and they have different preaccounter directives, # TODO : the standard input will already be consumed when looping # TODO : over the second printer below. files.append("-") printers = self.storage.getMatchingPrinters(options.printer) if not printers : raise PyKotaCommandLineError, _("There's no printer matching %s") % options.printer username = pwd.getpwuid(os.getuid())[0] if options.user : if not self.config.isAdmin : self.printInfo(_("The --user command line option will be ignored because you are not a PyKota Administrator."), "warn") else : username = options.user user = self.storage.getUser(username) if not user.Exists : self.printInfo(_("There's no user matching '%(username)s'.") \ % locals(), "error") else : if user.LimitBy and (user.LimitBy.lower() == "balance"): self.display("%s\n" % (_("Your account balance : %.2f") % (user.AccountBalance or 0.0))) sizeprinted = False done = {} for printer in printers : # Now fake some values. TODO : improve API to not need this anymore printername = printer.Name self.PrinterName = printer.Name self.JobSizeBytes = 1 self.preaccounter = openAccounter(self, ispreaccounter=1) key = self.preaccounter.name + self.preaccounter.arguments if not done.has_key(key) : totalsize = 0 inkusage = [] for filename in files : self.DataFile = filename self.preaccounter.beginJob(None) self.preaccounter.endJob(None) totalsize += self.preaccounter.getJobSize(None) inkusage.extend(self.preaccounter.inkUsage) done[key] = (totalsize, inkusage) (totalsize, inkusage) = done[key] if not sizeprinted : self.display("%s\n" % (_("Job size : %i pages") % totalsize)) sizeprinted = True userpquota = self.storage.getUserPQuota(user, printer) if userpquota.Exists : if (printer.MaxJobSize and (totalsize > printer.MaxJobSize)) \ or (userpquota.MaxJobSize and (totalsize > userpquota.MaxJobSize)) : self.display("%s\n" % (_("User %(username)s is not allowed to print so many pages on printer %(printername)s at this time.") % locals())) else : cost = userpquota.computeJobPrice(totalsize, inkusage) msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) if printer.PassThrough : msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) elif user.LimitBy == "nochange" : msg = "%s (%s)" % (msg, _("won't be charged, account is immutable")) self.display("%s\n" % msg) if user.LimitBy == "noprint" : self.display("%s\n" % (_("User %(username)s is forbidden to print at this time.") % locals())) if __name__ == "__main__" : parser = PyKotaOptionParser(description=_("Generates print quotes for end users."), usage="pykotme [options] [files]") parser.add_option("-P", "--printer", dest="printer", default="*", help=_("Acts on this printer only. You can specify several printer names by separating them with commas. The default value is '%default', which means all printers.")) parser.add_option("-u", "--user", dest="user", help=_("Acts on this user only. Only one username can be specified this way. The default value is the name of the user who launched this command. This option is ignored when the command is not launched by a PyKota Administrator.")) parser.add_example("--printer apple file1.ps