def _convert_etree_element_to_rule(entry_element): ''' Converts entry element to rule object. The format of xml for rule: <entry xmlns='http://www.w3.org/2005/Atom'> <content type='application/xml'> <RuleDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"> <Filter i:type="SqlFilterExpression"> <SqlExpression>MyProperty='XYZ'</SqlExpression> </Filter> <Action i:type="SqlFilterAction"> <SqlExpression>set MyProperty2 = 'ABC'</SqlExpression> </Action> </RuleDescription> </content> </entry> ''' rule = Rule() rule_element = entry_element.find('./atom:content/sb:RuleDescription', _etree_sb_feed_namespaces) if rule_element is not None: filter_element = rule_element.find('./sb:Filter', _etree_sb_feed_namespaces) if filter_element is not None: rule.filter_type = filter_element.attrib.get( _make_etree_ns_attr_name(_etree_sb_feed_namespaces['i'], 'type'), None) sql_exp_element = filter_element.find('./sb:SqlExpression', _etree_sb_feed_namespaces) if sql_exp_element is not None: rule.filter_expression = sql_exp_element.text action_element = rule_element.find('./sb:Action', _etree_sb_feed_namespaces) if action_element is not None: rule.action_type = action_element.attrib.get( _make_etree_ns_attr_name(_etree_sb_feed_namespaces['i'], 'type'), None) sql_exp_element = action_element.find('./sb:SqlExpression', _etree_sb_feed_namespaces) if sql_exp_element is not None: rule.action_expression = sql_exp_element.text # extract id, updated and name value from feed entry and set them of rule. for name, value in _ETreeXmlToObject.get_entry_properties_from_element( entry_element, True, '/rules').items(): setattr(rule, name, value) return rule
def _convert_etree_element_to_entity(entry_element): ''' Convert xml response to entity. The format of entity: <entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <title /> <updated>2008-09-18T23:46:19.3857256Z</updated> <author> <name /> </author> <id /> <content type="application/xml"> <m:properties> <d:Address>Mountain View</d:Address> <d:Age m:type="Edm.Int32">23</d:Age> <d:AmountDue m:type="Edm.Double">200.23</d:AmountDue> <d:BinaryData m:type="Edm.Binary" m:null="true" /> <d:CustomerCode m:type="Edm.Guid">c9da6455-213d-42c9-9a79-3e9149a57833</d:CustomerCode> <d:CustomerSince m:type="Edm.DateTime">2008-07-10T00:00:00</d:CustomerSince> <d:IsActive m:type="Edm.Boolean">true</d:IsActive> <d:NumOfOrders m:type="Edm.Int64">255</d:NumOfOrders> <d:PartitionKey>mypartitionkey</d:PartitionKey> <d:RowKey>myrowkey1</d:RowKey> <d:Timestamp m:type="Edm.DateTime">0001-01-01T00:00:00</d:Timestamp> </m:properties> </content> </entry> ''' entity = Entity() properties = entry_element.findall('./atom:content/m:properties', _etree_entity_feed_namespaces) for prop in properties: for p in prop: name = _get_etree_tag_name_without_ns(p.tag) value = p.text or '' mtype = p.attrib.get(_make_etree_ns_attr_name(_etree_entity_feed_namespaces['m'], 'type'), None) isnull = p.attrib.get(_make_etree_ns_attr_name(_etree_entity_feed_namespaces['m'], 'null'), None) # if not isnull and no type info, then it is a string and we just # need the str type to hold the property. if not isnull and not mtype: _set_entity_attr(entity, name, value) elif isnull == 'true': if mtype: property = EntityProperty(mtype, None) else: property = EntityProperty('Edm.String', None) else: # need an object to hold the property conv = _ENTITY_TO_PYTHON_CONVERSIONS.get(mtype) if conv is not None: property = conv(value) else: property = EntityProperty(mtype, value) _set_entity_attr(entity, name, property) # extract id, updated and name value from feed entry and set them of # rule. for name, value in _ETreeXmlToObject.get_entry_properties_from_element( entry_element, True).items(): if name in ['etag']: _set_entity_attr(entity, name, value) return entity