root / pykota / trunk / bin / pykoef @ 2609

Revision 2609, 5.5 kB (checked in by jerome, 19 years ago)

Insufficient permission to run a command now exits with status -2.
Error in command line options now exits with status -2.
Processing interrupted with Ctrl+C (or SIGTERM) now exits with status -3.

  • 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 Coefficients manager
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import os
28import sys
29import pwd
30
31from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
32
33__doc__ = N_("""pykoef v%(__version__)s (c) %(__years__)s %(__author__)s
34
35A coefficients Manager for PyKota.
36
37command line usage :
38
39  pykoef [options] cofficient1 coefficient2 ... coefficientN
40
41options :
42
43  -v | --version       Prints pykoef version number then exits.
44  -h | --help          Prints this message then exits.
45 
46  -a | --add           Adds coefficients if they don't exist in PyKota's
47                       database. If they exist, they are modified
48                       unless -s|--skipexisting is also used.
49
50  -d | --delete        Deletes coefficients from PyKota's database.
51
52  -l | --list          List informations about the coefficients.
53 
54  -P | --printer P     Only apply the command to printer P's coefficients.
55                       P can be a comma separated list of printers' names
56                       or wildcards.
57
58  -s | --skipexisting  In combination with the --add option above, tells
59                       pykoef to not modify existing coefficients.
60                       
61  -V | --value v       Sets the coefficients' value to v. This value can                     
62                       be any real number, positive or negative.
63
64  coefficient1 through coefficientN can contain wildcards if the --add option
65  is not set.
66
67examples :                             
68
69  # TODO
70""")
71       
72class PyKOef(PyKotaTool) :       
73    """A class for a coefficients manager."""
74    def main(self, names, options) :
75        """Manage printers coefficients."""
76        if (not self.config.isAdmin) and (not options["list"]) :
77            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command."))
78           
79        if options["list"] and not names :
80            names = ["*"]
81           
82        try :     
83            value = float(options["value"])
84        except :   
85            raise PyKotaCommandLineError, _("Coefficients' values must be numeric. See help.")
86           
87        #
88        # TODO   
89        #
90        raise RuntimeError, "Not implemented yet ! Please come back later..."
91                     
92if __name__ == "__main__" : 
93    retcode = 0
94    try :
95        defaults = { \
96                     "printer" : "*", \
97                     "value" : "1.0", \
98                   }
99        short_options = "hvadlsP:V:"
100        long_options = ["help", "version", "add", "delete", "list", "printer=", "skipexisting", "value="]
101       
102        # Initializes the command line tool
103        manager = PyKoef(doc=__doc__)
104        manager.deferredInit()
105       
106        # parse and checks the command line
107        (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options)
108       
109        # sets long options
110        options["help"] = options["h"] or options["help"]
111        options["version"] = options["v"] or options["version"]
112        options["add"] = options["a"] or options["add"]
113        options["delete"] = options["d"] or options["delete"] 
114        options["list"] = options["l"] or options["list"]
115        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
116        options["value"] = options["V"] or options["value"] or defaults["value"]
117        options["skipexisting"] = options["s"] or options["skipexisting"]
118       
119        if options["help"] :
120            manager.display_usage_and_quit()
121        elif options["version"] :
122            manager.display_version_and_quit()
123        elif (options["delete"] and options["add"]) \
124           or (options["skipexisting"] and not options["add"]) \
125           or (options["list"] and (options["add"] or options["delete"])) :
126            raise PyKotaCommandLineError, _("incompatible options, see help.")
127        elif (not args) and (options["add"] or options["delete"]) :   
128            raise PyKotaCommandLineError, _("You have to pass coefficients on the command line")
129        else :
130            retcode = manager.main(args, options)
131    except KeyboardInterrupt :       
132        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
133        retcode = -3
134    except PyKotaCommandLineError, msg :   
135        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
136        retcode = -2
137    except SystemExit :       
138        pass
139    except :
140        try :
141            manager.crashed("pykoef failed")
142        except :   
143            crashed("pykoef failed")
144        retcode = -1
145
146    try :
147        manager.storage.close()
148    except (TypeError, NameError, AttributeError) :   
149        pass
150       
151    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.