[Spice-devel,v2,35/43] Introduce a class to handle wireshark attributes

Submitted by Frediano Ziglio on July 8, 2015, 1:54 p.m.

Details

Message ID 1436363656-4266-36-git-send-email-fziglio@redhat.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Frediano Ziglio July 8, 2015, 1:54 p.m.
---
 python_modules/dissector.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

Patch hide | download patch | download mbox

diff --git a/python_modules/dissector.py b/python_modules/dissector.py
index d623576..827f7f0 100644
--- a/python_modules/dissector.py
+++ b/python_modules/dissector.py
@@ -25,6 +25,45 @@  hf_writer = None
 hf_defs = None
 
 
+# handle wireshark attributes
+# is quite complex as attributes ca come from
+# member or array
+# - pointers, have their attributes
+# - array, get from member
+# - primitive or structure from array, specific attributes or type
+# - primitive or structure not in array, member or type
+class WSAttributes:
+    def __init__(self, t, other=None):
+        self.add_attrs = other
+        self.attrs = t.attributes
+
+    def _getattr(self, name, default=[None,None]):
+        if self.add_attrs and name in self.add_attrs:
+            return self.add_attrs[name]
+        return self.attrs.get(name, default)
+
+    def __getattr__(self, name):
+        if name == 'name':
+            val = self._getattr('ws_name')[0]
+            if val is None:
+                val = self._getattr('ws')[1]
+        elif name == 'desc':
+            val = self._getattr('ws_desc')[0]
+            if val is None:
+                val = self._getattr('ws')[0]
+        elif name in {'type', 'base'}:
+            val = self._getattr('ws_' + name)[0]
+        elif name in {'txt', 'txt_n'}:
+            val = self._getattr('ws_' + name,None)
+        else:
+            raise AttributeError('Attribute %s not supported' % name)
+        self.__dict__[name] = val
+        return val
+
+    def has_txts(self):
+        return self.txt is not None or self.txt_n is not None
+
+
 def write_parser_helpers(writer):
     if writer.is_generated("helper", "demarshaller"):
         return