Introduce better item getter helper in query.py
authorIustin Pop <iustin@google.com>
Mon, 11 Mar 2013 09:13:24 +0000 (10:13 +0100)
committerIustin Pop <iustin@google.com>
Tue, 12 Mar 2013 16:16:03 +0000 (17:16 +0100)
And also use it for simple network fields. Currently the Python
library returns jsnull, which is formatted as 'None' by the Python
client, which (IMHO) is not nice, since these fields are strings (some
of them) or "other". An undeclared gateway is easier to diagnose when
properly reported by the server as missing, rather than "existing but
null".

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>

lib/query.py

index 37bf114..2b5fc4a 100644 (file)
@@ -977,6 +977,23 @@ def _GetItemAttr(attr):
   return lambda _, item: getter(item)
 
 
+def _GetItemMaybeAttr(attr):
+  """Returns a field function to return a not-None attribute of the item.
+
+  If the value is None, then C{_FS_UNAVAIL} will be returned instead.
+
+  @param attr: Attribute name
+
+  """
+  def _helper(_, obj):
+    val = getattr(obj, attr)
+    if val is None:
+      return _FS_UNAVAIL
+    else:
+      return val
+  return _helper
+
+
 def _GetNDParam(name):
   """Return a field function to return an ND parameter out of the context.
 
@@ -2614,7 +2631,7 @@ def _BuildNetworkFields():
   # Add simple fields
   fields.extend([
     (_MakeField(name, title, kind, doc),
-     NETQ_CONFIG, 0, _GetItemAttr(name))
+     NETQ_CONFIG, 0, _GetItemMaybeAttr(name))
      for (name, (title, kind, _, doc)) in _NETWORK_SIMPLE_FIELDS.items()])
 
   def _GetLength(getter):