summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2016-02-08 10:55:20 -0500
committerimarom <imarom@cisco.com>2016-02-08 10:55:20 -0500
commit6107c1ca4aa485c5971ff3326513b8f4934f7ac1 (patch)
treea109d80501bd087e3219f68c186fb55bc17e090a /scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
parentf5a5e50bfe046148a20f6ce578d6082119dec2c0 (diff)
huge refactor - again
Diffstat (limited to 'scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py')
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
new file mode 100644
index 00000000..1164076b
--- /dev/null
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_types.py
@@ -0,0 +1,95 @@
+
+from collections import namedtuple
+from utils.text_opts import *
+
+RpcCmdData = namedtuple('RpcCmdData', ['method', 'params'])
+
+class RpcResponseStatus(namedtuple('RpcResponseStatus', ['success', 'id', 'msg'])):
+ __slots__ = ()
+ def __str__(self):
+ return "{id:^3} - {msg} ({stat})".format(id=self.id,
+ msg=self.msg,
+ stat="success" if self.success else "fail")
+
+# simple class to represent complex return value
+class RC():
+
+ def __init__ (self, rc = None, data = None, is_warn = False):
+ self.rc_list = []
+
+ if (rc != None):
+ tuple_rc = namedtuple('RC', ['rc', 'data', 'is_warn'])
+ self.rc_list.append(tuple_rc(rc, data, is_warn))
+
+ def __nonzero__ (self):
+ return self.good()
+
+
+ def add (self, rc):
+ self.rc_list += rc.rc_list
+
+ def good (self):
+ return all([x.rc for x in self.rc_list])
+
+ def bad (self):
+ return not self.good()
+
+ def warn (self):
+ return any([x.is_warn for x in self.rc_list])
+
+ def data (self):
+ d = [x.data if x.rc else "" for x in self.rc_list]
+ return (d if len(d) != 1 else d[0])
+
+ def err (self):
+ e = [x.data if not x.rc else "" for x in self.rc_list]
+ return (e if len(e) != 1 else e[0])
+
+ def __str__ (self):
+ s = ""
+ for x in self.rc_list:
+ if x.data:
+ s += format_text("\n{0}".format(x.data), 'bold')
+ return s
+
+ def prn_func (self, msg, newline = True):
+ if newline:
+ print msg
+ else:
+ print msg,
+
+ def annotate (self, log_func = None, desc = None, show_status = True):
+
+ if not log_func:
+ log_func = self.prn_func
+
+ if desc:
+ log_func(format_text('\n{:<60}'.format(desc), 'bold'), newline = False)
+ else:
+ log_func("")
+
+ if self.bad():
+ # print all the errors
+ print ""
+ for x in self.rc_list:
+ if not x.rc:
+ log_func(format_text("\n{0}".format(x.data), 'bold'))
+
+ print ""
+ if show_status:
+ log_func(format_text("[FAILED]\n", 'red', 'bold'))
+
+
+ else:
+ if show_status:
+ log_func(format_text("[SUCCESS]\n", 'green', 'bold'))
+
+
+def RC_OK(data = ""):
+ return RC(True, data)
+
+def RC_ERR (err):
+ return RC(False, err)
+
+def RC_WARN (warn):
+ return RC(True, warn, is_warn = True)