]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
Mit Parsing config weitergemacht
authorFrank Brehm <frank@brehm-online.com>
Wed, 27 Apr 2011 17:12:08 +0000 (17:12 +0000)
committerFrank Brehm <frank@brehm-online.com>
Wed, 27 Apr 2011 17:12:08 +0000 (17:12 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@213 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateCommon.py [new file with mode: 0755]
LogRotateConfig.py

diff --git a/LogRotateCommon.py b/LogRotateCommon.py
new file mode 100755 (executable)
index 0000000..e51a0af
--- /dev/null
@@ -0,0 +1,132 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# $Id$
+# $URL$
+
+'''
+@author: Frank Brehm
+@contact: frank@brehm-online.com
+@license: GPL3
+@copyright: (c) 2010-2011 by Frank Brehm, Berlin
+@version: 0.1.0
+@summary: Module for common used functions
+'''
+
+import re
+import sys
+
+revision = '$Revision$'
+revision = re.sub( r'\$', '', revision )
+revision = re.sub( r'Revision: ', r'r', revision )
+revision = re.sub( r'\s*$', '', revision )
+
+__author__    = 'Frank Brehm'
+__copyright__ = '(C) 2011 by Frank Brehm, Berlin'
+__contact__    = 'frank@brehm-online.com'
+__version__    = '0.1.0 ' + revision
+__license__    = 'GPL3'
+
+
+#========================================================================
+
+def split_parts( text, keep_quotes = False, raise_on_unbalanced = True):
+    '''
+    Split the given text in chunks by whitespaces or
+    single or double quoted strings.
+        
+    @param text:        the text to split in chunks
+    @type text:         str
+    @param keep_quotes: keep quotes of quoted chunks
+    @type keep_quotes:  bool
+    @param raise_on_unbalanced: raise an exception on
+                                unbalanced quotes
+    @type raise_on_unbalanced:  bool
+
+    @return: list of chunks
+    @rtype:  list
+    '''
+
+    chunks = []
+    if text is None:
+        return chunks
+
+    txt = str(text)
+    last_chunk = ''
+
+    # Big loop to split the text - until it is empty
+    while txt != '':
+
+        # add chunk, if there is a chunk left and a whitspace
+        # at the begin of the line
+        match = re.search(r"\s+", txt)
+        if ( last_chunk != '' ) and match:
+            chunks.append(last_chunk)
+            last_chunk = ''
+
+        # clean the line
+        txt = txt.strip()
+        if txt == '':
+            break
+
+        # search for a single quoted string at the begin of the line
+        match = re.search(r"^'((?:\\'|[^'])*)'", txt)
+        if match:
+            chunk = match.group(1)
+            chunk = re.sub(r"\\'", "'", chunk)
+            if keep_quotes:
+                chunk = "'" + chunk + "'"
+            last_chunk += chunk
+            txt = re.sub(r"^'(?:\\'|[^'])*'", "", txt)
+            continue
+
+        # search for a double quoted string at the begin of the line
+        match = re.search(r'^"((?:\\"|[^"])*)"', txt)
+        if match:
+            chunk = match.group(1)
+            chunk = re.sub(r'\\"', '"', chunk)
+            if keep_quotes:
+                chunk = '"' + chunk + '"'
+            last_chunk += chunk
+            txt = re.sub(r'^"(?:\\"|[^"])*"', "", txt)
+            continue
+
+        # search for unquoted, whitespace delimited text
+        # at the begin of the line
+        match = re.search(r'^((?:[^\s\'"]+|\\\'|\\")+)', txt)
+        if match:
+            last_chunk += match.group(1)
+            txt = re.sub(r'^(?:[^\s\'"]+|\\\'|\\")+', "", txt)
+            continue
+
+        # Only whitespaces left
+        match = re.search(r'^\s*$', txt)
+        if match:
+            break
+
+        # Check for unbalanced quotes
+        match = re.search(r'^([\'"].*)\s*', txt)
+        if match:
+            chunk = match.group(1)
+            if raise_on_unbalanced:
+                raise Exception("Unbalanced quotes in »%s«." % ( str(text) ) )
+            else:
+                last_chunk += chunk
+                continue
+
+        # Here we should not come to ...
+        raise Exception("Broken split of »%s«: »%s« left" %( str(text), txt))
+
+    if last_chunk != '':
+        chunks.append(last_chunk)
+
+    return chunks
+
+#========================================================================
+
+if __name__ == "__main__":
+    pass
+
+#========================================================================
+
+# vim: fileencoding=utf-8 filetype=python ts=4 expandtab
index 204aaa937a1b8b897d5380a3db97690d72c29531..b941c580725cacf0101de39fc2024eb786ed715e 100755 (executable)
@@ -20,6 +20,8 @@ import pprint
 import os
 import os.path
 
+from LogRotateCommon import split_parts
+
 revision = '$Revision$'
 revision = re.sub( r'\$', '', revision )
 revision = re.sub( r'Revision: ', r'r', revision )
@@ -28,7 +30,7 @@ revision = re.sub( r'\s*$', '', revision )
 __author__    = 'Frank Brehm'
 __copyright__ = '(C) 2011 by Frank Brehm, Berlin'
 __contact__    = 'frank@brehm-online.com'
-__version__    = '0.0.2 ' + revision
+__version__    = '0.1.1 ' + revision
 __license__    = 'GPL3'
 
 
@@ -543,6 +545,7 @@ class LogrotateConfigurationReader(object):
         '''
 
         _ = self.t.lgettext
+        pp = pprint.PrettyPrinter(indent=4)
         self.logger.debug( _("Try reading configuration from '%s' ...")
                             % (self.config_file) )
 
@@ -614,7 +617,12 @@ class LogrotateConfigurationReader(object):
             # start of a logfile pattern
             match = re.search(r'^[\'"]', line)
             if match or os.path.isabs(line):
-                parts = self._split_parts(line)
+                parts = split_parts(line)
+                if self.verbose > 3:
+                    self.logger.debug(
+                        ( _("Split into parts of: »%s«") % (line))
+                        + ":\n" + pp.pformat(parts)
+                    )
 
         return True
 
@@ -669,85 +677,6 @@ class LogrotateConfigurationReader(object):
         self.new_log['size']          = self.default['size']
         self.new_log['start']         = self.default['start']
 
-    #------------------------------------------------------------
-    def _split_parts(self, text):
-        '''
-        Split the given text in chunks by whitespaces or
-        single or double quoted strings.
-        
-        @param text: the text to split in chunks
-        @type text:  str
-
-        @return: list of chunks
-        @rtype:  list
-        '''
-
-        chunks = []
-        if text is None:
-            return chunks
-
-        txt = str(text)
-        last_chunk = ''
-
-        # Big loop to split the text - until it is empty
-        while txt != '':
-
-            # add chunk, if there is a chunk left and a whitspace
-            # at the begin of the line
-            match = re.search(r"\s+", txt)
-            if ( last_chunk != '' ) and match:
-                chunks.append(last_chunk)
-                last_chunk = ''
-
-            # clean the line
-            txt = txt.strip()
-            if txt == '':
-                break
-
-            # search for a single quoted string at the begin of the line
-            match = re.search(r"^'((?:\\'|[^'])*)'", txt)
-            if match:
-                last_chunk += match.group(1)
-                txt = re.sub(r"^'(?:\\'|[^'])*'", "", txt)
-                continue
-
-            # search for a double quoted string at the begin of the line
-            match = re.search(r'^"((?:\\"|[^"])*)"', txt)
-            if match:
-                last_chunk += match.group(1)
-                txt = re.sub(r'^"(?:\\"|[^"])*"', "", txt)
-                continue
-
-            # search for unquoted, whitespace delimited text
-            # at the begin of the line
-            match = re.search(r'^([^\s\'"]+)', txt)
-            if match:
-                last_chunk += match.group(1)
-                txt = re.sub(r'^[^\s\'"]+', "", txt)
-                continue
-
-            # Only whitespaces left
-            match = re.search(r'^\s*$', txt)
-            if match:
-                break
-
-            # Here we should not come to ...
-            raise Exception("Broken split of »%s«: »%s« left" %( str(text), txt))
-
-        if last_chunk != '':
-            chunks.append(last_chunk)
-
-        _ = self.t.lgettext
-        pp = pprint.PrettyPrinter(indent=4)
-
-        if self.verbose > 3:
-            self.logger.debug(
-                ( _("Split into chunks of: »%s«") % (str(text)))
-                + ":\n" + pp.pformat(chunks)
-            )
-
-        return chunks
-
 #========================================================================
 
 if __name__ == "__main__":