root / pykota / trunk / bin / pykotme @ 1471

Revision 1463, 6.8 kB (checked in by jalet, 21 years ago)

pykotme now uses pkpgcounter to compute the job's size.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Quote sender
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
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#
25# $Log$
26# Revision 1.8  2004/05/10 07:23:21  jalet
27# pykotme now uses pkpgcounter to compute the job's size.
28#
29# Revision 1.7  2004/01/12 22:43:40  jalet
30# New formula to compute a job's price
31#
32# Revision 1.6  2004/01/08 14:10:32  jalet
33# Copyright year changed.
34#
35# Revision 1.5  2003/10/09 21:25:25  jalet
36# Multiple printer names or wildcards can be passed on the command line
37# separated with commas.
38# Beta phase.
39#
40# Revision 1.4  2003/10/07 09:07:27  jalet
41# Character encoding added to please latest version of Python
42#
43# Revision 1.3  2003/07/29 20:55:17  jalet
44# 1.14 is out !
45#
46# Revision 1.2  2003/07/25 10:41:29  jalet
47# Better documentation.
48# pykotme now displays the current user's account balance.
49# Some test changed in ldap module.
50#
51# Revision 1.1  2003/07/03 09:44:01  jalet
52# Now includes the pykotme utility
53#
54#
55#
56
57import sys
58import os
59import pwd
60
61from pykota import version
62from pykota.tool import PyKotaTool, PyKotaToolError
63from pykota.config import PyKotaConfigError
64from pykota.storage import PyKotaStorageError
65
66__doc__ = """pykotme v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
67
68Gives print quotes to users.
69
70command line usage :
71
72  pykotme  [options]  [files]
73
74options :
75
76  -v | --version       Prints pykotme's version number then exits.
77  -h | --help          Prints this message then exits.
78 
79  -P | --printer p     Gives a quote for this printer only. Actually p can
80                       use wildcards characters to select only
81                       some printers. The default value is *, meaning
82                       all printers.
83                       You can specify several names or wildcards,
84                       by separating them with commas.
85 
86examples :                             
87
88  $ pykotme --printer apple file1.ps file2.ps
89 
90  This will give a print quote to the current user. The quote will show
91  the price and size of a job consisting in file1.ps and file2.ps
92  which would be sent to the apple printer.
93 
94  $ pykotme --printer apple,hplaser <file1.ps
95 
96  This will give a print quote to the current user. The quote will show
97  the price and size of a job consisting in file1.ps as read from
98  standard input, which would be sent to the apple or hplaser
99  printer.
100
101  $ pykotme
102 
103  This will give a quote for a job consisting of what is on standard
104  input. The quote will list the job size, and the price the job
105  would cost on each printer.
106
107
108This program is free software; you can redistribute it and/or modify
109it under the terms of the GNU General Public License as published by
110the Free Software Foundation; either version 2 of the License, or
111(at your option) any later version.
112
113This program is distributed in the hope that it will be useful,
114but WITHOUT ANY WARRANTY; without even the implied warranty of
115MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
116GNU General Public License for more details.
117
118You should have received a copy of the GNU General Public License
119along with this program; if not, write to the Free Software
120Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
121
122Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
123       
124class PyKotMe(PyKotaTool) :       
125    """A class for pykotme."""
126    def main(self, files, options) :
127        """Gives print quotes."""
128        if (not sys.stdin.isatty()) and ("-" not in files) :
129            files.append("-")
130        pipe = os.popen("pkpgcounter %s" % " ".join([' "%s"' % f for f in files]))
131        answer = pipe.readline().strip()
132        status = pipe.close()
133        try :
134            if status is None :
135                nbpages = int(answer)
136            else :       
137                raise ValueError, "ERROR in pkpgcounter" 
138        except ValueError, msg :   
139            sys.stderr.write(_("Unable to compute size of %s in number of pages.\n") % files)
140            sys.stderr.write("%s\n" % msg)
141        else :   
142            # get current user
143            uid = os.geteuid()
144            username = pwd.getpwuid(uid)[0]
145            user = self.storage.getUser(username)
146            if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"):
147                print _("Your account balance : %.2f") % (user.AccountBalance or 0.0)
148               
149            printers = self.storage.getMatchingPrinters(options["printer"])
150            if not printers :
151                raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
152               
153            print _("Job size : %i pages") % nbpages   
154            for printer in printers :
155                userpquota = self.storage.getUserPQuota(user, printer)
156                cost = userpquota.computeJobPrice(nbpages)
157                print _("Cost on printer %s : %.2f") % (printer.Name, cost)
158           
159if __name__ == "__main__" : 
160    retcode = 0
161    try :
162        defaults = { \
163                     "printer" : "*", \
164                   }
165        short_options = "vhP:"
166        long_options = ["help", "version", "printer="]
167       
168        # Initializes the command line tool
169        sender = PyKotMe(doc=__doc__)
170       
171        # parse and checks the command line
172        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
173       
174        # sets long options
175        options["help"] = options["h"] or options["help"]
176        options["version"] = options["v"] or options["version"]
177        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
178       
179        if options["help"] :
180            sender.display_usage_and_quit()
181        elif options["version"] :
182            sender.display_version_and_quit()
183        else :
184            retcode = sender.main(args, options)
185    except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg :           
186        sys.stderr.write("%s\n" % msg)
187        sys.stderr.flush()
188        retcode = -1
189
190    try :
191        sender.storage.close()
192    except (TypeError, NameError, AttributeError) :   
193        pass
194       
195    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.