Python wrapper around MuleSoft DataWeave data transformation engine.
Project description
DataWeave Python Wrapper ![Build Status](https://pypi-camo.freetls.fastly.net/6daf6b2f687309046f4334388deed146bbad28a1/68747470733a2f2f7472617669732d63692e636f6d2f7465747261736369656e63652f74732d6461746177656176652d707974686f6e2e7376673f746f6b656e3d757a77354b465a70733351585439627152444859266272616e63683d6d61696e)
Python wrapper around MuleSoft's DataWeave data transformation engine.
Installation
Add this package to your project using pip
or pipenv
:
# using pip
pip install ts-dataweave
# using pipenv
pipenv install ts-dataweave
The post-install script will download and extract the correct DataWeave command-line binary from MuleSoft's DataWeave CLI repository.
Usage
To use DataWeave, import ts_dataweave
:
import ts_dataweave
ts_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.
The payload parameter should be a dictionary of ts_dataweave.DataWeavePayload
instances with string keys. The payloads will be added to DataWeave with the corresponding key as the name of the payload.
The script parameter should be a string, bytes, or file-like object containing the DataWeave script.
The timeout parameter 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.
The ts_dataweave.run()
function returns a bytes object containing the output of the DataWeave script execution.
A bytes object can easily be converted to a UTF-8 string by using
.decode("utf-8")
.
ts_dataweave.Payload
The dataweave.Payload
object contains information about a DataWeave payload. Use the constructor:
ts_dataweave.Payload(payloadType, data)
The payloadType parameter should be either "json"
, "csv"
, or "xml"
. Alternatively, the enumeration dataweave.PayloadType
can also be used. Additional input types are not recognized at this time.
The data parameter should be a string, bytes, dict, list, or file-like object containing the content of the payload. Note that if data is a dict or list, payloadType should be "json"
.
Full Example
import ts_dataweave
print(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"))
Tests
The repository includes a test suite.
python -m pytest
Compatibility
This package has been specifically developed and tested with Python 3.7 on Darwin (macOS) x86_64 and Linux x86_64 platforms.
Changelog
1.0.2
- Fix wrapping of
ts_dataweave.Error
with anotherts_dataweave.Error
- Sanitize payload IDs before executing DataWeave
1.0.1
- Use temporary file to capture DataWeave output instead of the standard output stream.
1.0.0
- Initial implementation.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.