def test_add_html_classes(): doc = Document("<html><body/></html>") transformation = Transformation( Rule("body", lib.add_html_classes("transformed"))) result = transformation(doc).root assert result[0].attributes["class"] == "transformed" doc = Document('<html><body class="loaded" /></html>') result = transformation(doc).root assert all(x in result[0].attributes["class"] for x in ("transformed", "loaded")) transformation = Transformation( Rule("body", lib.add_html_classes("transformed", "and_something_else"))) result = transformation(doc).root assert all(x in result[0].attributes["class"] for x in ("and_something_else", "loaded", "transformed")) transformation = Transformation( Rule("body", lib.add_html_classes(Ref("html_classes"))), context={"html_classes": ["transformed", "and_something_else"]}, ) result = transformation(doc).root assert all(x in result[0].attributes["class"] for x in ("and_something_else", "loaded", "transformed"))
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"
def put_variable(name, value=Ref('previous_result')): """ Puts the ``previous_result`` as ``name`` to the :term:`context` namespace. """ def callable_handler(transformation): setattr(transformation.context, name, value()) return transformation.states.previous_result def callable_handler_dot_lookup(transformation): setattr(dot_lookup(transformation.context, name), value()) return transformation.states.previous_result def ref_handler(transformation): setattr(transformation.context, name, value(transformation)) return transformation.states.previous_result def ref_handler_dot_lookup(transformation): setattr(dot_lookup(transformation.context, name), value(transformation)) return transformation.states.previous_result def simple_handler(transformation): setattr(transformation.context, name, value) return transformation.states.previous_result def simple_handler_dot_lookup(transformation): setattr(dot_lookup(transformation.context, name), value) return transformation.states.previous_result if is_Ref(value): if '.' in name: return ref_handler_dot_lookup return ref_handler elif callable(value): if '.' in name: return callable_handler_dot_lookup return callable_handler elif '.' in name: return simple_handler_dot_lookup else: return simple_handler
def test_dotted_Ref(): transformation = SimpleNamespace( _available_symbols={"root": SimpleNamespace(item="check")} ) assert Ref("root.item")(transformation) == "check"
def test_concatenate(): transformation = SimpleNamespace(_available_symbols={"foo": "bar"}) assert lib.concatenate("foo", Ref("foo"))(transformation) == "foobar"
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>"
def test_concatenate(): transformation = SimpleNamespace(_available_symbols={'foo': 'bar'}) assert lib.concatenate('foo', Ref('foo'))(transformation) == 'foobar'
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", (