Exemplo n.º 1
0
def test_If():
    def return_zero():
        return 0

    def return_one():
        return 1

    transformation = Transformation(
        Rule(If(0, operator.eq, 0), lib.put_variable('a')),
        Rule(Not(If(return_zero, operator.eq, return_one)),
             lib.put_variable('b')),
        result_object='context')
    result = transformation(etree.Element('root'))
    assert hasattr(result, 'a')
    assert hasattr(result, 'b')
Exemplo n.º 2
0
def test_If():
    def return_zero():
        return 0

    def return_one():
        return 1

    transformation = Transformation(
        Rule(If(0, operator.eq, 0), lib.put_variable("a")),
        Rule(Not(If(return_zero, operator.eq, return_one)),
             lib.put_variable("b")),
        result_object="context",
    )
    result = transformation(Document("<root/>"))
    assert hasattr(result, "a")
    assert hasattr(result, "b")
Exemplo n.º 3
0
def test_subtransformation():
    subtransformation = Transformation(
        Rule('*', lib.set_localname('pablo'))
    )
    transformation = Transformation(
        lib.f(id, Ref('root')), lib.put_variable('source_id'),
        subtransformation,
        lib.f(id, Ref('root')), lib.put_variable('result_id'),
        lib.debug_symbols('source_id', 'result_id'),
        Rule(Not(If(Ref('source_id'), operator.eq, Ref('result_id'))),
             (lib.debug_message('NO!'), lib.debug_symbols('root'),
                 lib.set_localname('neruda'), AbortRule))
    )
    doc = etree.fromstring('<augustus />')
    assert etree.QName(doc).text == 'augustus'

    result = transformation(doc)
    assert result.tag == 'pablo'
Exemplo n.º 4
0
def test_css_selector(selector, expected):
    document = Document(
        '<section xmlns="foo"><table><head>Table Header</head></table>'
        '<cb type="start">X</cb><row>#</row></section>')
    transformation = Transformation(
        Rule(selector, (lib.get_text, lib.put_variable("result"))),
        result_object="context.result",
    )
    assert transformation(document) == expected
Exemplo n.º 5
0
def test_config_is_immutable():
    trnsfmtn = Transformation(
        lib.put_variable("test", "result"),
        name="strip_surrounding_content",
        result_object="context.test",
    )
    document = Document("<root/>")

    assert trnsfmtn(document) == "result"
    assert trnsfmtn(document) == "result"
Exemplo n.º 6
0
def test_subtransformation():
    subtransformation = Transformation(Rule("*", lib.set_localname("pablo")))
    transformation = Transformation(
        lib.f(id, Ref("root")),
        lib.put_variable("source_id"),
        subtransformation,
        lib.f(id, Ref("root")),
        lib.put_variable("result_id"),
        lib.debug_symbols("source_id", "result_id"),
        Rule(
            Not(If(Ref("source_id"), operator.eq, Ref("result_id"))),
            (
                lib.debug_message("NO!"),
                lib.debug_symbols("root"),
                lib.set_localname("neruda"),
                AbortRule,
            ),
        ),
    )
    doc = Document("<augustus />")
    assert doc.root.local_name == "augustus"

    result = transformation(doc)
    assert result.root.local_name == "pablo"
Exemplo n.º 7
0
def test_set_text():
    node = new_tag_node("pre")
    transformation = Transformation(lib.put_variable("x", "Hello world."),
                                    Rule("/", lib.set_text(Ref("x"))))
    assert str(transformation(node)) == "<pre>Hello world.</pre>"
Exemplo n.º 8
0
import operator as op

from delb import first, new_tag_node, TagNode, tag

from inxs import lib, If, Once, Ref, Rule, Transformation

MODS_NAMESPACE = "http://www.loc.gov/mods/v3"
TEI_NAMESPACE = "http://www.tei-c.org/ns/1.0"

as_result = lib.put_variable("result")
f = lib.f
prev = Ref("previous_result")
result = Ref("result")


def generate_skeleton(context):
    header = context.result = new_tag_node("teiHeader",
                                           namespace=TEI_NAMESPACE)

    context.biblFull_titleStmt = header.new_tag_node("titleStmt")
    context.msDesc = header.new_tag_node("msDesc")
    context.publicationStmt = header.new_tag_node("publicationStmt")
    context.titleStmt = header.new_tag_node("titleStmt")

    header.append_child(
        tag(
            "fileDesc",
            (
                tag(
                    "sourceDesc",
                    (