Exemplo n.º 1
0
def test_is_root_condition():
    transformation = Transformation(
        Rule(("a", "/"), lib.append("basket")),
        result_object="context.basket",
        context={"basket": []},
    )
    result = transformation(Document("<a><a/></a>"))
    assert len(result) == 1
Exemplo n.º 2
0
def test_common_conditions():
    document = etree.fromstring(
        '<root><a href="foo"/><a id="bar"/><a href="peng"/></root>')
    transformation = Transformation(Rule(
        '*', (lib.get_attribute('href'), lib.append('references'))),
                                    common_rule_conditions={'href': None},
                                    context={'references': []},
                                    result_object='context.references')
    assert transformation(document) == ['foo', 'peng']
Exemplo n.º 3
0
def test_attributes_re_key(constraint, expected):
    document = Document('<root><item1 default-source="x"/>'
                        '<item2 default-value="y"/><item3/></root>')
    transformation = Transformation(
        Rule(constraint, (lib.get_localname, lib.append("result"))),
        context={"result": []},
        result_object="context.result",
    )
    assert transformation(document) == expected
Exemplo n.º 4
0
def test_attributes_re_key():
    document = etree.fromstring('<root><item1 default-source="x"/>'
                                '<item2 default-value="y"/><item3/></root>')
    transformation = Transformation(
        Rule({re.compile('default-'): None},
             (lib.debug_symbols('element'), lib.get_localname,
              lib.append('result'))),
        context={'result': []},
        result_object='context.result')
    assert transformation(document) == ['item1', 'item2']
Exemplo n.º 5
0
def test_common_conditions():
    document = Document(
        '<root><a href="foo"/><a id="bar"/><a href="peng"/></root>')
    transformation = Transformation(
        Rule("*", (lib.get_attribute("href"), lib.append("references"))),
        common_rule_conditions={"href": None},
        context={"references": []},
        result_object="context.references",
    )
    assert transformation(document) == ["foo", "peng"]
Exemplo n.º 6
0
def test_SkipToNextElement():
    def more_complicated_test(node: TagNode):
        # well, supposedly
        if "x" not in node.attributes:
            raise SkipToNextNode
        if int(node.attributes["x"]) % 2:
            raise SkipToNextNode
        return node.local_name

    transformation = Transformation(
        Rule("*", (more_complicated_test, lib.append("evens"))),
        context={"evens": []},
        result_object="context.evens",
    )
    doc = Document('<root><a x="1"/><b x="2"/><c x="3"/><d x="4"/></root>')
    assert transformation(doc) == ["b", "d"]
Exemplo n.º 7
0
def test_is_root_condition():
    transformation = Transformation(Rule(('a', '/'), lib.append('basket')),
                                    result_object='context.basket',
                                    context={'basket': []})
    result = transformation(etree.fromstring('<a><a/></a>'))
    assert len(result) == 1
Exemplo n.º 8
0
mods_name = Transformation(
    Once(
        ("roleTerm", {
            "type": "text"
        }, lib.text_equals("author")),
        (
            lib.make_node(local_name="author", namespace=TEI_NAMESPACE),
            as_result,
            lib.put_variable("role", "author"),
        ),
    ),
    Once(
        ("namePart", {
            "type": "family"
        }),
        (lib.get_text, lib.f(tag, "surname", prev), lib.append("result")),
    ),
    Once(
        ("namePart", {
            "type": "given"
        }),
        (lib.get_text, lib.f(tag, "forename", prev), lib.append("result")),
    ),
    Once(
        If(Ref("role"), op.eq, "author"),
        (
            lib.append(
                "outer_context.titleStmt", Ref("result"), copy_node=True),
            lib.append("outer_context.biblFull_titleStmt",
                       Ref("result"),
                       copy_node=True),