Extensions
==========

Tatoo follows the microservice architecture by dividing different components
into third party packages. It allows tatoo itself to be small, simple and
testable.

It is very easy to write custom extensions. Let's create one in
``maths.py`` file::

    from tatoo.extension import Extension

    ext = Extension(__name__, version='0.1.0')

    @ext.task
    def add(x, y):
        return x + y

    @ext.task
    def sub(x, y):
        return x - y

    @ext.task
    def mul(x, y):
        return x * y

    @ext.task
    def div(x, y):
        return x / y

.. sidebar:: Task names

    In the :ref:`task names <task-names>` section you learned that each
    task has a unique name, and by default it's generated from the callable's
    name. It's not true for tasks defined in extensions. Default behavior here
    is to prepend the extension name (and dot), so that ``add`` task
    defined in ``math`` extension gets ``math.add`` name by default.

You will also need a simple ``setup.py``::

    from setuptools import setup

    setup(
        name='math-tasks',
        version='0.1.0',
        py_modules=['maths'],
        entry_points="""
        [tatoo.extensions]
        maths = maths:ext
        """
    )

Now you're able to *install* this extension using pip::

    $ pip install .

Let's make sure that our ``math`` extension is loaded and is usable::

    >>> from tatoo import Environment
    >>> env = Environment('testenv')
    >>> print(list(env.extensions))
    ['math']
    >>> res = env.tasks['math.add'].apply(args=(1, 2))
    >>> res.result
    3
