[PATCH] hwsim tests: add magic function call to wpasupplicant wrapper class

Johannes Berg johannes at sipsolutions.net
Thu Jan 23 15:19:18 EST 2014


From: Johannes Berg <johannes.berg at intel.com>

To make some code simpler, instead of having to wrap every single
wpa_supplicant control interface call, add some magic to the
class to allow calling anything by name. Allow passing arbitrary
arguments and keywords as well.

For example, this allows changing:
  dev[0].request("RADIO_WORK add test-work-b freq=2417")
to
  dev[0].radio_work.add("test-work-b", freq=2417)

Signed-hostap: Johannes Berg <johannes.berg at intel.com>
---
 tests/hwsim/wpasupplicant.py | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/tests/hwsim/wpasupplicant.py b/tests/hwsim/wpasupplicant.py
index 082704b..ba9c3ff 100644
--- a/tests/hwsim/wpasupplicant.py
+++ b/tests/hwsim/wpasupplicant.py
@@ -16,6 +16,21 @@ import wpaspy
 logger = logging.getLogger()
 wpas_ctrl = '/var/run/wpa_supplicant'
 
+class _Request:
+    def __init__(self, request, name):
+        self._request = request
+        self._name = name
+    def __getattr__(self, name):
+        return _Request(self._request, "%s %s" % (self._name, name))
+    def __call__(self, *args, **kwargs):
+        args = list(args)
+        if args:
+            args = [str(a) for a in list(args)]
+        for n, v in kwargs.iteritems():
+            args.append('%s=%s' % (n, str(v)))
+        args = [self._name] + args
+        return self._request(' '.join(args))
+
 class WpaSupplicant:
     def __init__(self, ifname=None, global_iface=None):
         self.group_ifname = None
@@ -730,9 +745,6 @@ class WpaSupplicant:
         if ev is None:
             raise Exception("Association with the AP timed out")
 
-    def relog(self):
-        self.request("RELOG")
-
     def wait_completed(self, timeout=10):
         for i in range(0, timeout * 2):
             if self.get_status_field("wpa_state") == "COMPLETED":
@@ -769,3 +781,20 @@ class WpaSupplicant:
             vals['opportunistic'] = opportunistic
             return vals
         return None
+
+    # objects should always be nonzero
+    def __nonzero__(self):
+        return 1
+
+    def __getattr__(self, name):
+        return _Request(self.request, name.upper())
+
+_real_methods = {}
+for _n in dir(WpaSupplicant):
+    _f = getattr(WpaSupplicant, _n)
+    if callable(_f):
+        _real_methods[_n] = _f
+def _ctrl__call__(self, attr):
+    if attr in _real_methods:
+        return _real_methods[attr]
+WpaSupplicant.__call__ = _ctrl__call__
-- 
1.8.5.2



More information about the HostAP mailing list