[elbe-devel] [PATCH 1/1] Fix ebase.text expected behavior

dion at linutronix.de dion at linutronix.de
Fri Jun 28 11:54:31 CEST 2019


From: Olivier Dion <dion at linutronix.de>

When passing *default* as a keyword argument to 'ebase.text', we
expect *default* to be return if the path to the node _doesn't_
exists.

There's a couple of places where the expected behavior is different.
By passing a _dict_ object like as default and passing a second
keyword argument called *key*.

Thus, here's the convention:

  1) Is *default* a _dict_ like object, i.e. does it implement the
     '__getitem__' method?
  2) If 1) is True, is there a second keyword called *key*?
  3) If 2) is True then get the item  *default[key]*.
  4) If 1) or 2) are False , then return *default*.

This is a weird implementation .. it would make much more sense to
pass *default=my_dict[key]* directly instead of *default=my_dict,
key=key*.

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/treeutils.py | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/elbepack/treeutils.py b/elbepack/treeutils.py
index 0caea6d5..b1ac3711 100644
--- a/elbepack/treeutils.py
+++ b/elbepack/treeutils.py
@@ -38,16 +38,16 @@ class ebase(object):
     def __init__(self, et):
         self.et = et
 
-    def text(self, path, **args):
+    def text(self, path, **kwargs):
         el = self.et.find("./" + path)
-        if (el is None) and "default" not in args:
-            raise Exception("Cant find path %s" % path)
-        elif (el is None) and "default" in args:
-            default = args["default"]
-            if isinstance(default, str):
+        if el is None:
+            if "default" in kwargs:
+                default = kwargs["default"]
+                if hasattr(default, "__getitem__") and "key" in kwargs:
+                    return default[kwargs["key"]]
                 return default
 
-            return default[args["key"]]
+            raise Exception("Cant find path %s" % path)
 
         return el.text
 
-- 
2.11.0




More information about the elbe-devel mailing list