]> Frank Brehm's Git Trees - my-stuff/isc-config-parser.git/commitdiff
Methode _get_next_token() definiert
authorFrank Brehm <frank@brehm-online.com>
Sat, 29 Jan 2011 15:01:07 +0000 (15:01 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sat, 29 Jan 2011 15:01:07 +0000 (15:01 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/IscConfigParser/trunk@193 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

IscConfigParser.py

index e7d16d4d9881c782affe875f777b601496491828..3735ce00f65a06e58261d220caa2d59d13a9236d 100755 (executable)
@@ -59,6 +59,8 @@ class IscConfigParser(LoggingObject):
 
         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
@@ -110,6 +112,7 @@ class IscConfigParser(LoggingObject):
             current_filename = filename
 
         self._current_filename = current_filename
+        self._token_list = []
 
         self._files = { 
             current_filename: {
@@ -128,6 +131,7 @@ class IscConfigParser(LoggingObject):
 
         del self._files
         del self._current_filename
+        del self._token_list
 
         return result
 
@@ -138,27 +142,56 @@ class IscConfigParser(LoggingObject):
         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 ):
@@ -359,12 +392,12 @@ if __name__ == "__main__":
     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 ) )