[node-patches] Change in ovirt-node[master]: Add ovirt.node.files.BackupedFiles ctx manager

fabiand at fedoraproject.org fabiand at fedoraproject.org
Tue Dec 11 20:09:33 UTC 2012


Fabian Deutsch has uploaded a new change for review.

Change subject: Add ovirt.node.files.BackupedFiles ctx manager
......................................................................

Add ovirt.node.files.BackupedFiles ctx manager

Change-Id: Id8e44560feacdcd1a56322345f22bf16a3eb73da
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/plugins/__init__.py
M scripts/tui/src/ovirt/node/tui.py
M scripts/tui/src/ovirt/node/utils.py
3 files changed, 79 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/74/9874/1

diff --git a/scripts/tui/src/ovirt/node/plugins/__init__.py b/scripts/tui/src/ovirt/node/plugins/__init__.py
index bf2f087..4094a85 100644
--- a/scripts/tui/src/ovirt/node/plugins/__init__.py
+++ b/scripts/tui/src/ovirt/node/plugins/__init__.py
@@ -172,6 +172,7 @@
         self.on_change(change)
         self._changes.update(change)
         LOGGER.debug(self._changes)
+        return True
 
     def _on_ui_save(self):
         """Called when data should be saved
diff --git a/scripts/tui/src/ovirt/node/tui.py b/scripts/tui/src/ovirt/node/tui.py
index 0b22473..255b901 100644
--- a/scripts/tui/src/ovirt/node/tui.py
+++ b/scripts/tui/src/ovirt/node/tui.py
@@ -101,6 +101,7 @@
                     plugin.validate(path, new_value)
                     plugin._on_ui_change({path: new_value})
                     widget.notice = ""
+                    # FIXME change state of Save button
 
                 except ovirt.node.plugins.Concern as e:
                     LOGGER.error("Concern when updating: %s" % e)
@@ -154,9 +155,6 @@
             widgets.append(("flow", widget))
 
         if config["save_button"]:
-#            save = urwid.Button("Save", lambda x: plugin._on_ui_save())
-#            save = urwid.Padding(save, "left", width=8)
-#            save = urwid.Filler(save, ("fixed top", 1))
             save = ovirt.node.widgets.Button("Save")
             urwid.connect_signal(save, 'click', lambda x: plugin._on_ui_save())
             widgets.append(urwid.Filler(save))
diff --git a/scripts/tui/src/ovirt/node/utils.py b/scripts/tui/src/ovirt/node/utils.py
index b2274be..93ebfdc 100644
--- a/scripts/tui/src/ovirt/node/utils.py
+++ b/scripts/tui/src/ovirt/node/utils.py
@@ -4,6 +4,8 @@
 
 import subprocess
 import logging
+import shutil
+import os
 
 LOGGER = logging.getLogger(__name__)
 
@@ -75,3 +77,78 @@
         process.stdin.write(stdin)
     while process.poll() != 0:
         yield process.stdout.readline()
+
+
+def copy_contents(src, dst):
+    assert all([os.path.isfile(f) for f in [src, dst]]), \
+           "Source and destination need to exist"
+    with open(src, "r") as srcf, open(dst, "wb") as dstf:
+        dstf.write(srcf.read())
+
+
+class BackupedFiles(object):
+    """This context manager can be used to keep backup of files while messing
+    with them.
+
+    >>> txt = "Hello World!"
+    >>> dst = "example.txt"
+    >>> with open(dst, "w") as f:
+    ...     f.write(txt)
+    >>> txt == open(dst).read()
+    True
+
+    >>> with BackupedFiles([dst]) as backup:
+    ...     b = backup.of(dst)
+    ...     txt == open(b).read()
+    True
+
+    >>> with BackupedFiles([dst]) as backup:
+    ...     try:
+    ...         open(dst, "w").write("Argh ...").close()
+    ...         raise Exception("Something goes wrong ...")
+    ...     except:
+    ...         backup.restore(dst)
+    >>> txt == open(dst).read()
+    True
+
+    >>> os.remove(dst)
+    """
+
+    files = []
+    backups = {}
+    suffix = ".backup"
+
+    def __init__(self, files, suffix=".backup"):
+        assert type(files) is list, "A list of files is required"
+        assert all([os.path.isfile(f) for f in files]), \
+               "Not all files exist: %s" % files
+        self.files = files
+        self.suffix = suffix
+
+    def __enter__(self):
+        """Create backups when starting
+        """
+        for fn in self.files:
+            backup = "%s%s" % (fn, self.suffix)
+            assert not os.path.exists(backup)
+            shutil.copy(fn, backup)
+            self.backups[fn] = backup
+        return self
+
+    def __exit__(self, a, b, c):
+        """Remove all backups when done
+        """
+        for fn in self.files:
+            backup = self.backups[fn]
+            os.remove(backup)
+
+    def of(self, fn):
+        """Returns the backup file for the given file
+        """
+        assert fn in self.backups, "No backup for '%s'" % fn
+        return self.backups[fn]
+
+    def restore(self, fn):
+        """Restore contens of a previously backupe file
+        """
+        copy_contents(self.of(fn), fn)


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

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



More information about the node-patches mailing list