From 3a392504d3843a3b90e1509f1ba5529f79a16076 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 6 Dec 2010 17:03:16 +0000 Subject: [PATCH] Allgemeinen Optionen-Parser angefangen git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@146 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- bin/fbrehm/__init__.py | 9 +++ bin/fbrehm/common/__init__.py | 9 +++ bin/fbrehm/common/getopt.py | 117 ++++++++++++++++++++++++++++++++++ bin/nagios/__init__.py | 9 ++- bin/nagios/config.py | 14 ++++ 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100755 bin/fbrehm/__init__.py create mode 100755 bin/fbrehm/common/__init__.py create mode 100755 bin/fbrehm/common/getopt.py diff --git a/bin/fbrehm/__init__.py b/bin/fbrehm/__init__.py new file mode 100755 index 0000000..3d21abb --- /dev/null +++ b/bin/fbrehm/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: (c) 2010 - 2011 by Frank Brehm, Berlin +@summary: All my own stuff +''' +# vim: fileencoding=utf-8 filetype=python ts=4 diff --git a/bin/fbrehm/common/__init__.py b/bin/fbrehm/common/__init__.py new file mode 100755 index 0000000..129b73b --- /dev/null +++ b/bin/fbrehm/common/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: (c) 2010 - 2011 by Frank Brehm, Berlin +@summary: All my own common stuff +''' +# vim: fileencoding=utf-8 filetype=python ts=4 diff --git a/bin/fbrehm/common/getopt.py b/bin/fbrehm/common/getopt.py new file mode 100755 index 0000000..4499a47 --- /dev/null +++ b/bin/fbrehm/common/getopt.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: (c) 2010-2011 by Frank Brehm, Berlin +@license: GPL3 +@version: 0.0.1 +@summary: This module handle all getopt stuff +''' + +__author__ = 'Frank Brehm ' +__copyright__ = '(C) 2010 by Frank Brehm, Berlin' +__version__ = '0.0.1' + +import os +import sys + +from optparse import OptionError +from optparse import OptionParser +from optparse import OptionConflictError + +class BaseOptParser(object): + ''' + This is the base class for Option Parsing. All other getopts classes should + derived from this class. This class adds the default cmd named options like + '--test' and '--verbose'. + It implements also all callback functions/methods - so no class has to + write its own callback and every callback is reusable for every class. + @author: Robin Wittler / Frank Brehm + @contact: robin.wittler@profitbricks.com / frank@brehm-online.com + ''' + + #--------------------------------------------------------------------- + def __init__( self, prog='%prog', version=None, description='' ): + ''' + Costructor. + @param prog: The name of the calling process (e.g. sys.argv[0]) + @type prog: str + @param version: The version string to use + @type version: str + @param description: The Description the process should use + @type description: str + @return: None + ''' + self.prog = prog + self.version = version + self.description = description + self.usage = 'Usage: %s [options]' %(prog) + + self.options = None + self.args = None + self.__options_set = False + self.__action_set = None + self.parsed = False + + self.parser = OptionParser( + prog=self.prog, + version=self.version, + description=self.description + ) + + self.addOption( + '--simulate', + '--test', + '-T', + default = False, + action = 'store_true', + dest = 'test', + help = 'set this do simulate commands' + ) + + self.addOption( + '--verbose', + '-v', + default = False, + action = 'count', + dest = 'verbose', + help = 'set the verbosity level' + ) + + def getOpts(self): + ''' + The baseimplentation of getOpts. It ensures that evry class + which derived from the base class have to call the 'addOption' + method first to add some options. + @return: None + ''' + if not self.__options_set: + raise RuntimeError( + 'You must call addOption first.' + ) + if not self.parsed: + self.options, self.args = self.parser.parse_args() + self.parsed = True + + def addOption(self, *targs, **kwargs): + ''' + This Method adds the options to the parser instance. + @param targs: The getopts cmd param names (e.g. '--help', '-h') + @type targs: tuple + @param kwargs: The named getopts options (e.g. 'dest', 'help', + 'callback') + @type kwargs: dict + @return: None + @rtype: None + ''' + self.parser.add_option(*targs, **kwargs) + if not self.__options_set: + self.__options_set = True + + + #--------------------------------------------------------------------- + + + +# vim: fileencoding=utf-8 filetype=python ts=4 expandtab diff --git a/bin/nagios/__init__.py b/bin/nagios/__init__.py index 58e5493..d0b397e 100644 --- a/bin/nagios/__init__.py +++ b/bin/nagios/__init__.py @@ -1,4 +1,9 @@ #!/usr/bin/python - - +# -*- coding: utf-8 -*- +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: (c) 2010 - 2011 by Frank Brehm, Berlin +@summary: All what is about Nagios +''' # vim: fileencoding=utf-8 filetype=python ts=4 diff --git a/bin/nagios/config.py b/bin/nagios/config.py index 03ce506..10974d2 100644 --- a/bin/nagios/config.py +++ b/bin/nagios/config.py @@ -4,6 +4,15 @@ # $Id$ # $URL$ +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@license: GPL3 +@copyright: (c) 2010-2011 by Frank Brehm, Berlin +@version: 0.1.0 +@summary: This module handles nagios configuration. +''' + import os import re import sys @@ -13,6 +22,11 @@ import pprint from nagios.cfg.struct import NagiosConfigStruct, NagiosConfigStructError +__author__ = 'Frank Brehm' +__contact__ = 'frank@brehm-online.com' +__version__ = '0.1.0' +__license__ = 'GPL3' + class NagiosConfigError(Exception): """Base class for exceptions in this module.""" pass -- 2.39.5