From a86c3c09557a31ebace99775be0e04b2a0c2b22f Mon Sep 17 00:00:00 2001
From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl>
Date: Sun, 8 Oct 2017 10:33:53 +0200
Subject: [PATCH] gfh

---
 coolamqp/framing/compilation/xml_fields.py | 13 ++++++++++++-
 coolamqp/framing/compilation/xml_tags.py   | 19 ++++++++-----------
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/coolamqp/framing/compilation/xml_fields.py b/coolamqp/framing/compilation/xml_fields.py
index d6dfe9b..5609cb8 100644
--- a/coolamqp/framing/compilation/xml_fields.py
+++ b/coolamqp/framing/compilation/xml_fields.py
@@ -12,7 +12,7 @@ def nop(x):
 
 __all__ = [
     '_name', '_docs', '_ComputedField', '_ValueField', '_SimpleField',
-    '_docs_with_label'
+    '_docs_with_label', '_get_tagchild', '_ChildField'
 ]
 
 class _Field(object):
@@ -73,6 +73,17 @@ class _SimpleField(_ValueField):
     def __init__(self, name, field_type=nop, default=_Required):
         super(_SimpleField, self).__init__(name, name, field_type, default)
 
+def _get_tagchild(elem, tag):
+    return [e for e in elem.getchildren() if e.tag == tag]
+
+
+class _ChildField(_ComputedField):
+    """
+    List of other properties
+    """
+    def __init__(self, name, xml_tag, fun, postexec=nop):
+        super(_ChildField, self).__init__(name, lambda elem: postexec(map(fun, _get_tagchild(elem, xml_tag))))
+
 
 def get_docs(elem, label):
     """Parse an XML element. Return documentation"""
diff --git a/coolamqp/framing/compilation/xml_tags.py b/coolamqp/framing/compilation/xml_tags.py
index 89c91cd..6397e94 100644
--- a/coolamqp/framing/compilation/xml_tags.py
+++ b/coolamqp/framing/compilation/xml_tags.py
@@ -62,9 +62,9 @@ class Class(BaseObject):
         _name,
         _SimpleField('index', int),
         _docs_with_label,
-        _ComputedField('methods', lambda elem: sorted(map(Method, _get_tagchild(elem, 'method')),
+        _ChildField('methods', 'method', Method, postexec=lambda q: sorted(q,
             key=lambda m: (m.name.strip('-')[0], -len(m.response)))),
-        _ComputedField('properties', lambda elem: map(Field, _get_tagchild(elem, 'field')))
+        _ChildField('properties', 'field', Field)
     ]
 
 
@@ -77,8 +77,7 @@ class Domain(BaseObject):
     ]
 
 
-def _get_tagchild(elem, tag):
-    return [e for e in elem.getchildren() if e.tag == tag]
+
 
 class Method(BaseObject):
     NAME = 'method'
@@ -88,13 +87,11 @@ class Method(BaseObject):
         _SimpleField('index', int),
         _SimpleField('label', default=None),
         _docs,
-        _ComputedField('fields', lambda elem: [Field(fie) for fie in _get_tagchild(elem, 'field')]),
-        _ComputedField('response', lambda elem: [e.attrib['name'] for e in elem.findall('response')]),
-        _ComputedField('sent_by_client', lambda elem:  any(e.attrib.get('name', '') == 'server' for e in
-                           _get_tagchild(elem, 'chassis'))),
-        _ComputedField('sent_by_server', lambda elem: any(e.attrib.get('name', '') == 'client' for e in
-                           _get_tagchild(elem, 'chassis'))),
-        _ComputedField('constant', lambda elem: all(Field(fie).reserved for fie in _get_tagchild(elem, 'field'))),
+        _ChildField('fields', 'field', Field),
+        _ChildField('response', 'response', lambda e: e.attrib['name']),
+        _ChildField('sent_by_client', 'chassis', lambda e: e.attrib.get('name', '') == 'client', postexec=any),
+        _ChildField('sent_by_server', 'chassis', lambda e: e.attrib.get('name', '') == 'server', postexec=any),
+        _ChildField('constant', 'field', lambda e: Field(e).reserved, postexec=all)
     ]
 
 
-- 
GitLab