Projet

Général

Profil

0005-cityweb-add-json-to-xml-converter-function-15883.patch

Josué Kouka, 31 mai 2017 13:53

Télécharger (3,05 ko)

Voir les différences:

Subject: [PATCH 5/6] cityweb: add json to xml converter function (#15883)

 passerelle/contrib/cityweb/utils.py | 63 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)
passerelle/contrib/cityweb/utils.py
15 15

  
16 16
import os
17 17
import zipfile
18
from xml.etree import ElementTree as etree
19
from xml.dom import minidom
18 20

  
19 21
from django.core.files.storage import default_storage
20 22

  
21 23

  
24
class ElementFactory(etree.Element):
25

  
26
    def __init__(self, *args, **kwargs):
27
        self.text = kwargs.pop('text', None)
28
        namespace = kwargs.pop('namespace', None)
29
        if namespace:
30
            super(ElementFactory, self).__init__(
31
                etree.QName(namespace, args[0]), **kwargs
32
            )
33
            self.namespace = namespace
34
        else:
35
            super(ElementFactory, self).__init__(*args, **kwargs)
36

  
37
    def __unicode__(self):
38
        if isinstance(self.tag, (etree.QName,)):
39
            return self.tag.text
40
        return self.tag
41

  
42
    __repr__ = __str__ = __unicode__
43

  
44
    def append(self, element, allow_new=True):
45
        if not allow_new:
46
            if isinstance(element.tag, etree.QName):
47
                found = self.find(element.tag.text)
48
            else:
49
                found = self.find(element.tag)
50
            if found is not None:
51
                return self
52
        super(ElementFactory, self).append(element)
53
        return self
54

  
55
    def to_pretty_xml(self):
56
        raw = etree.tostring(self)
57
        parsed = minidom.parseString(raw)
58
        return parsed.toprettyxml(indent='\t')
59

  
60

  
22 61
def flatten_payload(*subs):
23 62
    result = {}
24 63
    for sub in subs:
25
        result.update(sub)  # priority on last sub dict
64
        if not isinstance(sub, (dict,)):
65
            continue
66
        result.update(sub)  # priority to the last sub dict
26 67
    return result
27 68

  
28 69

  
......
40 81
                fpath = os.path.join(root, f)
41 82
                zipf.write(fpath, os.path.basename(fpath))
42 83
    return archname
84

  
85

  
86
def json_to_xml(path, value, parent, namespace=None):
87
    if isinstance(path, list) and len(path) > 1:
88
        if namespace:
89
            tag = '{%s}%s' % (namespace, path[0])
90
        else:
91
            tag = path[0]
92
        # parent_child = parent.find('{%s}%s' % (namespace, path[0]))
93
        parent_child = parent.find(tag)
94
        if parent_child is not None:
95
            element = parent_child
96
            path.pop(0)
97
        else:
98
            element = ElementFactory(path.pop(0), namespace=namespace)
99
        element.append(json_to_xml(path, value, element, namespace=namespace),
100
                       allow_new=False)
101
    else:
102
        element = ElementFactory(path[0], text=value, namespace=namespace)
103
    return element
43
-