root / pykota / trunk / bin / pykotme @ 1488

Revision 1488, 6.8 kB (checked in by jalet, 20 years ago)

Now pykotme doesn't spawn a new process anymore to compute job's size, but
use the PDLAnalyzer class directly

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