def assign_dict(self, element, tag_name, value): new_element = etree.Element(tag_name) # Replaces the previous node with the new one. self._xml.replace(element, new_element) # Copies #text and @attrs from the dict. helpers.dict_to_etree(value, new_element)
def assign_sequence_or_set(element, value): element.clear() for item in value: temp_element = etree.Element('temp') helpers.dict_to_etree(item, temp_element) for child in temp_element.iterchildren(): element.append(child) del temp_element
def assign_dict(self, node, xml_dict): """Assigns a Python dict to a ``lxml`` node. :param node: A node to assign the dict to. :param xml_dict: The dict with attributes/children to use. """ new_node = etree.Element(node.tag) # Replaces the previous node with the new one self._xml.replace(node, new_node) # Copies #text and @attrs from the xml_dict helpers.dict_to_etree(xml_dict, new_node)
def __init__(self, xml): u"""Creates the mappet object from either lxml object, a string or a dict. If you pass a dict without root element, one will be created for you with 'root' as tag name. >>> Mappet({'a': {'#text': 'list_elem_1', '@attr1': 'val1'}}).to_str() '<a attr1="val1">list_elem_1</a>' >>> Mappet({'#text': 'list_elem_1', '@attr1': 'val1'}).to_str() '<root attr1="val1">list_elem_1</root>' """ if etree.iselement(xml): self._xml = xml elif isinstance(xml, basestring): self._xml = etree.fromstring(xml) elif isinstance(xml, dict): if len(xml) == 1: root_name = xml.keys()[0] body = xml[root_name] else: root_name = 'root' body = xml self._xml = helpers.dict_to_etree(body, etree.Element(root_name)) else: raise AttributeError('Specified data cannot be used to construct a Mappet object.')
def __init__(self, xml): u"""Creates the mappet object from either lxml object, a string or a dict.""" if etree.iselement(xml): self._xml = xml elif isinstance(xml, basestring): self._xml = etree.fromstring(xml) elif isinstance(xml, dict): self._xml = helpers.dict_to_etree(xml, etree.Element('root')) else: raise AttributeError('Specified data cannot be used to construct a Mappet object.')
def __init__(self, xml): u"""Creates the mappet object from either lxml object, a string or a dict.""" if etree.iselement(xml): self._xml = xml elif isinstance(xml, basestring): self._xml = etree.fromstring(xml) elif isinstance(xml, dict): self._xml = helpers.dict_to_etree(xml, etree.Element('root')) else: raise AttributeError( 'Specified data cannot be used to construct a Mappet object.')
def report_view_xml(self, id): report, results, total, show_org = self._report_view(id, None, None) file_path = '%s/%s.xml' % (h.get_export_dir(), re.sub('[^a-zA-Z0-9_-]+', '_', report['name'].encode('ascii','ignore'))) if report['report_on'] == "activities": activity_list = [] for row in results: data_name = '' activity_data = {} if 'activity' in row and 'data' in row['activity']: activity = row['activity']['data'] if 'package' in activity: activity_data = activity.get('package') elif 'dataset' in activity: activity_data = activity.get('dataset') elif 'group' in activity: activity_data = activity.get('group') data_name = activity_data.get('name', '') activity_list.append({ '@time': row['activity']['timestamp'], '@user': row['user']['fullname'] or row['user']['name'], '@activity_type': row['activity']['activity_type'], '@data_name': data_name }) tree = h.dict_to_etree({ 'activities' : {'activity' : activity_list }}) response = h.createResponseWithXML(tree, file_path, tk.response) elif report['report_on'] == "details": tree = h.dict_to_etree({ 'users' : {'user' : [ { '@name': row['fullname'] or row['name'], '@email': row['email'], '@state': row['state'], '@organization': row['organization'], '@role': row['capacity'], '@system_administrator': str(row['sysadmin']) } for row in results]}}) response = h.createResponseWithXML(tree, file_path, tk.response) return response
def area_list(request): if request.args.get("zipCode"): # Nintendo, for whatever reason, require a separate "selectedArea" element # as a root node within output. # This violates about every XML specification in existence. # I am reasonably certain there was a mistake as their function to # interpret nodes at levels accepts a parent node, to which they seem to # have passed passed NULL instead of response. # # We are not going to bother spending time to deal with this. @response() def area_list_only_segments(): return { "areaList": { "place": { "segment": "segment title", "list": { "areaPlace": {"areaName": "place name", "areaCode": 2} }, }, }, "areaCount": 1, } area_list_output = area_list_only_segments() selected_area = dict_to_etree("selectedArea", {"areaCode": 1}) selected_area_output = etree.tostring(selected_area, pretty_print=True) return area_list_output + selected_area_output area_code = request.args.get("areaCode") if not area_code: # We expect either a zip code or an area code. return exceptions.BadRequest() if area_code == "0": # An area code of 0 is passed upon first search. return { "areaList": { "place": { "segment": "segment title", "list": {"areaPlace": {"areaName": "place name", "areaCode": 2}}, }, }, "areaCount": 1, } if area_code == "2": # An area code of 0 is passed upon first search. All else is deterministic. return { "areaList": { "place": { "container0": "aaaa", "segment": "area_title", "list": { "areaPlace": { "areaName": "place one", "areaCode": 3, "isNextArea": 0, "display": 1, "kanji1": "title", "kanji2": "kanji2", "kanji3": "two", "kanji4": "three", } }, }, }, "areaCount": 1, } return exceptions.NotFound()