[node-patches] Change in ovirt-node[master]: config: Add chainability for update() calls

fabiand at fedoraproject.org fabiand at fedoraproject.org
Thu Oct 17 10:55:19 UTC 2013


Fabian Deutsch has uploaded a new change for review.

Change subject: config: Add chainability for update() calls
......................................................................

config: Add chainability for update() calls

Previously the following pattern was needed to activate configurations:

    cfg = defaults.Keyboard()
    cfg.update(layout="de")
    cfg.transaction().run()

or
    cfg = defaults.Keyboard()
    cfg.update(layout="de")
    cfg.commit()

now it's possible to chain this up to

    defaults.Keyboard().update(layout="de").commit()

This patch also includes a testcase for this (tiny) feature.

Change-Id: I36a101a7ee6f729241cc99663f2a6986c964dcdd
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M src/ovirt/node/config/defaults.py
A tests/nose/defaults.py
2 files changed, 81 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/63/20263/1

diff --git a/src/ovirt/node/config/defaults.py b/src/ovirt/node/config/defaults.py
index 26afabe..dea95cb 100644
--- a/src/ovirt/node/config/defaults.py
+++ b/src/ovirt/node/config/defaults.py
@@ -153,6 +153,9 @@
         new_dict = dict((k.upper(), v) for k, v in kwargs.items())
         self.raw_file.update(new_dict, remove_empty=True)
 
+        # Returning self allows chaining for decorated functions
+        return self
+
     @staticmethod
     def map_and_update_defaults_decorator(func):
         """
diff --git a/tests/nose/defaults.py b/tests/nose/defaults.py
new file mode 100644
index 0000000..a34363c
--- /dev/null
+++ b/tests/nose/defaults.py
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# defaults.py - Copyright (C) 2013 Red Hat, Inc.
+# Written by Fabian Deutsch <fabiand at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+import logging
+
+from mock import patch
+
+from ovirt.node.config.defaults import NodeConfigFileSection
+from ovirt.node.utils import Transaction
+from ovirt.node.utils.fs import FakeFs
+
+
+class DummyNodeConfigFileSection(NodeConfigFileSection):
+    keys = ("DUMMY_KEY",)
+    txe_counter = 0
+
+    @NodeConfigFileSection.map_and_update_defaults_decorator
+    def update(self, key):
+        pass
+
+    def transaction(self):
+        tx = Transaction("Dummy TX")
+
+        obj = self
+
+        class DummyTXE(Transaction.Element):
+            def commit(self):
+                obj.txe_counter += 1
+
+        tx.append(DummyTXE())
+
+        return tx
+
+    def configure_dummy(self):
+        return self.update(key="default")
+
+
+ at patch("ovirt.node.utils.fs.File", FakeFs.File)
+class TestSanity():
+    """A class to test the basic features of the defaults module
+    """
+
+    defaults_file = FakeFs.File("/etc/default/ovirt")
+
+    def setUp(self):
+        FakeFs.erase()
+        self.defaults_file.touch()
+        logging.basicConfig()
+
+    def test_chaining(self):
+        cfg = DummyNodeConfigFileSection()
+
+        cfg.update("bar").commit()
+
+        assert cfg.txe_counter == 1
+        assert self.defaults_file.read() == 'DUMMY_KEY="bar"\n'
+
+        cfg.configure_dummy().commit()
+
+        assert cfg.txe_counter == 2
+        assert self.defaults_file.read() == 'DUMMY_KEY="default"\n'


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I36a101a7ee6f729241cc99663f2a6986c964dcdd
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