[node-patches] Change in ovirt-node[node-3.0]: fs: Add sub()/search() to File

fabiand at fedoraproject.org fabiand at fedoraproject.org
Wed Feb 19 14:15:39 UTC 2014


Fabian Deutsch has uploaded a new change for review.

Change subject: fs: Add sub()/search() to File
......................................................................

fs: Add sub()/search() to File

sub() and search() allow simple operations on the file contents.

Change-Id: I2e2f7d9578fb9b00b44d78a93f43f7c5198d66e4
Signed-off-by: Fabian Deutsch <fabiand at redhat.com>
---
M src/ovirt/node/utils/fs.py
1 file changed, 41 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/60/24760/1

diff --git a/src/ovirt/node/utils/fs.py b/src/ovirt/node/utils/fs.py
index 2043463..d25251d 100644
--- a/src/ovirt/node/utils/fs.py
+++ b/src/ovirt/node/utils/fs.py
@@ -24,9 +24,11 @@
 """
 
 from ovirt.node import log
+from ovirt.node.utils import process
 import shutil
 import os
 import StringIO
+import re
 
 from ovirt.node import base
 
@@ -123,6 +125,34 @@
         """
         return os.access(self.filename, mode)
 
+    def sed(self, expr, inplace=True):
+        """Run a sed expression on the file
+        """
+        cmd = ["sed", "-c"]
+        if inplace:
+            cmd.append("-i")
+        cmd += ["-e", expr, self.filename]
+        return process.pipe(cmd)
+
+    def sub(self, pat, repl, count=0, inplace=True):
+        """Run a regexp subs. on the file contents
+        Args:
+            inplace: If the contents shall be directly replaced
+        Returns:
+            The new value
+        """
+        flags |= re.MULTILINE
+        newval = re.sub(pat, repl, self.read(), count)
+        if inplace:
+            self.write(newval)
+        return newval
+
+    def findall(self, pat, flags=0):
+        """Find all regexps in the contents
+        """
+        flags |= re.MULTILINE
+        return re.findall(pat, self.read(), flags)
+
     def __iter__(self):
         with open(self.filename, "r") as src:
             for line in src:
@@ -172,6 +202,10 @@
         >>> FakeFs.File("foo").write("bar")
         >>> FakeFs.filemap
         {'foo': 'bar'}
+
+        >>> FakeFs.File("foo").sub("b(ar)", r"ro\\1")
+        'roar'
+
         >>> FakeFs.erase()
         >>> FakeFs.filemap
         {}
@@ -204,6 +238,13 @@
         def access(self, mode):
             return self.filename in FakeFs.filemap
 
+        def sed(self, expr, inplace=True):
+            newval = process.pipe(["sed", "-e", expr],
+                                  stdin=self.read())
+            if inplace:
+                self.write(newval)
+            return newval
+
         def __iter__(self):
             for line in StringIO.StringIO(self.read()):
                 yield line


-- 
To view, visit http://gerrit.ovirt.org/24760
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2e2f7d9578fb9b00b44d78a93f43f7c5198d66e4
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: node-3.0
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list