Show
Ignore:
Timestamp:
02/06/03 11:39:23 (21 years ago)
Author:
jalet
Message:

Preliminary edpykota work.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/tool.py

    r713 r715  
    1515# 
    1616# $Log$ 
     17# Revision 1.9  2003/02/06 10:39:23  jalet 
     18# Preliminary edpykota work. 
     19# 
    1720# Revision 1.8  2003/02/06 09:19:02  jalet 
    1821# More robust behavior (hopefully) when the user or printer is not managed 
     
    4649import sys 
    4750import os 
     51import getopt 
    4852import smtplib 
    4953 
    5054from mx import DateTime 
    5155 
    52 from pykota import config 
    53 from pykota import storage 
    54 from pykota import logger 
     56from pykota import version, config, storage, logger 
    5557 
    5658class PyKotaToolError(Exception): 
     
    6567class PyKotaTool :     
    6668    """Base class for all PyKota command line tools.""" 
    67     def __init__(self, isfilter=0) : 
     69    def __init__(self, isfilter=0, doc="PyKota %s (c) 2003 %s" % (version.__version__, version.__author__)) : 
    6870        """Initializes the command line tool.""" 
     71        self.documentation = doc 
    6972        self.config = config.PyKotaConfig(os.environ.get("CUPS_SERVERROOT", "/etc/cups")) 
    7073        self.logger = logger.openLogger(self.config) 
     
    7578        self.adminmail = self.config.getAdminMail() 
    7679         
     80    def display_version_and_quit(self) : 
     81        """Displays version number, then exists successfully.""" 
     82        print version.__version__ 
     83        sys.exit(0) 
     84     
     85    def display_usage_and_quit(self) : 
     86        """Displays command line usage, then exists successfully.""" 
     87        print self.documentation 
     88        sys.exit(0) 
     89         
     90    def parseCommandline(self, argv, short, long) : 
     91        """Parses the command line, controlling options.""" 
     92        # split options in two lists: those which need an argument, those which don't need any 
     93        withoutarg = [] 
     94        witharg = [] 
     95        lgs = len(short) 
     96        i = 0 
     97        while i < lgs : 
     98            ii = i + 1 
     99            if (ii < lgs) and (short[ii] == ':') : 
     100                # needs an argument 
     101                witharg.append(short[i]) 
     102                ii = ii + 1 # skip the ':' 
     103            else : 
     104                # doesn't need an argument 
     105                withoutarg.append(short[i]) 
     106            i = ii 
     107                 
     108        for option in long : 
     109            if option[-1] == '=' : 
     110                # needs an argument 
     111                witharg.append(option[:-1]) 
     112            else : 
     113                # doesn't need an argument 
     114                withoutarg.append(option) 
     115         
     116        # we begin with all possible options unset 
     117        parsed = {} 
     118        for option in withoutarg + witharg : 
     119            parsed[option] = None 
     120         
     121        # then we parse the command line 
     122        args = []       # to not break if something unexpected happened 
     123        try : 
     124            options, args = getopt.getopt(argv, short, long) 
     125            if options : 
     126                for (o, v) in options : 
     127                    # we skip the '-' chars 
     128                    lgo = len(o) 
     129                    i = 0 
     130                    while (i < lgo) and (o[i] == '-') : 
     131                        i = i + 1 
     132                    o = o[i:] 
     133                    if o in witharg : 
     134                        # needs an argument : set it 
     135                        parsed[o] = v 
     136                    elif o in withoutarg : 
     137                        # doesn't need an argument : boolean 
     138                        parsed[o] = 1 
     139                    else : 
     140                        # should never occur 
     141                        raise PyKotaToolError, "Unexpected problem when parsing command line" 
     142            elif (not args) and sys.stdin.isatty() : # no option and no argument, we display help if we are a tty 
     143                self.display_usage_and_quit() 
     144        except getopt.error, msg : 
     145            sys.stderr.write("%s\n" % msg) 
     146            sys.stderr.flush() 
     147            self.display_usage_and_quit() 
     148        return (parsed, args) 
     149     
    77150    def sendMessage(self, touser, fullmessage) : 
    78151        """Sends an email message containing headers to some user."""