diff --git a/coolamqp/framing/compilation/xml_fields.py b/coolamqp/framing/compilation/xml_fields.py index d6dfe9b3d70a1dfb2562ff751bcb83e79a367566..5609cb81e2e706c027f6c30e8f8f56ceafd2f7fa 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 89c91cd45ac6e312bfdb55d51805ad0eb3c92765..6397e946d48d24923c980b2af137b297097b5223 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) ]