#!/usr/bin/env python # ----------------------------------------------------------------------- # Copyright (C) 2003 Chris Ottrey. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MER- # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # ----------------------------------------------------------------------- # # This code is part of the pytvgrab project: # http://pytvgrab.sourceforge.net # # ----------------------------------------------------------------------- # Subversion Information, do not edit # # $Rev: 250 $ # $LastChangedDate: 2004-10-19 05:41:28 +1000 (Tue, 19 Oct 2004) $ # $LastChangedRevision: 250 $ # $LastChangedBy: gustavo $ # # $Log: $ # import re import sys import getopt import string import os import i18n import datetime2 from output import bold from config_locale import toprint class Option: PAD_SIZE=20 def __init__(self, function, s, long, message, param=None): self.function=function self.s=s self.long=long self.message=message self.param=param if param: self.message%=param # __init__() def getLongopt(self): result='' if self.long: result=self.long if self.param: result=result+'='+self.param return result # getLongopt() def __str__(self, pad_size=None): result='' pad=self.PAD_SIZE*' ' if pad_size: pad=pad_size*' ' if self.s: result=result + bold('-%s') % self.s[0] if self.long: result=result+',' else: result=result+' ' else: result=result+' ' l=self.getLongopt() if l: tmp = bold( '--%s' % l ) tmp += pad[ : len( pad ) - len( l ) ] else: tmp = ' %s' % pad result += ' ' + tmp + ' ' if self.message: result=result+'%s' % (self.message) return result # __str__ # Option class ProgException(Exception): pass class Prog: __revision__= '' __version__ = '' __author__ = '' cmd_line = [_('%s [OPTION]...')] def getElapsedTime(self): # calculate elapsed time _time_end = datetime2.nowdt() return ( _time_end - self._time_start ) # getElapsedTime() def function_help(self): print self.usage() sys.exit() # function_help() def function_version(self): print self.version() sys.exit() # function_version() options=[ Option(function_help, 'h', 'help' , _('display this message') ), Option(function_version, None, 'version' , _('display the version') ) ] def get_args(self): shortargs='' longargs=[] for option in self.options: if option.s: shortargs=shortargs+option.s if option.param: shortargs=shortargs+':' if option.long: longargs.append(option.long) if option.param: longargs[-1]=longargs[-1]+'=' return shortargs, longargs # get_args() def version(self): version='?' if self.__version__: version= self.__version__ result= self.name + ( _( ' (Version %s)' ) % version ) if self.__revision__: revision=self.__revision__ m=re.match(r'^[$]Rev(ision)?[:][ ](?P[0-9.]+)[ ][$]$', self.__revision__) if m: revision=m.groupdict()['revision'] result=result + _(' (Revision %s)') % revision if self.__author__: result=result+_(' (by %s)') % (self.__author__) return result # version() def getPad_size(self): result=0 for option in self.options: width=len(option.getLongopt()) if width > result: result=width return result # getPad_size() def usage(self): result=self.version() for line in self.cmd_line: result+='\n' + _( 'Usage:') + ( ( ' %s' % line ) % self.name ) result+='\n' pad_size=self.getPad_size() for option in self.options: result=result+' %s\n' % option.__str__(pad_size) return result # usage() def getFunctions(self): result={} for option in self.options: if option.s: o='-%s' % option.s result.update({o:option.function}) if option.long: o='--%s' % option.long result.update({o:option.function}) return result # getFunctions() def set_args(self, args): self.args=args # set_args() def __init__(self, argv): self._time_start=datetime2.nowdt() self.name=os.path.basename(argv[0]) shortargs, longargs=self.get_args() function=self.getFunctions() try: opts, args=getopt.getopt(argv[1:], shortargs, longargs) for o, a in opts: if a: function[o](self, a) else: function[o](self) self.set_args(args) except SystemExit: sys.exit() except (getopt.GetoptError, ProgException), e: sys.stderr.write( toprint( _('ERROR: %s\n\n') % e ) ) sys.stderr.write( toprint( '%s\n' % self.usage() ) ) sys.exit(2) self.main() # __init__() def main(self): None # Prog # -------------- Unit Tests -------------- # using_unittest2=False try: import unittest2 as unittest using_unittest2=True except: import unittest class Prog_UnitTest(unittest.TestCase): def setUp(self): class P(Prog): def function_a(self, a): self.a=int(a) def function_b(self): self.b=1 def function_c(self): self.c='yes' def function_d(self): self.d='Y' options=[ Option(function_a, 'a' , 'aay' , 'an option with a %s', 'VALUE' ) , Option(function_b, 'b' , 'bee' , 'an option' ) , Option(function_c, 'c' , None , 'short only option' ) , Option(function_d, None , 'dee' , 'long only option' ) ] # P self.p01=P(['p01', '-a4' ]) self.p02=P(['p02', '-b' ]) self.p03=P(['p03', '-c' ]) self.p04=P(['p04', '--dee' ]) self.p05=P(['p05', '-a5' , '-b' ]) self.p06=P(['p06', '--aay=6' ]) self.p07=P(['p07', '--bee' ]) self.p08=P(['p08', '--aay=7' , '--bee' ]) self.p09=P(['p09', '--bee' , '--dee' ]) self.p10=P(['p10', '--aay=8' , '--bee', '-c' ]) self.p11=P(['p11', '--aay=9' , '--bee', '--dee' ]) self.p12=P(['p12', '-bc' ]) self.p13=P(['p13' ]) self.p13.__version__='1.0' self.p14=P(['p14' ]) self.p14.__version__='1.0' self.p14.__author__='Pee Rog' self.p14_usage='p14' + ( _( " (Version %s)" ) % '1.0' ) + \ ( _( ' (by %s)' ) % 'Pee Rog' ) + '\n' + \ ( _( 'Usage: %s' ) % ( _( '%s [OPTION]...' ) % 'p14' ) ) + '\n' + \ ' ' + bold('-a') + ', ' + bold( '--aay=VALUE' ) + \ ' an option with a VALUE\n' +\ ' ' + bold('-b') + ', ' + bold( '--bee' ) + ' an option\n' + \ ' ' + bold('-c') + ' short only option\n' + \ ' ' + bold('--dee') + ' long only option\n' self.p15=P(['p15', 'arg1', 'arg2']) # setUp() def test01(self): v=self.p01.name ; assert v == 'p01', v def test02(self): v=self.p01.args ; assert v == [], v def test03(self): v=self.p01.a ; assert v == 4, v def test04(self): v=self.p02.name ; assert v == 'p02', v def test05(self): v=self.p02.b ; assert v == 1, v def test06(self): v=self.p03.name ; assert v == 'p03', v def test07(self): v=self.p03.c ; assert v == 'yes', v def test08(self): v=self.p04.d ; assert v == 'Y', v def test09(self): v=self.p05.a ; assert v == 5, v def test10(self): v=self.p05.b ; assert v == 1, v def test11(self): v=self.p06.a ; assert v == 6, v def test12(self): v=self.p07.b ; assert v == 1, v def test13(self): v=self.p08.a ; assert v == 7, v def test14(self): v=self.p08.b ; assert v == 1, v def test15(self): v=self.p09.b ; assert v == 1, v def test16(self): v=self.p09.d ; assert v == 'Y', v def test17(self): v=self.p10.a ; assert v == 8, v def test18(self): v=self.p10.b ; assert v == 1, v def test19(self): v=self.p10.c ; assert v == 'yes', v def test20(self): v=self.p11.a ; assert v == 9, v def test21(self): v=self.p11.b ; assert v == 1, v def test22(self): v=self.p11.d ; assert v == 'Y', v def test23(self): v=self.p12.b ; assert v == 1, v def test24(self): v=self.p12.c ; assert v == 'yes', v def test25(self): v=self.p13.version(); assert v == 'p13 (Version 1.0)', v def test26(self): v=self.p14.version(); assert v == 'p14 (Version 1.0) (by Pee Rog)', v def test27(self): v=self.p14.usage() ; assert v == self.p14_usage, v def test28(self): v=self.p15.args ; assert v == ['arg1','arg2'], v class Option_UnitTest(unittest.TestCase): def func(): return 'ran func' o1=Option(func, 'a' , 'aay' , 'an option with a %s', 'VALUE' ) o2=Option(func, 'b' , 'bee' , 'an option' ) o3=Option(func, 'c' , None , 'short only option' ) o4=Option(func, None , 'dee' , 'long only option' ) def test01(self): v=self.o1.s ; assert v == 'a', v def test02(self): v=self.o1.long ; assert v == 'aay', v def test03(self): v=self.o1.param ; assert v == 'VALUE', v def test04(self): v=self.o1.message ; assert v == 'an option with a VALUE', v #def test05(self): v=self.o1.function ; assert v == self.func, v #def test06(self): v=self.o1.function(); assert v == self.func(), v def test07(self): v=self.o1.__str__(1); assert v == ( bold('-a') + ', ' + bold('--aay=VALUE') + ' an option with a VALUE' ), v def test08(self): v=self.o2.__str__(1); assert v == ( bold('-b') + ', ' + bold('--bee') + ' an option' ), v def test09(self): v=self.o3.__str__(1); assert v == ( bold('-c') + ' short only option' ), v def test10(self): v=self.o4.__str__(1); assert v == ( ' ' + bold('--dee') + ' long only option' ), v def test11(self): v=str(self.o1) ; assert v == ( bold('-a') + ', ' + bold('--aay=VALUE') + ' an option with a VALUE' ), v if using_unittest2 or __name__ == '__main__': unittest.main() # -------------- Unit Tests -------------- #