Beispiel #1
0
def test_attr_setnode5():
    EXPECTED = """<a xmlns:ns="urn:bogus:x" ns:x="1" ns:y="1" ns:z=""/>"""
    doc = parse('<a xmlns:ns="urn:bogus:x" ns:x="1"/>')
    attr_y = tree.attribute(BOGUS_X_NS, u'ns:y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(BOGUS_X_NS, u'ns:z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL+EXPECTED)
    return
Beispiel #2
0
def test_attr_setnode2():
    EXPECTED = """<a x="1" y="1" z=""/>"""
    doc = parse('<a x="1"/>')
    attr_y = tree.attribute(None, u'y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(None, u'z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL+EXPECTED)
    return
Beispiel #3
0
def test_attr_setnode5():
    EXPECTED = """<a xmlns:ns="urn:bogus:x" ns:x="1" ns:y="1" ns:z=""/>"""
    doc = parse('<a xmlns:ns="urn:bogus:x" ns:x="1"/>')
    attr_y = tree.attribute(BOGUS_X_NS, u'ns:y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(BOGUS_X_NS, u'ns:z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #4
0
def test_attr_setnode2():
    EXPECTED = """<a x="1" y="1" z=""/>"""
    doc = parse('<a x="1"/>')
    attr_y = tree.attribute(None, u'y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(None, u'z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #5
0
 def __set__(self, obj, value):
     #from amara import bindery; doc = bindery.parse('<a x="1"/>'); doc.a.x = unicode(int(doc.a.x)+1)
     if isinstance(value, basestring):
         attr = tree.attribute(self.ns, self.local, unicode(value))
         obj.xml_attributes.setnode(attr)
     else:
         obj.xml_attributes[self.ns, self.local].xml_value = value
     return
Beispiel #6
0
 def __set__(self, obj, value):
     # from amara import bindery; doc = bindery.parse('<a x="1"/>'); doc.a.x = unicode(int(doc.a.x)+1)
     if isinstance(value, basestring):
         attr = tree.attribute(self.ns, self.local, unicode(value))
         obj.xml_attributes.setnode(attr)
     else:
         obj.xml_attributes[self.ns, self.local].xml_value = value
     return
Beispiel #7
0
 def test_create_new_doc_attr_tree_attr(self):
     EXPECTED = '<A a="b">One</A>'
     doc = tree.entity()
     A = tree.element(None, u'A')
     
     new_attr = tree.attribute(None, u'a', u'b')
     A.xml_attributes.setnode(new_attr)
     A.xml_append(tree.text(u'One'))
     doc.xml_append(A)
     self.compare_output(doc, XMLDECL+EXPECTED)
     return 
Beispiel #8
0
    def test_create_new_doc_attr_tree_attr(self):
        EXPECTED = '<A a="b">One</A>'
        doc = tree.entity()
        A = tree.element(None, u'A')

        new_attr = tree.attribute(None, u'a', u'b')
        A.xml_attributes.setnode(new_attr)
        A.xml_append(tree.text(u'One'))
        doc.xml_append(A)
        self.compare_output(doc, XMLDECL + EXPECTED)
        return
Beispiel #9
0
    def test_attr_assignment(self):
        doc = parse(SANE_DEFAULT_XML, prefixes=SANE_DEFAULT_XML_PREFIXES)
        monty = doc.doc.monty

        # Create attribute node
        attr_node = tree.attribute(u'urn:bogus:a', 'setitem', 'value')
        monty[u'urn:bogus:a', 'setitem'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem')],
                         'value')

        # Check for mismatched namespace
        attr_node = tree.attribute(u'urn:bogus:a', 'setitem2', 'value')

        def f():
            monty[u'urn:wrong-value', 'setitem2'] = attr_node

        self.assertRaises(ValueError, f)

        # Check for mismatched local name
        def f():
            monty[u'urn:bogus:a', 'setitem'] = attr_node

        self.assertRaises(ValueError, f)

        # Test with no namespace supplied on node.
        attr_node = tree.attribute(None, 'setitem3', 'value')
        monty[u'urn:bogus:a', 'setitem3'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem3')],
                         'value')

        # Test with no namespace supplied in key.
        attr_node = tree.attribute(u'urn:bogus:a', 'setitem4', 'value')
        monty[None, 'setitem4'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem4')],
                         'value')

        # Test with no namespace supplied at all.
        attr_node = tree.attribute(None, 'setitem5', 'value')
        monty[None, 'setitem5'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem5')],
                         'value')
Beispiel #10
0
    def test_attr_assignment(self):
        doc = parse(SANE_DEFAULT_XML, prefixes=SANE_DEFAULT_XML_PREFIXES)
        monty = doc.doc.monty

        # Create attribute node 
        attr_node = tree.attribute(u'urn:bogus:a', 'setitem', 'value')
        monty[u'urn:bogus:a', 'setitem'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem')], 
                         'value')

        # Check for mismatched namespace
        attr_node = tree.attribute(u'urn:bogus:a', 'setitem2', 'value')
        def f():
            monty[u'urn:wrong-value', 'setitem2'] = attr_node
        self.assertRaises(ValueError, f)

        # Check for mismatched local name
        def f():
            monty[u'urn:bogus:a', 'setitem'] = attr_node
        self.assertRaises(ValueError, f)

        # Test with no namespace supplied on node.
        attr_node = tree.attribute(None, 'setitem3', 'value')
        monty[u'urn:bogus:a', 'setitem3'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem3')], 
                         'value')

        # Test with no namespace supplied in key.
        attr_node = tree.attribute(u'urn:bogus:a', 'setitem4', 'value')
        monty[None, 'setitem4'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem4')], 
                         'value')

        # Test with no namespace supplied at all.
        attr_node = tree.attribute(None, 'setitem5', 'value')
        monty[None, 'setitem5'] = attr_node
        self.assertEqual(monty.xml_attributes[(u'urn:bogus:a', u'setitem5')], 
                         'value')
Beispiel #11
0
    EMPTY_NODESET,
)

from amara.xpath import context
from test_expressions import DOC

default_context = context(DOC, 1, 1)


EGG1 = tree.element(None, "egg1")
EGG1.xml_append(tree.text("egg1"))
EGG2 = tree.element(None, "egg2")
EGG2.xml_append(tree.text("egg2"))

NUM = tree.element(None, "num")
NUM0 = tree.attribute(None, "num0", "0")
NUM.xml_attributes.setnode(NUM0)
NUM2 = tree.attribute(None, "num2", "2")
NUM.xml_attributes.setnode(NUM0)
NUM4 = tree.attribute(None, "num4", "4")
NUM.xml_attributes.setnode(NUM0)
NUM31 = tree.attribute(None, "num31", "31")
NUM.xml_attributes.setnode(NUM0)


def test_or_expr():
    for (left, right), expected in (
        ((FALSE, FALSE), datatypes.FALSE),
        ((TRUE, FALSE), datatypes.TRUE),
        ((FALSE, TRUE), datatypes.TRUE),
        ((TRUE, TRUE), datatypes.TRUE),
Beispiel #12
0
    def __setitem__(self, key, value):
        '''
        from amara import bindery
        DOC = "<a><b>spam</b></a>"
        doc = bindery.parse(DOC)
        doc.a.b[0] = u"eggs"
        doc.xml_write()
        
        --> "<a><b>eggs</b></a>"

        from amara import bindery
        DOC = "<a><b>spam</b></a>"
        doc = bindery.parse(DOC)
        doc.a[u'b'] = u"eggs"
        doc.xml_write()
        
        --> "<a><b>eggs</b></a>"
        '''
        target = None
        if isinstance(key, int):
            target = list(itertools.islice(element_iterator(self.xml_parent, self.xml_namespace, self.xml_qname), key, key+1))[0]
            parent = self.xml_parent
        else:
            parent = self
            force_type = None
            if isinstance(key, tuple):
                if len(key) == 3:
                    force_type, key = key[0], key[1:]
            elif isinstance(key, basestring):
                key = (None, key)
            else:
                raise TypeError('Inappropriate key (%s)'%(key))
            if force_type in (None, tree.attribute.xml_type) and hasattr(self, 'xml_attributes'):
                target = None
                if isinstance(value, tree.attribute):
                    # Check that the namespaces match
                    ns, local = key
                    if (ns and value.xml_namespace and
                        ns != value.xml_namespace):
                        raise ValueError(
                            "Namespaces don't match: key ns==%r, attrnode ns=%r"
                            % (ns, value.xml_namespace))
                    if local != value.xml_local:
                        raise ValueError(
                            "Local names don't match: key name==%r, attrnode name=%r"
                            % (local, value.xml_local))

                    # If either namespace value is None, use the other
                    # as the default.
                    if value.xml_namespace is None:
                        if ns is None:
                            # If no namespaces specified at all,
                            # use the default one.
                            ns = self.xml_namespaces[None]
                        value = tree.attribute(ns, value.xml_local, value.xml_value)
                    elif ns is None:
                        ns = value.xml_namespace
                    # If they match, perform the assignment.
                    self.xml_attributes.setnode(value)
                else:    
                    self.xml_attributes[key] = value
            elif force_type in (None, tree.element.xml_type):
                target = self.xml_find_named_child(*key)
                if target is None:
                    new_elem = parent.factory_entity.xml_element_factory(*key)
                    new_elem.xml_append(value)
                    parent.xml_append(new_elem)
            else:
                raise KeyError('namespace/local name combination not found (%s)'%(str(key)))
        if target is not None:
            #No target.xml_clear()...
            for child in target.xml_children:
                target.xml_remove(child)
            target.xml_append(value)
        return
Beispiel #13
0
    def __setitem__(self, key, value):
        """
        from amara import bindery
        DOC = "<a><b>spam</b></a>"
        doc = bindery.parse(DOC)
        doc.a.b[0] = u"eggs"
        doc.xml_write()
        
        --> "<a><b>eggs</b></a>"

        from amara import bindery
        DOC = "<a><b>spam</b></a>"
        doc = bindery.parse(DOC)
        doc.a[u'b'] = u"eggs"
        doc.xml_write()
        
        --> "<a><b>eggs</b></a>"
        """
        target = None
        if isinstance(key, int):
            target = list(
                itertools.islice(element_iterator(self.xml_parent, self.xml_namespace, self.xml_qname), key, key + 1)
            )[0]
            parent = self.xml_parent
        else:
            parent = self
            force_type = None
            if isinstance(key, tuple):
                if len(key) == 3:
                    force_type, key = key[0], key[1:]
            elif isinstance(key, basestring):
                key = (None, key)
            else:
                raise TypeError("Inappropriate key (%s)" % (key))
            if force_type in (None, tree.attribute.xml_type) and hasattr(self, "xml_attributes"):
                target = None
                if isinstance(value, tree.attribute):
                    # Check that the namespaces match
                    ns, local = key
                    if ns and value.xml_namespace and ns != value.xml_namespace:
                        raise ValueError(
                            "Namespaces don't match: key ns==%r, attrnode ns=%r" % (ns, value.xml_namespace)
                        )
                    if local != value.xml_local:
                        raise ValueError(
                            "Local names don't match: key name==%r, attrnode name=%r" % (local, value.xml_local)
                        )

                    # If either namespace value is None, use the other
                    # as the default.
                    if value.xml_namespace is None:
                        if ns is None:
                            # If no namespaces specified at all,
                            # use the default one.
                            ns = self.xml_namespaces[None]
                        value = tree.attribute(ns, value.xml_local, value.xml_value)
                    elif ns is None:
                        ns = value.xml_namespace
                    # If they match, perform the assignment.
                    self.xml_attributes.setnode(value)
                else:
                    self.xml_attributes[key] = value
            elif force_type in (None, tree.element.xml_type):
                target = self.xml_find_named_child(*key)
                if target is None:
                    new_elem = parent.factory_entity.xml_element_factory(*key)
                    new_elem.xml_append(value)
                    parent.xml_append(new_elem)
            else:
                raise KeyError("namespace/local name combination not found (%s)" % (str(key)))
        if target is not None:
            # No target.xml_clear()...
            for child in target.xml_children:
                target.xml_remove(child)
            target.xml_append(value)
        return
Beispiel #14
0
    # nodeset literals
    nodeset_literal,
    EMPTY_NODESET,
)

from amara.xpath import context
from test_expressions import DOC
default_context = context(DOC, 1, 1)

EGG1 = tree.element(None, 'egg1')
EGG1.xml_append(tree.text('egg1'))
EGG2 = tree.element(None, 'egg2')
EGG2.xml_append(tree.text('egg2'))

NUM = tree.element(None, 'num')
NUM0 = tree.attribute(None, 'num0', '0')
NUM.xml_attributes.setnode(NUM0)
NUM2 = tree.attribute(None, 'num2', '2')
NUM.xml_attributes.setnode(NUM0)
NUM4 = tree.attribute(None, 'num4', '4')
NUM.xml_attributes.setnode(NUM0)
NUM31 = tree.attribute(None, 'num31', '31')
NUM.xml_attributes.setnode(NUM0)


def test_or_expr():
    for (left, right), expected in (
        ((FALSE, FALSE), datatypes.FALSE),
        ((TRUE, FALSE), datatypes.TRUE),
        ((FALSE, TRUE), datatypes.TRUE),
        ((TRUE, TRUE), datatypes.TRUE),