resource-translate

Utilities to translate resources between platforms.


Keywords
API, integration, resources, translation
License
Apache-2.0
Install
pip install resource-translate==1.2.0

Documentation

Resource Translate

Utilities to translate resources between platforms.

PyPI GitHub Actions: CI Workflow License: Apache 2.0 Code Style: Black


Installation

pip install resource_translate

Examples

Translating an Object

Given the following MockPerson object:

class _MockAddress:
    town = "Bobtonville"
    country_code = "US"


class MockPerson:
    first_name = "Bob"
    calling_code = 1
    phone_number = "(916) 276-6782"
    employer = None
    address = _MockAddress

And the following PersonFromObj translator:

from resource_translate import Translator, attr


class PersonFromObj(Translator):
    constants = {"tags": "mock-person"}
    mapping = {
        "name": "first_name",
        "billing_address": {"city": "address.town"},
        "employer": "employer",
        "missing": "absent",
    }

    @attr
    def phone(self):
        return f"+{self.resource.calling_code} {self.resource.phone_number}"

    @attr("billing_address")
    def country(self):
        if self.resource.address.country_code == "US":
            return "USA"

        return self.resource.address.country_code

    @attr("nested", "attr")
    def deep(self):
        if self.repr["billing_address"]["country"] == "USA":
            return "Deep Attribute"

Calling:

PersonFromObj(MockPerson).repr

Returns:

{
    "name": "Bob",
    "phone": "+1 (916) 276-6782",
    "nested": {"attr": {"deep": "Deep Attribute"}},
    "tags": "mock-person",
    "billing_address": {"city": "Bobtonville", "country": "USA"},
}

Translating a Mapping

Given the following MOCK_PERSON mapping:

_MOCK_ADDRESS = {"town": "Bobtonville", "country_code": "US"}

MOCK_PERSON = {
    "first_name": "Bob",
    "calling_code": 1,
    "phone_number": "(916) 276-6782",
    "employer": None,
    "address": _MOCK_ADDRESS,
}

And the following PersonFromMap translator:

from resource_translate import Translator, attr


class PersonFromMap(Translator):
    constants = {"tags": "mock-person"}
    mapping = {
        "name": "first_name",
        "billing_address": {"city": ("address", "town")},
        "employer": "employer",
        "missing": "absent",
    }

    @attr
    def phone(self):
        return f"+{self.resource['calling_code']} {self.resource['phone_number']}"

    @attr("billing_address")
    def country(self):
        if self.resource["address"]["country_code"] == "US":
            return "USA"

        return self.resource["address"]["country_code"]

    @attr("nested", "attr")
    def deep(self):
        if self.repr["billing_address"]["country"] == "USA":
            return "Deep Attribute"

Calling:

PersonFromMap(MOCK_PERSON, from_map=True).repr

Returns:

{
    "name": "Bob",
    "phone": "+1 (916) 276-6782",
    "nested": {"attr": {"deep": "Deep Attribute"}},
    "tags": "mock-person",
    "billing_address": {"city": "Bobtonville", "country": "USA"},
}

Explicit Attributes

Keyword arguments are set directly on the translated resource - given the prior PersonFromObj translator, calling:

PersonFromObj(MockPerson, tags="kwargs-override", billing_address={"postal_code": "78498"}).repr

Returns:

{
    "name": "Bob",
    "phone": "+1 (916) 276-6782",
    "nested": {"attr": {"deep": "Deep Attribute"}},
    "tags": "kwargs-override",
    "billing_address": {"city": "Bobtonville", "country": "USA", "postal_code": "78498"},
}

For additional examples, see tests/.


Reference

$ python
>>> from resource_translate import Translator, attr
>>> help(Translator)
...
>>> help(attr)
...

Testing

pip install pytest[ pytest-cov]

Having cloned the repository, from the root directory:

pytest[ --cov resource_translate --cov-report term-missing]