Skip to content

.tool.transformer

fediverse_pasture_inputs.tool.transformer

Helper class to create object and activity from examples

ExampleTransformer

Class to create objects and activities from examples

Source code in fediverse_pasture_inputs/tool/transformer.py
class ExampleTransformer:
    """Class to create objects and activities from
    examples"""

    def __init__(self):
        self.sender = activity_sender()

    async def init_sender_for_example(self, example):
        self.sender.init_create_note(lambda x: {**x, **example})
        await self.sender.send("http://remote.example/")

    async def create_object(self, example):
        """Creates the sample object

        ```pycon
        >>> import asyncio
        >>> et = ExampleTransformer()
        >>> asyncio.run(et.create_object({"content": "moo"}))
        {'type': 'Note',
            'attributedTo': 'http://actor.example',
            'to': ['as:Public'],
            'id': 'http://actor.example/...',
            'published': '...',
            'content': 'moo',
            '@context': ['https://www.w3.org/ns/activitystreams',
                {'Hashtag': 'as:Hashtag', 'sensitive': 'as:sensitive'}]}

        ```
        """
        await self.init_sender_for_example(example)
        obj = self.sender.note
        obj["@context"] = [
            "https://www.w3.org/ns/activitystreams",
            {"Hashtag": "as:Hashtag", "sensitive": "as:sensitive"},
        ]
        return obj

    async def create_activity(self, example):
        """Creates the sample activity

        ```pycon
        >>> import asyncio
        >>> et = ExampleTransformer()
        >>> asyncio.run(et.create_activity({"content": "moo"}))
        {'@context': ['https://www.w3.org/ns/activitystreams',
            {'Hashtag': 'as:Hashtag', 'sensitive': 'as:sensitive'}],
            'type': 'Create',
            'actor': 'http://actor.example',
            'to': [...],
            'id': 'http://actor.example/...',
            'published': '...',
            'object': {'type': 'Note',
                'attributedTo': 'http://actor.example',
                'to': ['as:Public', 'http://remote.example/'],
                'id': 'http://actor.example/...',
                'published': '...',
                'content': 'moo'}}

        ```
        """
        await self.init_sender_for_example(example)
        return self.sender.activity

create_activity(example) async

Creates the sample activity

>>> import asyncio
>>> et = ExampleTransformer()
>>> asyncio.run(et.create_activity({"content": "moo"}))
{'@context': ['https://www.w3.org/ns/activitystreams',
    {'Hashtag': 'as:Hashtag', 'sensitive': 'as:sensitive'}],
    'type': 'Create',
    'actor': 'http://actor.example',
    'to': [...],
    'id': 'http://actor.example/...',
    'published': '...',
    'object': {'type': 'Note',
        'attributedTo': 'http://actor.example',
        'to': ['as:Public', 'http://remote.example/'],
        'id': 'http://actor.example/...',
        'published': '...',
        'content': 'moo'}}
Source code in fediverse_pasture_inputs/tool/transformer.py
async def create_activity(self, example):
    """Creates the sample activity

    ```pycon
    >>> import asyncio
    >>> et = ExampleTransformer()
    >>> asyncio.run(et.create_activity({"content": "moo"}))
    {'@context': ['https://www.w3.org/ns/activitystreams',
        {'Hashtag': 'as:Hashtag', 'sensitive': 'as:sensitive'}],
        'type': 'Create',
        'actor': 'http://actor.example',
        'to': [...],
        'id': 'http://actor.example/...',
        'published': '...',
        'object': {'type': 'Note',
            'attributedTo': 'http://actor.example',
            'to': ['as:Public', 'http://remote.example/'],
            'id': 'http://actor.example/...',
            'published': '...',
            'content': 'moo'}}

    ```
    """
    await self.init_sender_for_example(example)
    return self.sender.activity

create_object(example) async

Creates the sample object

>>> import asyncio
>>> et = ExampleTransformer()
>>> asyncio.run(et.create_object({"content": "moo"}))
{'type': 'Note',
    'attributedTo': 'http://actor.example',
    'to': ['as:Public'],
    'id': 'http://actor.example/...',
    'published': '...',
    'content': 'moo',
    '@context': ['https://www.w3.org/ns/activitystreams',
        {'Hashtag': 'as:Hashtag', 'sensitive': 'as:sensitive'}]}
Source code in fediverse_pasture_inputs/tool/transformer.py
async def create_object(self, example):
    """Creates the sample object

    ```pycon
    >>> import asyncio
    >>> et = ExampleTransformer()
    >>> asyncio.run(et.create_object({"content": "moo"}))
    {'type': 'Note',
        'attributedTo': 'http://actor.example',
        'to': ['as:Public'],
        'id': 'http://actor.example/...',
        'published': '...',
        'content': 'moo',
        '@context': ['https://www.w3.org/ns/activitystreams',
            {'Hashtag': 'as:Hashtag', 'sensitive': 'as:sensitive'}]}

    ```
    """
    await self.init_sender_for_example(example)
    obj = self.sender.note
    obj["@context"] = [
        "https://www.w3.org/ns/activitystreams",
        {"Hashtag": "as:Hashtag", "sensitive": "as:sensitive"},
    ]
    return obj