def test_tuple_unmarshall(self):
        xml = """
        <foo>
            <boo>
                <bar>abc</bar>
                <baz>1</baz>
            </boo>
            <boo>
                <bar>qwe</bar>
                <baz>2</baz>
            </boo>
        </foo>
        """
        span = SimpleXMLElement(xml)
        d = {'foo': {'boo': ({'bar': str, 'baz': int}, )}}

        e = {
            'foo': {
                'boo': (
                    {
                        'bar': 'abc',
                        'baz': 1
                    },
                    {
                        'bar': 'qwe',
                        'baz': 2
                    },
                )
            }
        }
        self.eq(span.unmarshall(d), e)
Ejemplo n.º 2
0
def desserializar(xml):
    "Analiza un XML y devuelve un diccionario"
    xml = SimpleXMLElement(xml)

    dic = xml.unmarshall(XML_FORMAT, strict=True)
    
    regs = []

    for dic_comprobante in dic['comprobantes']:
        reg = {
            'detalles': [],
            'ivas': [],
            'tributos': [],
            'permisos': [],
            'cmps_asocs': [],
            }
        comp = dic_comprobante['comprobante']
        mapear(reg, comp, MAP_ENC)
        reg['forma_pago']= ''.join([d['formapago']['descripcion'] for d in comp['formaspago']])


        for detalles in comp['detalles']:
            det = detalles['detalle']
            reg['detalles'].append(mapear({}, det, MAP_DET))
            
        for ivas in comp['ivas']:
            iva = ivas['iva']
            reg['ivas'].append(mapear({}, iva, MAP_IVA))

        for tributos in comp['tributos']:
            tributo = tributos['tributo']
            reg['tributos'].append(mapear({}, tributo, MAP_TRIB))

        regs.append(reg)
    return regs
    def test_basic(self):
        span = SimpleXMLElement(
            '<span><a href="python.org.ar">pyar</a>'
            '<prueba><i>1</i><float>1.5</float></prueba></span>')
        span1 = SimpleXMLElement('<span><a href="google.com">google</a>'
                                 '<a>yahoo</a><a>hotmail</a></span>')
        self.eq([str(a) for a in span1.a()], ['google', 'yahoo', 'hotmail'])

        span1.add_child('a', 'altavista')
        span1.b = "ex msn"
        d = {'href': 'http://www.bing.com/', 'alt': 'Bing'}
        span1.b[:] = d
        self.eq(sorted([(k, v) for k, v in span1.b[:]]), sorted(d.items()))

        xml = ('<?xml version="1.0" encoding="UTF-8"?><span>'
               '<a href="google.com">google</a><a>yahoo</a>'
               '<a>hotmail</a><a>altavista</a>'
               '<b alt="Bing" href="http://www.bing.com/">ex msn</b></span>')
        self.eq(span1.as_xml(), xml if PY2 else xml.encode('utf-8'))
        self.assertTrue('b' in span1)

        span.import_node(span1)
        xml = (
            '<?xml version="1.0" encoding="UTF-8"?><span>'
            '<a href="python.org.ar">pyar</a><prueba><i>1</i>'
            '<float>1.5</float></prueba><span><a href="google.com">google</a>'
            '<a>yahoo</a><a>hotmail</a><a>altavista</a>'
            '<b alt="Bing" href="http://www.bing.com/">ex msn</b>'
            '</span></span>')
        self.eq(span.as_xml(), xml if PY2 else xml.encode('utf-8'))

        types = {'when': datetime.datetime}
        when = datetime.datetime.now()
        dt = SimpleXMLElement('<when>%s</when>' % when.isoformat())
        self.eq(dt.unmarshall(types)['when'], when)
