]> Frank Brehm's Git Trees - my-stuff/isc-config-parser.git/commitdiff
Bugfixing
authorFrank Brehm <frank@brehm-online.com>
Sun, 16 Jan 2011 22:09:34 +0000 (22:09 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sun, 16 Jan 2011 22:09:34 +0000 (22:09 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/IscConfigParser/trunk@188 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

IscConfigParser.py

index b92f4487b1672b8543331aeef951663c13f4f4be..fa7290371da0689781be37f4cf05dba4b745b8ce 100755 (executable)
@@ -19,6 +19,7 @@ import os.path
 import sys
 
 from fbrehm.common.logging_obj import LoggingObject
+from fbrehm.common.getopt import BaseOptParser
 
 revision = '$Revision$'
 revision = re.sub( r'\$', '', revision )
@@ -56,7 +57,7 @@ class IscConfigParser(LoggingObject):
         # Other properties
 
     #------------------------------------------------------
-    def parse( self, input, filename = None ):
+    def parse( self, input ):
         """Read a file in a ISC-like config style.
 
            The input can be either a file-like object or a string. If a string
@@ -67,21 +68,19 @@ class IscConfigParser(LoggingObject):
 
            @param input: file-like object or a string
            @type input: str
-           @param filename: filename for debugging and error messages
-           @type filename: str or None
            @return: the contents from the file as a standard python dictionary
            @rtype: dict
         """
 
         result = {}
 
-        if filename is not None and not os.path.isfile( filename ):
-            raise IscConfigParserError( "File {0!r} not found.".format( filename ) )
+        if input is not None and not os.path.isfile( input ):
+            raise IscConfigParserError( "File {0!r} not found.".format( input ) )
 
         fd = None
-        if filename is not None:
-            fd = open( filename, "r" )
-            self.current_filename = filename
+        if input is not None:
+            fd = open( input, "r" )
+            self.current_filename = input
         else:
             fs = sys.stdin
             self.current_filename = '<stdin>'
@@ -89,7 +88,7 @@ class IscConfigParser(LoggingObject):
         self.logger.debug( "Reading configuration file {0!r} ...".format( self.current_filename ) )
 
 
-        if filename is not None:
+        if input is not None:
             fd.close()
 
         return result
@@ -97,13 +96,11 @@ class IscConfigParser(LoggingObject):
 
 #========================================================================
 
-def parse_isc_config( input, filename = None, verbose = 0 ):
+def parse_isc_config( input, verbose = 0 ):
     '''Wrapper for IscConfigParser.parse()
 
        @param input: file-like object or a string
        @type input: str
-       @param filename: filename for debugging and error messages
-       @type filename: str or None
        @param verbose: verbosity level (default: 0)
        @type verbose: int
        @return: the contents from the file as a standard python dictionary
@@ -111,7 +108,7 @@ def parse_isc_config( input, filename = None, verbose = 0 ):
     '''
 
     parser = IscConfigParser( verbose = verbose )
-    return parser.parse( input, filename = filename )
+    return parser.parse( input )
 
 #========================================================================
 
@@ -119,9 +116,9 @@ if __name__ == "__main__":
 
     opt_parser = BaseOptParser(
         version     = __version__,
-        prog        = 'ipv6-hostpart.py',
-        description = u'Calculates the 64 bit host part of an IPv6 address by given MAC addresses or infiniband GUIDs.',
-        usage       = u'%s [Options] <MAC address or IB-GUID> [<MAC> ...]'
+        prog        = 'IscConfigParser.py',
+        description = u'Parses an ISC Bind configuration file.',
+        usage       = u'%s [Options] [<named.conf> ...]'
     )
 
     opt_parser.getOpts()
@@ -129,14 +126,15 @@ if __name__ == "__main__":
     verbose = opt_parser.options.verbose
 
     files = []
-    if len( sys.argv ) > 0:
-        files = sys.argv[1:]
+    if opt_parser.args is not None and len( opt_parser.args ) > 0:
+        files = opt_parser.args[1:]
     else:
-        files = os.sep + os.path.join( 'etc', 'bind', 'named.conf' )
+        files = [ os.sep + os.path.join( 'etc', 'bind', 'named.conf' ) ]
 
+    pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
     for conffile in files:
-        conf = parse_isc_config( conffile, 
-
+        conf = parse_isc_config( conffile, verbose = verbose )
+        print "Read configuration: \n%s" % ( pp.pformat( conf ) )
     
 
 #========================================================================