From dcd51402801b9f14efd284b87b2c1a5bd54dfa20 Mon Sep 17 00:00:00 2001 From: Frederic Lepied Date: Wed, 20 Feb 2002 21:47:47 +0000 Subject: added insert_before. --- share/ConfigFile.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'share/ConfigFile.py') diff --git a/share/ConfigFile.py b/share/ConfigFile.py index 582b36e..98b32a6 100644 --- a/share/ConfigFile.py +++ b/share/ConfigFile.py @@ -325,6 +325,27 @@ class ConfigFile: matches = matches + 1 return matches + def insert_before(self, regex, value, at_top_if_not_found=0, all=0): + matches = 0 + r=re.compile(regex) + lines = self.get_lines() + for idx in range(0, len(lines)): + res = r.search(lines[idx]) + if res: + s = substitute_re_result(res, value) + log(_("inserted in %s before the line %d:\n%s\nthe line:\n%s") % (self.path, idx, lines[idx], s)) + lines.insert(idx, s) + self.modified() + matches = matches + 1 + if not all: + return matches + if matches == 0 and at_top_if_not_found: + log(_("inserted at the top of %s the line:\n%s") % (self.path, value)) + lines.insert(0, value) + self.modified() + matches = matches + 1 + return matches + def insert_at(self, idx, value): lines = self.get_lines() try: -- cgit v1.2.1