Package: ts-dataweave
DataWeave Python WrapperPython wrapper around MuleSoft's DataWeave data transformation engine.InstallationAdd this package to your project usingpiporpipenv:# using pippipinstallts-dataweave# using pipenvpipenvinstallts-dataweaveThe post-install script will download and extract the correct DataWeave command-line binary fromMuleSoft's DataWeave CLI repository.UsageTo use DataWeave, importts_dataweave:importts_dataweavets_dataweave.run(payload,script, [timeout])Runs a DataWeave script with the specified payloads. The input payloads and script are written to temporary files and then removed when the execution is complete.Thepayloadparameter should be a dictionary ofts_dataweave.Payloadinstances withstringkeys. The payloads will be added to DataWeave with the corresponding key as the name of the payload.Thescriptparameter should be astring,bytes, orfile-likeobject containing the DataWeave script.Thetimeoutparameter is optional and should be the number of seconds that the script is allowed to run before an error is raised. The default timeout is 30 seconds.Thets_dataweave.run()function returns abytesobject containing the output of the DataWeave script execution.Abytesobject can easily be converted to a UTF-8 string by using.decode("utf-8").ts_dataweave.PayloadThedataweave.Payloadobject contains information about a DataWeave payload. Use the constructor:ts_dataweave.Payload([payloadType],data)ThepayloadTypeparameter should be either"json","csv", or"xml". Alternatively, the members from the enumerationdataweave.PayloadTypecan also be used. Additional input types are not recognized at this time. If thepayloadTypeparameter is omitted, the type will be guessed based on the first non-whitespace byte ofdata:b"{"➡️"json"b"["➡️"json"b"<"➡️"xml"Else ➡️"csv"If the payload is large or there is any chance of ambiguity, please specifypayloadType.Thedataparameter should be astring,bytes,dict,list, orfile-likeobject containing the content of the payload. ThepayloadTypeparameter can be safely omitted ifdatais adictorlist.ExamplesExample with explicit payload types:importts_dataweaveprint(ts_dataweave.run({"doc1":ts_dataweave.Payload("xml",# can also use ts_dataweave.XML"""<root><firstname>Mike</firstname><lastname>Foo</lastname></root>"""),"doc2":ts_dataweave.Payload("json",# can also use ts_dataweave.JSON"""{"birthday": "yesterday","favoriteFood": "salami"}"""),},"""contact: {firstname: doc1.root.firstname,lastname: doc1.root.lastname,food: doc2.favoriteFood,bday: doc2.birthday}""").decode("utf-8"))Payload data types can also be auto-detected:importts_dataweaveprint(ts_dataweave.run({"doc1":ts_dataweave.Payload("""<root><firstname>Mike</firstname><lastname>Foo</lastname></root>"""),"doc2":ts_dataweave.Payload("""{"birthday": "yesterday","favoriteFood": "salami"}"""),},"""contact: {firstname: doc1.root.firstname,lastname: doc1.root.lastname,food: doc2.favoriteFood,bday: doc2.birthday}""").decode("utf-8"))Pythondicts andlists can also be used for payload data with payload type safely omitted:importts_dataweaveprint(ts_dataweave.run({"doc1":ts_dataweave.Payload(["Mike","Foo"]),"doc2":ts_dataweave.Payload({"birthday":"yesterday","favoriteFood":"salami"}),},"""contact: {firstname: doc1[0],lastname: doc1[1],food: doc2.favoriteFood,bday: doc2.birthday}""").decode("utf-8"))TestsThe repository includes a test suite.python-mpytestCompatibilityThis package has been specifically developed and tested with Python 3.7 on Darwin (macOS) x86_64 and Linux x86_64 platforms.Changelog1.0.4Update DataWeave CLI to v1.0.16 for macOS and Linux.1.0.3Optional auto-detection ofpayloadTypeints_dataweave.Payloadconstructor1.0.2Fix wrapping ofts_dataweave.Errorwith anotherts_dataweave.ErrorSanitize payload IDs before executing DataWeave1.0.1Use temporary file to capture DataWeave output instead of the standard output stream.1.0.0Initial implementation.