root / pykota / trunk / bin / repykota @ 3356

Revision 3356, 4.9 kB (checked in by jerome, 16 years ago)

Normalize the help for the -P|--printer command line option.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[696]1#! /usr/bin/env python
[3260]2# -*- coding: UTF-8 -*-
[696]3#
[3260]4# PyKota : Print Quotas for CUPS
[696]5#
[3275]6# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
[3260]7# This program is free software: you can redistribute it and/or modify
[873]8# it under the terms of the GNU General Public License as published by
[3260]9# the Free Software Foundation, either version 3 of the License, or
[873]10# (at your option) any later version.
[3260]11#
[873]12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
[3260]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[696]19#
20# $Id$
21#
[2028]22#
[696]23
[3348]24"""Minimalist print accounting reports for PyKota."""
25
[731]26import sys
[1041]27import os
28import pwd
[731]29
30from mx import DateTime
31
[3294]32import pykota.appinit
[3348]33from pykota.utils import run
34from pykota.commandline import PyKotaOptionParser
35from pykota.errors import PyKotaCommandLineError
[3295]36from pykota.tool import PyKotaTool
[1048]37from pykota import reporter
[731]38       
39class RePyKota(PyKotaTool) :       
40    """A class for repykota."""
[1041]41    def main(self, ugnames, options) :
[731]42        """Print Quota reports generator."""
[3348]43        if options.ingroups and options.groups :
44            raise PyKotaCommandLineError, _("Incompatible options, see help.")
45           
[1785]46        if self.config.isAdmin :
47            # PyKota administrator
[1041]48            if not ugnames :
49                # no username, means all usernames
50                ugnames = [ "*" ]
[2452]51               
[3348]52            if options.ingroups :
53                groupsnames = options.ingroups.split(",")
[2452]54                groups = [self.storage.getGroup(gname) for gname in groupsnames]
55                members = {}
56                for group in groups :
57                    if not group.Exists :
58                        self.printInfo("Group %s doesn't exist." % group.Name, "warn")
59                    else :   
60                        for user in self.storage.getGroupMembers(group) :
61                            members[user.Name] = user
62                ugnames = [ m for m in members.keys() if self.matchString(m, ugnames) ]
[1041]63        else :       
64            # reports only the current user
[3348]65            if options.ingroups :
[2512]66                raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.")
[2452]67               
[1785]68            username = pwd.getpwuid(os.geteuid())[0]
[3348]69            if options.groups :
[1071]70                user = self.storage.getUser(username)
71                if user.Exists :
72                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
73                else :   
74                    ugnames = [ ]
[1041]75            else :
[1071]76                ugnames = [ username ]
[1041]77       
[3348]78        printers = self.storage.getMatchingPrinters(options.printer)
[900]79        if not printers :
[3348]80            raise PyKotaCommandLineError, _("There's no printer matching %s") % options.printer
[1041]81           
[3348]82        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, options.groups)
[1048]83        print self.reportingtool.generateReport()
[731]84                   
85if __name__ == "__main__" : 
[3348]86    parser = PyKotaOptionParser(description=_("Minimalist print accounting reports for PyKota. If not launched by a PyKota administrator, additionnal arguments representing users or groups names are ignored, limiting the scope of the reports to the current user."),
87                                usage="repykota [options] [usernames|groupnames]")
88    parser.add_option("-g", "--groups",
89                            action="store_true",
90                            dest="groups",
91                            help=_("Generate group print quota reports instead of user print quota reports."))
92    parser.add_option("-i", "--ingroups",
93                            dest="ingroups",
94                            help=_("Only reports users who are members of the specified groups. This option is reserved to PyKota administrators."))
95    parser.add_option("-P", "--printer",
96                            dest="printer",
97                            default="*",
[3356]98                            help=_("Acts on this printer only. You can specify several printer names by separating them with commas. The default value is '%default', which means all printers."))
[3348]99                           
100    parser.add_example('',
101                       _("This would generate a report for all users on all printers."))
102    parser.add_example('--printer HP2100',
103                       _("This would generate a report for all users who print to printer 'HP2100'."))
104    parser.add_example('--printer "laser*,*pson" jerome "jo*"',
105                       _("This would generate a report for all users named 'jerome' or whose name begins with 'jo', on all printers which name begins with 'laser' or ends with 'pson'."))
106                       
107    run(parser, RePyKota)
Note: See TracBrowser for help on using the browser.