Ejemplo n.º 4
0
def desserializar(xml):
    "Analiza un XML y devuelve un diccionario"
    xml = SimpleXMLElement(xml)

    dic = xml.unmarshall(XML_FORMAT, strict=True)
    
    regs = []

    for dic_comprobante in dic['comprobantes']:
        reg = {
            'detalles': [],
            'ivas': [],
            'tributos': [],
            'permisos': [],
            'cmps_asocs': [],
            }
        comp = dic_comprobante['comprobante']
        mapear(reg, comp, MAP_ENC)
        reg['forma_pago']= ''.join([d['formapago']['descripcion'] for d in comp['formaspago']])


        for detalles in comp['detalles']:
            det = detalles['detalle']
            reg['detalles'].append(mapear({}, det, MAP_DET))
            
        for ivas in comp['ivas']:
            iva = ivas['iva']
            reg['ivas'].append(mapear({}, iva, MAP_IVA))

        for tributos in comp['tributos']:
            tributo = tributos['tributo']
            reg['tributos'].append(mapear({}, tributo, MAP_TRIB))

        regs.append(reg)
    return regs
    def test_adv_unmarshall(self):
        xml = """
        <activations>
            <items>
                <number>01234</number>
                <status>1</status>
                <properties>
                    <name>foo</name>
                    <value>3</value>
                </properties>
                <properties>
                    <name>bar</name>
                    <value>4</value>
                </properties>
            </items>
            <items>
                <number>04321</number>
                <status>0</status>
            </items>
        </activations>
        """
        span = SimpleXMLElement(xml)
        d = {
            'activations': [{
                'items': {
                    'number': str,
                    'status': int,
                    'properties': ({
                        'name': str,
                        'value': int
                    }, )
                }
            }]
        }

        e = {
            'activations': [{
                'items': {
                    'number':
                    '01234',
                    'status':
                    1,
                    'properties': ({
                        'name': 'foo',
                        'value': 3
                    }, {
                        'name': 'bar',
                        'value': 4
                    })
                }
            }, {
                'items': {
                    'number': '04321',
                    'status': 0
                }
            }]
        }
        self.eq(span.unmarshall(d), e)
Ejemplo n.º 6
0
 def test_multiple_element_unmarshall_one(self):
     xml = """
     <results>
         <foo>bar</foo>
     </results>
     """
     span = SimpleXMLElement(xml)
     d = {'results': {'foo': [str]}}
     e = {'results': {'foo': ['bar']}}
     self.eq(span.unmarshall(d), e)
Ejemplo n.º 7
0
 def test_multiple_element_unmarshall_one(self):
     xml = """
     <results>
         <foo>bar</foo>
     </results>
     """
     span = SimpleXMLElement(xml)
     d = {'results': {'foo': [str]}}
     e = {'results': {'foo': ['bar']}}
     self.eq(span.unmarshall(d), e)
    def test_unmarshall(self):
        span = SimpleXMLElement('<span><name>foo</name><value>3</value></span>')
        d = {'span': {'name': str, 'value': int}}
        e = {'span': {'name': 'foo', 'value': 3}}
        self.eq(span.unmarshall(d), e)

        span = SimpleXMLElement('<span><name>foo</name><name>bar</name></span>')
        d = {'span': [{'name': str}]}
        e = {'span': [{'name': 'foo'}, {'name': 'bar'}]}
        self.eq(span.unmarshall(d), e)

        span = SimpleXMLElement('<activations><items><number>01234</number><status>1</status></items><items><number>04321</number><status>0</status></items></activations>')
        d = {'activations': [
                {'items': {
                    'number': str,
                    'status': int
                }}
            ]}

        e = {'activations': [{'items': {'number': '01234', 'status': 1}}, {'items': {'number': '04321', 'status': 0}}]}
        self.eq(span.unmarshall(d), e)
Ejemplo n.º 9
0
    def test_unmarshall(self):
        span = SimpleXMLElement('<span><name>foo</name><value>3</value></span>')
        d = {'span': {'name': str, 'value': int}}
        e = {'span': {'name': 'foo', 'value': 3}}
        self.eq(span.unmarshall(d), e)

        span = SimpleXMLElement('<span><name>foo</name><name>bar</name></span>')
        d = {'span': [{'name': str}]}
        e = {'span': [{'name': 'foo'}, {'name': 'bar'}]}
        self.eq(span.unmarshall(d), e)

        span = SimpleXMLElement('<activations><items><number>01234</number><status>1</status></items><items><number>04321</number><status>0</status></items></activations>')
        d = {'activations': [
                {'items': {
                    'number': str,
                    'status': int
                }}
            ]}

        e = {'activations': [{'items': {'number': '01234', 'status': 1}}, {'items': {'number': '04321', 'status': 0}}]}
        self.eq(span.unmarshall(d), e)
