aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrederic Lepied <flepied@mandriva.com>2002-02-20 21:47:47 +0000
committerFrederic Lepied <flepied@mandriva.com>2002-02-20 21:47:47 +0000
commitdcd51402801b9f14efd284b87b2c1a5bd54dfa20 (patch)
treefd24408447080c1d06682460f0884f8fa5a45c99
parentdfbf9275f7e2642710402b77a95f19709869247a (diff)
downloadmsec-dcd51402801b9f14efd284b87b2c1a5bd54dfa20.tar
msec-dcd51402801b9f14efd284b87b2c1a5bd54dfa20.tar.gz
msec-dcd51402801b9f14efd284b87b2c1a5bd54dfa20.tar.bz2
msec-dcd51402801b9f14efd284b87b2c1a5bd54dfa20.tar.xz
msec-dcd51402801b9f14efd284b87b2c1a5bd54dfa20.zip
added insert_before.
-rw-r--r--share/ConfigFile.py21
1 files changed, 21 insertions, 0 deletions
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: