root / pykota / trunk / bin / pykotme @ 2344

Revision 2344, 5.1 kB (checked in by jerome, 19 years ago)

Moved the GPL blurb into a single location.
Now uses named parameters in commands' help.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[1057]1#! /usr/bin/env python
[1144]2# -*- coding: ISO-8859-15 -*-
[1057]3
4# PyKota Print Quota Quote sender
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
[2028]8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
[1057]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
[2303]21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[1057]22#
23# $Id$
24#
[2028]25#
[1057]26
27import sys
28import os
29import pwd
30
[1796]31from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
[1488]32from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError
[1057]33
[2344]34__doc__ = N_("""pykotme v%(__version__)s (c) %(__years__)s %(__author__)s
[1057]35
36Gives print quotes to users.
37
38command line usage :
39
40  pykotme  [options]  [files]
41
42options :
43
44  -v | --version       Prints pykotme's version number then exits.
45  -h | --help          Prints this message then exits.
46 
47  -P | --printer p     Gives a quote for this printer only. Actually p can
48                       use wildcards characters to select only
49                       some printers. The default value is *, meaning
50                       all printers.
[1156]51                       You can specify several names or wildcards,
52                       by separating them with commas.
[1057]53 
54examples :                             
55
56  $ pykotme --printer apple file1.ps file2.ps
57 
58  This will give a print quote to the current user. The quote will show
59  the price and size of a job consisting in file1.ps and file2.ps
60  which would be sent to the apple printer.
[1156]61 
62  $ pykotme --printer apple,hplaser <file1.ps
63 
64  This will give a print quote to the current user. The quote will show
65  the price and size of a job consisting in file1.ps as read from
66  standard input, which would be sent to the apple or hplaser
67  printer.
[1057]68
69  $ pykotme
70 
71  This will give a quote for a job consisting of what is on standard
72  input. The quote will list the job size, and the price the job
73  would cost on each printer.
[2344]74""")
[1057]75       
[1488]76       
[1057]77class PyKotMe(PyKotaTool) :       
78    """A class for pykotme."""
79    def main(self, files, options) :
80        """Gives print quotes."""
[1463]81        if (not sys.stdin.isatty()) and ("-" not in files) :
82            files.append("-")
[1488]83        totalsize = 0   
84        for filename in files :   
85            try :
86                parser = PDLAnalyzer(filename)
87                totalsize += parser.getJobSize()
88            except PDLAnalyzerError, msg :   
[1584]89                self.printInfo(msg)
[1099]90           
[2232]91        printers = self.storage.getMatchingPrinters(options["printer"])
92        if not printers :
93            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
94           
95        username = pwd.getpwuid(os.getuid())[0]
[1488]96        user = self.storage.getUser(username)
97        if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"):
98            print _("Your account balance : %.2f") % (user.AccountBalance or 0.0)
99           
100        print _("Job size : %i pages") % totalsize   
[2232]101        if user.Exists :
102            for printer in printers :
103                userpquota = self.storage.getUserPQuota(user, printer)
104                if userpquota.Exists :
105                    cost = userpquota.computeJobPrice(totalsize)
106                    print _("Cost on printer %s : %.2f") % (printer.Name, cost)
[1488]107           
[1057]108if __name__ == "__main__" : 
[1113]109    retcode = 0
[1057]110    try :
111        defaults = { \
112                     "printer" : "*", \
113                   }
114        short_options = "vhP:"
115        long_options = ["help", "version", "printer="]
116       
117        # Initializes the command line tool
118        sender = PyKotMe(doc=__doc__)
[2210]119        sender.deferredInit()
[1057]120       
121        # parse and checks the command line
122        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
123       
124        # sets long options
125        options["help"] = options["h"] or options["help"]
126        options["version"] = options["v"] or options["version"]
127        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
128       
129        if options["help"] :
130            sender.display_usage_and_quit()
131        elif options["version"] :
132            sender.display_version_and_quit()
133        else :
[1113]134            retcode = sender.main(args, options)
[2216]135    except KeyboardInterrupt :       
136        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
[1526]137    except SystemExit :       
138        pass
[1517]139    except :
140        try :
141            sender.crashed("pykotme failed")
142        except :   
[1546]143            crashed("pykotme failed")
[1113]144        retcode = -1
[1057]145
[1113]146    try :
147        sender.storage.close()
148    except (TypeError, NameError, AttributeError) :   
149        pass
150       
151    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.