From: Frank Brehm Date: Wed, 27 Apr 2011 08:19:46 +0000 (+0000) Subject: Mit Parsing config weitergemacht X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=8a81bf081abc32fb8dd6d9de7ade75397b317053;p=my-stuff%2Fpy-logrotate.git Mit Parsing config weitergemacht git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@212 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/LogRotateConfig.py b/LogRotateConfig.py index c982bd2..204aaa9 100755 --- a/LogRotateConfig.py +++ b/LogRotateConfig.py @@ -611,6 +611,11 @@ class LogrotateConfigurationReader(object): self.scripts[newscript].append(line) continue + # start of a logfile pattern + match = re.search(r'^[\'"]', line) + if match or os.path.isabs(line): + parts = self._split_parts(line) + return True #------------------------------------------------------------ @@ -664,6 +669,84 @@ 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 #========================================================================