Ejemplo n.º 10
0
def desserializar(xml):
    "Analiza un XML y devuelve un diccionario"
    xml = SimpleXMLElement(xml)

    dic = xml.unmarshall(XML_FORMAT, strict=True)

    regs = []

    for dic_comprobante in dic["comprobantes"]:
        reg = {
            "detalles": [],
            "ivas": [],
            "tributos": [],
            "permisos": [],
            "cbtes_asoc": [],
            "opcionales": [],
        }
        comp = dic_comprobante["comprobante"]
        mapear(reg, comp, MAP_ENC)
        reg["forma_pago"] = "".join(
            [d["formapago"]["descripcion"] for d in comp["formaspago"]])

        for detalles in comp["detalles"]:
            det = detalles["detalle"]
            reg["detalles"].append(mapear({}, det, MAP_DET))

        for ivas in comp["ivas"]:
            iva = ivas["iva"]
            reg["ivas"].append(mapear({}, iva, MAP_IVA))

        for tributos in comp.get("tributos", []):
            tributo = tributos["tributo"]
            reg["tributos"].append(mapear({}, tributo, MAP_TRIB))

        for cbte_asocs in comp.get("cmpasociados", []):
            cbte_asoc = cbte_asocs["cmpasociado"]
            reg["cbtes_asoc"].append(mapear({}, cbte_asoc, MAP_CBTE_ASOC))

        for opcionales in comp.get("opcionales", []):
            opcional = opcionales["opcional"]
            reg["opcionales"].append(mapear({}, opcional, MAP_OPCIONAL))

        regs.append(reg)
    return regs
    def test_adv_unmarshall(self):
        xml = """
        <activations>
            <items>
                <number>01234</number>
                <status>1</status>
                <properties>
                    <name>foo</name>
                    <value>3</value>
                </properties>
                <properties>
                    <name>bar</name>
                    <value>4</value>
                </properties>
            </items>
            <items>
                <number>04321</number>
                <status>0</status>
            </items>
        </activations>
        """
        span = SimpleXMLElement(xml)
        d = {'activations': [
                {'items': {
                    'number': str,
                    'status': int,
                    'properties': ({
                        'name': str,
                        'value': int
                    }, )
                }}
            ]}

        e = {'activations': [
                {'items': {'number': '01234', 'status': 1, 'properties': ({'name': 'foo', 'value': 3}, {'name': 'bar', 'value': 4})}},
                {'items': {'number': '04321', 'status': 0}}
            ]}
        self.eq(span.unmarshall(d), e)
    def test_basic(self):
        span = SimpleXMLElement('<span><a href="python.org.ar">pyar</a><prueba><i>1</i><float>1.5</float></prueba></span>')
        span1 = SimpleXMLElement('<span><a href="google.com">google</a><a>yahoo</a><a>hotmail</a></span>')
        self.eq([str(a) for a in span1.a()], ['google', 'yahoo', 'hotmail'])

        span1.add_child('a', 'altavista')
        span1.b = "ex msn"
        d = {'href': 'http://www.bing.com/', 'alt': 'Bing'}
        span1.b[:] = d
        self.eq(sorted([(k, v) for k, v in span1.b[:]]), sorted(d.items()))

        xml = '<?xml version="1.0" encoding="UTF-8"?><span><a href="google.com">google</a><a>yahoo</a><a>hotmail</a><a>altavista</a><b alt="Bing" href="http://www.bing.com/">ex msn</b></span>'
        self.eq(span1.as_xml(), xml)
        self.assertTrue('b' in span1)

        span.import_node(span1)
        xml = '<?xml version="1.0" encoding="UTF-8"?><span><a href="python.org.ar">pyar</a><prueba><i>1</i><float>1.5</float></prueba><span><a href="google.com">google</a><a>yahoo</a><a>hotmail</a><a>altavista</a><b alt="Bing" href="http://www.bing.com/">ex msn</b></span></span>'
        self.eq(span.as_xml(), xml)

        types = {'when': datetime.datetime}
        when = datetime.datetime.now()
        dt = SimpleXMLElement('<when>%s</when>' % when.isoformat())
        self.eq(dt.unmarshall(types)['when'], when)
    def test_tuple_unmarshall(self):
        xml = """
        <foo>
            <boo>
                <bar>abc</bar>
                <baz>1</baz>
            </boo>
            <boo>
                <bar>qwe</bar>
                <baz>2</baz>
            </boo>
        </foo>
        """
        span = SimpleXMLElement(xml)
        d = {'foo': {
                'boo': ({'bar': str, 'baz': int}, )
        }}

        e = {'foo': {
                'boo': (
                {'bar': 'abc', 'baz': 1},
                {'bar': 'qwe', 'baz': 2},
            )}}
        self.eq(span.unmarshall(d), e)
 def test_unmarshall_cdata(self):
     span = SimpleXMLElement(
         '<span><name><![CDATA[foo<name/>]]></name><value>3</value></span>')
     d = {'span': {'name': str, 'value': int}}
     e = {'span': {'name': 'foo<name/>', 'value': 3}}
     self.eq(span.unmarshall(d), e)
Ejemplo n.º 15
0
 def test_unmarshall_cdata(self):
     span = SimpleXMLElement('<span><name><![CDATA[foo<name/>]]></name><value>3</value></span>')
     d = {'span': {'name': str, 'value': int}}
     e = {'span': {'name': 'foo<name/>', 'value': 3}}
     self.eq(span.unmarshall(d), e)