<filename>notion_readonly/maps.py
from inspect import signature
from .markdown import notion_to_plaintext


class mapper(property):
    def __init__(self, path, api_to_python, *args, **kwargs):
        self.api_to_python = api_to_python
        self.path = (
            ".".join(map(str, path))
            if isinstance(path, (list, tuple))
            else path
        )
        super().__init__(*args, **kwargs)


def field_map(path, api_to_python=lambda x: x):
    """
    Returns a property that maps a Block attribute onto a field in the
    API data structures.
   
    - `path` can either be a top-level field-name, a list that
    specifies the key names to traverse, or a dot-delimited string
    representing the same traversal.

    - `api_to_python` is a function that converts what is received
    from the API into an internal representation to be returned to the
    Python layer.
    """

    if isinstance(path, str):
        path = path.split(".")

    def fget(self):
        kwargs = {}
        if (
            "client" in signature(api_to_python).parameters
            and "id" in signature(api_to_python).parameters
        ):
            kwargs["client"] = self._client
            kwargs["id"] = self.id
        return api_to_python(self.get(path), **kwargs)

    return mapper(
        fget=fget,
        path=path,
        api_to_python=api_to_python,
    )


def property_map(
    name, api_to_python=lambda x: x, markdown=True
):
    """
    Similar to `field_map`, except it works specifically with the data
    under the "properties" field in the API's block table, and just
    takes a single name to specify which subkey to reference.  Also,
    these properties all seem to use a special "embedded list" format
    that breaks the text up into a sequence of chunks and associated
    format metadata. If `markdown` is True, we convert this
    representation into plain text.
    """

    def api2py(x, client=None, id=""):
        x = x or [[""]]
        if markdown:
            x = notion_to_plaintext(x)
        kwargs = {}
        params = signature(api_to_python).parameters
        if "client" in params:
            kwargs["client"] = client
        if "id" in params:
            kwargs["id"] = id
        return api_to_python(x, **kwargs)

    return field_map(["properties", name], api_to_python=api2py)

<filename>notion_readonly/maps.py
from inspect import signature
from .markdown import notion_to_plaintext


class mapper(property):
    def __init__(self, path, api_to_python, *args, **kwargs):
        self.api_to_python = api_to_python
        self.path = (
            ".".join(map(str, path))
            if isinstance(path, (list, tuple))
            else path
        )
        super().__init__(*args, **kwargs)


def field_map(path, api_to_python=lambda x: x):
    """
    Returns a property that maps a Block attribute onto a field in the
    API data structures.
   
    - `path` can either be a top-level field-name, a list that
    specifies the key names to traverse, or a dot-delimited string
    representing the same traversal.

    - `api_to_python` is a function that converts what is received
    from the API into an internal representation to be returned to the
    Python layer.
    """

    if isinstance(path, str):
        path = path.split(".")

    def fget(self):
        kwargs = {}
        if (
            "client" in signature(api_to_python).parameters
            and "id" in signature(api_to_python).parameters
        ):
            kwargs["client"] = self._client
            kwargs["id"] = self.id
        return api_to_python(self.get(path), **kwargs)

    return mapper(
        fget=fget,
        path=path,
        api_to_python=api_to_python,
    )


def property_map(
    name, api_to_python=lambda x: x, markdown=True
):
    """
    Similar to `field_map`, except it works specifically with the data
    under the "properties" field in the API's block table, and just
    takes a single name to specify which subkey to reference.  Also,
    these properties all seem to use a special "embedded list" format
    that breaks the text up into a sequence of chunks and associated
    format metadata. If `markdown` is True, we convert this
    representation into plain text.
    """

    def api2py(x, client=None, id=""):
        x = x or [[""]]
        if markdown:
            x = notion_to_plaintext(x)
        kwargs = {}
        params = signature(api_to_python).parameters
        if "client" in params:
            kwargs["client"] = client
        if "id" in params:
            kwargs["id"] = id
        return api_to_python(x, **kwargs)

    return field_map(["properties", name], api_to_python=api2py)

Returns a property that maps a Block attribute onto a field in the     API data structures.         - `path` can either be a top-level field-name, a list that     specifies the key names to traverse, or a dot-delimited string     representing the same traversal.      - `api_to_python` is a function that converts what is received     from the API into an internal representation to be returned to the     Python layer. Similar to `field_map`, except it works specifically with the data     under the "properties" field in the API's block table, and just     takes a single name to specify which subkey to reference.  Also,     these properties all seem to use a special "embedded list" format     that breaks the text up into a sequence of chunks and associated     format metadata. If `markdown` is True, we convert this     representation into plain text.