{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# First look at pluggy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ " import pluggy, pytest" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " import pluggy, pytest" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ " specification, implementation = pluggy.HookspecMarker('example') ,pluggy.HookimplMarker('example')" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " specification, implementation = pluggy.HookspecMarker('example') ,pluggy.HookimplMarker('example')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ " class Spec:\n", " \n", "Define specifications as function with the name of the hook and reusable function signature.\n", " \n", " @specification\n", " def function(object):\n", " \n", "A specification needs to exist before an implementation." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " class Spec:\n", " \n", "Define specifications as function with the name of the hook and reusable function signature.\n", " \n", " @specification\n", " def function(object):\n", " \n", "A specification needs to exist before an implementation." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ " @implementation\n", " def function(object):\n", " \n", "An implementation of the function specification.\n", " \n", " return type(object)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " @implementation\n", " def function(object):\n", " \n", "An implementation of the function specification.\n", " \n", " return type(object)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ " class Another:\n", " @implementation(trylast=True)\n", " def function(object):\n", " \n", "Another implementation of the function spec.\n", " \n", " return str(object)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " class Another:\n", " @implementation(trylast=True)\n", " def function(object):\n", " \n", "Another implementation of the function spec.\n", " \n", " return str(object)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "The manager connects specifications and implementations with a manager.\n", "\n", " manager = pluggy.PluginManager('example')\n", " \n", "Specifications need to be registered, but no implementations exist to the manager.\n", "\n", " manager.add_hookspecs(Spec)\n", " \n", " >>> with pytest.raises(TypeError): manager.hook.function(int)\n", " \n", "The hook only accepts keyword arguments with names that validate. \n", "\n", " >>> manager.hook.function(object=int)\n", " []" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "The manager connects specifications and implementations with a manager.\n", "\n", " manager = pluggy.PluginManager('example')\n", " \n", "Specifications need to be registered, but no implementations exist to the manager.\n", "\n", " manager.add_hookspecs(Spec)\n", " \n", " >>> with pytest.raises(TypeError): manager.hook.function(int)\n", " \n", "The hook only accepts keyword arguments with names that validate. \n", "\n", " >>> manager.hook.function(object=int)\n", " []" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Include an implementation. \n", " \n", " manager.register(__import__(__name__))\n", " \n", " >>> manager.hook.function(object=int)\n", " []\n", " \n", "We cannot register the same object twice.\n", " \n", " >>> with pytest.raises(ValueError): manager.register(__import__(__name__))" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Include an implementation. \n", " \n", " manager.register(__import__(__name__))\n", " \n", " >>> manager.hook.function(object=int)\n", " []\n", " \n", "We cannot register the same object twice.\n", " \n", " >>> with pytest.raises(ValueError): manager.register(__import__(__name__))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Adding another hook returns more results when the hook is triggered. \n", " \n", " manager.register(Another)\n", "\n", " >>> manager.hook.function(object=int)\n", " [, \"\"]" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Adding another hook returns more results when the hook is triggered. \n", " \n", " manager.register(Another)\n", "\n", " >>> manager.hook.function(object=int)\n", " [, \"\"]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Implementations can be unregistered. \n", " \n", " manager.unregister(__import__(__name__))\n", " >>> [x.function for x in manager.hook.function.get_hookimpls()]\n", " []\n", "\n", " manager.register(__import__(__name__))\n", "\n", " >>> [x.function for x in manager.hook.function.get_hookimpls()]\n", " [, ]" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Implementations can be unregistered. \n", " \n", " manager.unregister(__import__(__name__))\n", " >>> [x.function for x in manager.hook.function.get_hookimpls()]\n", " []\n", "\n", " manager.register(__import__(__name__))\n", "\n", " >>> [x.function for x in manager.hook.function.get_hookimpls()]\n", " [, ]\n" ] } ], "metadata": { "kernelspec": { "display_name": "pidgy 3", "language": "python", "name": "pidgy" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }