import sys
from fbrehm.common.logging_obj import LoggingObject
+from fbrehm.common.getopt import BaseOptParser
revision = '$Revision$'
revision = re.sub( r'\$', '', revision )
# 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
@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>'
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
#========================================================================
-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
'''
parser = IscConfigParser( verbose = verbose )
- return parser.parse( input, filename = filename )
+ return parser.parse( input )
#========================================================================
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()
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 ) )
#========================================================================