]> Frank Brehm's Git Trees - my-stuff/nagios.git/commitdiff
Allgemeinen Optionen-Parser angefangen
authorFrank Brehm <frank@brehm-online.com>
Mon, 6 Dec 2010 17:03:16 +0000 (17:03 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 6 Dec 2010 17:03:16 +0000 (17:03 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@146 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

bin/fbrehm/__init__.py [new file with mode: 0755]
bin/fbrehm/common/__init__.py [new file with mode: 0755]
bin/fbrehm/common/getopt.py [new file with mode: 0755]
bin/nagios/__init__.py
bin/nagios/config.py

diff --git a/bin/fbrehm/__init__.py b/bin/fbrehm/__init__.py
new file mode 100755 (executable)
index 0000000..3d21abb
--- /dev/null
@@ -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 (executable)
index 0000000..129b73b
--- /dev/null
@@ -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 (executable)
index 0000000..4499a47
--- /dev/null
@@ -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 <frank@brehm-online.com>'
+__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
index 58e5493c35ccc0d8659c34d28d5473c7d1ee46a2..d0b397ebd7a4ede65de00b7534da88dcb092a118 100644 (file)
@@ -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
index 03ce50644c29e879eab4e5e5a9aac1ae3014d8cf..10974d260637a29b71d6d3609d6278ee348b28c0 100644 (file)
@@ -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