root / pykota / trunk / bin / pykotme @ 1144

Revision 1144, 6.7 kB (checked in by jalet, 21 years ago)

Character encoding added to please latest version of Python

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