]> Frank Brehm's Git Trees - my-stuff/isc-config-parser.git/commitdiff
Mehrzeilige gequotete Dinge, Fehlerbehandlung
authorFrank Brehm <frank@brehm-online.com>
Thu, 27 Jan 2011 18:35:40 +0000 (18:35 +0000)
committerFrank Brehm <frank@brehm-online.com>
Thu, 27 Jan 2011 18:35:40 +0000 (18:35 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/IscConfigParser/trunk@191 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

IscConfigParser.py

index e432f74b8255cf66856c897a0694adb04f3d26fa..31f7bb6566a4920de311996263187f329265afb0 100755 (executable)
@@ -246,30 +246,66 @@ class IscConfigParser(LoggingObject):
                         if ( self.verbose > 2 ):
                             self.logger.debug( "new line: {0!r}".format( line ) )
 
-            line = line.lstrip()
-
+            # Tokens without quotings
             match = re.search( r'^([^"\s]+)', line )
             if match is not None:
-                res_array.append( match.group(1) )
+                token = match.group(1)
+                if token == ';':
+                    res_array.append( ';' )
+                else:
+                    semicolon_match = re.search( r';$', token )
+                    if semicolon_match is not None:
+                        token = re.sub( r';$', '', token )
+                        res_array.append( token )
+                        res_array.append( ';' )
+                    else:
+                        res_array.append( token )
                 line = re.sub( r'^[^"\s]+\s*', '', line )
                 continue
 
+            # quoted tokens
             match = re.search( r'^\s*"((?:[^"]|(?<=\\"))*)"', line )
             if match is not None:
                 res_array.append( match.group(1) )
                 line = re.sub( r'^\s*"(?:[^"]|(?<=\\"))*\s*"', '', line )
+                continue
+
+            # tokens with line breaks
+            match = re.search( r'^\s*"', line )
+            if match is not None:
+
+                token = re.sub( r'^\s*"', '', line )
 
-#        # remove whitespaces from the beginning and end of the line
-#        if not self._in_quoting:
-#            result = result.strip( " \t\n\r" )
+                do_quote_loop = True
+                while do_quote_loop:
 
-#        # Remove Perl-like comments
-#        if re.search( r'^(:?[^"]*(:?"[^"]*")?)*?#', result ):
-#            result = re.sub( r'^((:?[^"]*(:?"[^"]*")?)*?)#.*', r'\1', result )
+                    new_line = fd.readline()
+
+                    if new_line == '' or new_line is None:
+                        raise IscConfigParserError( "Unbalanced Quotings in {0!r}, starting at line {1}.".format( filename, current_rownum ) )
+
+                    if ( self.verbose > 2 ):
+                        self.logger.debug( "read line {0}: {1!r}".format( next_rownum, new_line ) )
+                    next_rownum += 1
+
+                    quote_end_match = re.search( r'((?:[^"]|(?<=\\"))*)"', new_line )
+                    if quote_end_match is not None:
+                        token += quote_end_match.group(1)
+                        res_array.append( token )
+                        do_quote_loop = False
+                        line = re.sub( r'(?:[^"]|(?<=\\"))*"', '', new_line )
+                    else:
+                        token += new_line
+
+                continue
+
+            line = line.lstrip()
+            if line == '':
+                break
 
-#        # Remoce C++-like comments
-#        if re.search( r'^(?:[^"]*(:?"[^"]*")?)*?//', result ):
-#            result = re.sub( r'^((:?[^"]*(:?"[^"]*")?)*?)//.*', r'\1', result )
+            raise IscConfigParserError( "This should not happen! file: {0!r}, line {1}, rest of line: {2!r}".format(
+                filename, current_rownum, line
+            ) )
 
         self._files[filename]['current_rownum'] = current_rownum
         self._files[filename]['next_rownum'] = next_rownum
@@ -324,8 +360,11 @@ if __name__ == "__main__":
         print "Configfiles to read: %s" % ( pp.pformat( files ) )
 
     for conffile in files:
-        conf = parse_isc_config( conffile, verbose = verbose )
-        print "Read configuration: \n%s" % ( pp.pformat( conf ) )
+        try:
+            conf = parse_isc_config( conffile, verbose = verbose )
+            print "Read configuration: \n%s" % ( pp.pformat( conf ) )
+        except IscConfigParserError as e:
+            print "Error in reading configuration file {0!r}: {1}".format( conffile, e )
     
 
 #========================================================================