super( IscConfigParser, self ).__init__( verbose = verbose )
+ self.pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
+
self.current_filename = None
self.base_dir = base_dir
self.do_includes = do_includes
current_filename = filename
self._current_filename = current_filename
+ self._token_list = []
self._files = {
current_filename: {
del self._files
del self._current_filename
+ del self._token_list
return result
result = {}
self.logger.debug( "Reading file {0!r} ...".format( filename ) )
- pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
- line_tokens = self._read_next_line()
- while line_tokens is not None:
- rownum = self._files[filename]['current_rownum']
- if ( self.verbose > 1 ) and ( len(line_tokens) > 0 ):
- self.logger.debug( "%4d: %s" % ( rownum, pp.pformat( line_tokens ) ) )
- line_tokens = self._read_next_line()
+ next_token_tuple = self._get_next_token()
+ while next_token_tuple is not None:
+ if self.verbose > 1:
+ self.logger.debug( "%s (%d): %s" % ( next_token_tuple[1], next_token_tuple[2], self.pp.pformat( next_token_tuple[0] ) ) )
+ next_token_tuple = self._get_next_token()
return result
+ #------------------------------------------------------
+ def _get_next_token( self ):
+ """ Gives the next token from inputstream back.
+ Returns None if EOF.
+ TODO: Resolves includings, if there are any.
+
+ @return: A tuple of three values:
+ - the value of the next token
+ - the filename, where it was found
+ - the line number of this file, where this token was found
+ or None if EOF
+ @rtype: tuple or None
+ """
+
+ # current token list is empty - read the next list
+ if len( self._token_list ) < 1:
+ next_list = self._read_next_line()
+ if next_list is None:
+ return None
+ while len( next_list ) < 1:
+ next_list = self._read_next_line()
+ if next_list is None:
+ return None
+ self._token_list = next_list
+
+ # shift from self._token_list and return
+ token = self._token_list.pop(0)
+ filename = self._current_filename
+ rownum = self._files[filename]['current_rownum']
+ return ( token, filename, rownum )
+
#------------------------------------------------------
def _open_configfile( self, filename ):
- """Checks the availablity of the given filename, and opens it readonly.
- Raises an IscConfigParserError exception, if the file isn't available, or another
- exception if the open fails.
+ """ Checks the availablity of the given filename, and opens it readonly.
+ Raises an IscConfigParserError exception, if the file isn't available, or another
+ exception if the open fails.
- @param filename: the filename to open
- @type filename: str
+ @param filename: the filename to open
+ @type filename: str
- @return: The file descriptor of the opened file
- @rtype: file object
+ @return: The file descriptor of the opened file
+ @rtype: file object
"""
if not os.path.isfile( filename ):
verbose = opt_parser.options.verbose
files = []
- pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
if opt_parser.args is not None and len( opt_parser.args ) > 0:
files = opt_parser.args[:]
else:
files = [ os.sep + os.path.join( 'etc', 'bind', 'named.conf' ) ]
+ pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
if verbose > 1:
print "Configfiles to read: %s" % ( pp.pformat( files ) )