From 430424aac0e407748e1241b7a610fff5dace5a99 Mon Sep 17 00:00:00 2001 From: IWASE Yusuke Date: Tue, 22 Nov 2016 11:20:20 +0900 Subject: [PATCH] stringify: Add optional attributes list to be displayed Currently, propery type attributes are ignored in the str and json representations. If we want to include them, it is required to override to_jsondict() and from_jsondict() methods. This patch adds the optional attributes list to specify the addtional attributes included in the str and json representations. Signed-off-by: IWASE Yusuke Signed-off-by: FUJITA Tomonori --- ryu/lib/stringify.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py index 81eb6178..94ffbbcb 100644 --- a/ryu/lib/stringify.py +++ b/ryu/lib/stringify.py @@ -144,6 +144,15 @@ class StringifyMixin(object): _class_prefixes = [] _class_suffixes = [] + # List of attributes ignored in the str and json representations. + _base_attributes = [] + + # Optional attributes included in the str and json representations. + # e.g.) In case of attributes are property, the attributes will be + # skipped in the str and json representations. + # Then, please specify the attributes into this list. + _opt_attributes = [] + def stringify_attrs(self): """an override point for sub classes""" return obj_python_attrs(self) @@ -368,14 +377,17 @@ def obj_python_attrs(msg_): yield(k, getattr(msg_, k)) return base = getattr(msg_, '_base_attributes', []) + opt = getattr(msg_, '_opt_attributes', []) for k, v in inspect.getmembers(msg_): - if k.startswith('_'): + if k in opt: + pass + elif k.startswith('_'): continue - if callable(v): + elif callable(v): continue - if k in base: + elif k in base: continue - if hasattr(msg_.__class__, k): + elif hasattr(msg_.__class__, k): continue yield (k, v)