Skip to content

fediverse_pasture_inputs.tool

python -mfediverse_pasture_inputs.tool

Tool for helping with creating the documentation for the fediverse-pasture-inputs

Usage:

python -mfediverse_pasture_inputs.tool [OPTIONS] COMMAND [ARGS]...

Options:

Name Type Description Default
--help boolean Show this message and exit. False

Subcommands

  • docs: Creates a documentation page for each input
  • zip-file: Creates a zip file containing the the generated ActivityPub objects

python -mfediverse_pasture_inputs.tool docs

Creates a documentation page for each input

Usage:

python -mfediverse_pasture_inputs.tool docs [OPTIONS]

Options:

Name Type Description Default
--path text Path of the directory the documentation pages are to be deposited docs/inputs
--help boolean Show this message and exit. False

python -mfediverse_pasture_inputs.tool zip-file

Creates a zip file containing the the generated ActivityPub objects and activities

Usage:

python -mfediverse_pasture_inputs.tool zip-file [OPTIONS]

Options:

Name Type Description Default
--path text Path of the directory the zip file is created at docs/assets
--help boolean Show this message and exit. False

fediverse_pasture_inputs.tool

current_asset_archive(tag=__version__)

Downloads the zipfile for tag and then provides it as a generator.

>>> with current_asset_archive("0.1.8") as assets:
...     assets.namelist()
    ['assets/', 'assets/note2.jsonap', ...]

Returns:

Type Description
Generator[ZipFile, None, None]

archive of the inputs

Source code in fediverse_pasture_inputs/tool/__init__.py
@contextmanager
def current_asset_archive(
    tag: str = __version__,
) -> Generator[zipfile.ZipFile, None, None]:
    """
    Downloads the zipfile for `tag` and then
    provides it as a generator.

    ```pycon
    >>> with current_asset_archive("0.1.8") as assets:
    ...     assets.namelist()
        ['assets/', 'assets/note2.jsonap', ...]

    ```

    :returns: archive of the inputs
    """
    with tempfile.TemporaryDirectory() as tmpdirname:
        filename = f"{tmpdirname}/assets.zip"
        urlretrieve(make_url(tag), filename)
        with zipfile.ZipFile(filename) as fp:
            yield fp

extract(tag=__version__)

Extracts the asset zipfile

Source code in fediverse_pasture_inputs/tool/__init__.py
def extract(tag: str = __version__):
    """Extracts the asset zipfile"""
    with current_asset_archive(tag) as archive:
        archive.extractall()

make_url(tag)

Returns the url of the asset zip file

>>> make_url("0.1.8")
'https://codeberg.org/api/packages/funfedidev/generic/fediverse_pasture_assets/0.1.8/fediverse_pasture_assets.zip'
Source code in fediverse_pasture_inputs/tool/__init__.py
def make_url(tag):
    """Returns the url of the asset zip file

    ```pycon
    >>> make_url("0.1.8")
    'https://codeberg.org/api/packages/funfedidev/generic/fediverse_pasture_assets/0.1.8/fediverse_pasture_assets.zip'

    ```
    """
    return f"https://codeberg.org/api/packages/funfedidev/generic/fediverse_pasture_assets/{tag}/fediverse_pasture_assets.zip"

__main__

docs(path)

Creates a documentation page for each input

Source code in fediverse_pasture_inputs/tool/__main__.py
@main.command()
@click.option(
    "--path",
    default="docs/inputs",
    help="Path of the directory the documentation pages are to be deposited",
)
def docs(path):
    """Creates a documentation page for each input"""
    Path(path).mkdir(parents=True, exist_ok=True)
    asyncio.run(run_for_path(path))

main()

Tool for helping with creating the documentation for the fediverse-pasture-inputs

Source code in fediverse_pasture_inputs/tool/__main__.py
@click.group()
def main():
    """Tool for helping with creating the documentation for the
    fediverse-pasture-inputs"""
    ...

zip_file(path)

Creates a zip file containing the the generated ActivityPub objects and activities

Source code in fediverse_pasture_inputs/tool/__main__.py
@main.command()
@click.option(
    "--path",
    default="docs/assets",
    help="Path of the directory the zip file is created at",
)
def zip_file(path):
    """Creates a zip file containing the the generated ActivityPub objects
    and activities"""
    Path(path).mkdir(parents=True, exist_ok=True)

    with zipfile.ZipFile(f"{path}/samples.zip", "w") as zipcontainer:
        for inputs in available.values():
            asyncio.run(add_samples_to_zip(zipcontainer, inputs))

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