From 0ffbd2b84fd72570cd601fe5ced3f8835610fcd1 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 15 Apr 2011 21:21:22 +0000 Subject: [PATCH] weiter .. git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@198 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- LogRotateGetopts.py | 91 +++++++++++++++++++++++++++++++++++++++++++-- logrotate.py | 2 + 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/LogRotateGetopts.py b/LogRotateGetopts.py index 808794f..de7e082 100755 --- a/LogRotateGetopts.py +++ b/LogRotateGetopts.py @@ -14,6 +14,12 @@ ''' import re +import sys + +from optparse import OptionError +from optparse import OptionParser +from optparse import OptionConflictError + revision = '$Revision$' revision = re.sub( r'\$', '', revision ) @@ -40,7 +46,7 @@ class LogrotateOptParser(object): #------------------------------------------------------- def __init__( self, prog = '%prog', version = None, - description = '', + description = 'rotates and compresses system logs', usage = 'Usage: %s [options]', ): ''' @@ -57,15 +63,50 @@ class LogrotateOptParser(object): ''' self.prog = prog - self.version = version + ''' + @ivar: The name of the calling process + @type: str + ''' + + self.version = __version__ + ''' + @ivar: The version string to use + @type: str + ''' + self.description = description + ''' + @ivar: description of the program + @type: str + ''' + self.usage = usage %(prog) + ''' + @ivar: the usage string in getopt help output + @type: str + ''' self.options = None + ''' + @ivar: a dict with all given commandline options + after calling getOpts() + @type: dict or None + ''' + self.args = None - self.__options_set = False - self.__action_set = None + ''' + @ivar: a list with all commandline parameters, what are not options + @type: list or None + ''' + self.parsed = False + ''' + @ivar: flag, whether the parsing was done + @type: bool + ''' + + if version: + self.version = version self.parser = OptionParser( prog = self.prog, @@ -73,8 +114,50 @@ class LogrotateOptParser(object): description = self.description, usage = self.usage ) + ''' + @ivar: the working OptionParser Object + @type: optparse.OptionParser + ''' + + self._add_options() + + #------------------------------------------------------- + def _add_options(self): + ''' + Private function to add all necessary options + to the OptionParser object + ''' + + self.parser.add_option( + '--simulate', + '--test', + '-T', + default = False, + action = 'store_true', + dest = 'test', + help = 'set this do simulate commands' + ) + self.parser.add_option( + '--verbose', + '-v', + default = False, + action = 'count', + dest = 'verbose', + help = 'set the verbosity level' + ) + + #---------------------------------------------------------------------- + def getOpts(self): + ''' + Wrapper function to OptionParser.parse_args(). + Sets self.options and self.args with the appropriate values. + @return: None + ''' + if not self.parsed: + self.options, self.args = self.parser.parse_args() + self.parsed = True #======================================================================== diff --git a/logrotate.py b/logrotate.py index 06ffde8..e165a22 100755 --- a/logrotate.py +++ b/logrotate.py @@ -15,6 +15,8 @@ import re +from LogRotateGetopts import LogrotateOptParser; + revision = '$Revision$' revision = re.sub( r'\$', '', revision ) revision = re.sub( r'Revision: ', r'r', revision ) -- 2.39.5