forked from pytest-dev/pluggy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pytest-dev#136 from Avira/elevator-pitch
Improve docs: introduce roles and add another example
- Loading branch information
Showing
11 changed files
with
271 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
:orphan: | ||
|
||
Api Reference | ||
============= | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import eggsample | ||
|
||
@eggsample.hookimpl | ||
def eggsample_add_ingredients(ingredients): | ||
"""Here the caller expects us to return a list.""" | ||
if "egg" in ingredients: | ||
spam = ["lovely spam", "wonderous spam"] | ||
else: | ||
spam = ["splendiferous spam", "magnificent spam"] | ||
return spam | ||
|
||
@eggsample.hookimpl | ||
def eggsample_prep_condiments(condiments): | ||
"""Here the caller passes a mutable object, so we mess with it directly.""" | ||
try: | ||
del condiments["steak sauce"] | ||
except KeyError: | ||
pass | ||
condiments['spam sauce'] = 42 | ||
return f"Now this is what I call a condiments tray!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from setuptools import setup | ||
|
||
setup(name="eggsample-spam", install_requires="eggsample", | ||
entry_points={'eggsample': ['spam = eggsample_spam']}, | ||
py_modules=['eggsample_spam']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import pluggy | ||
|
||
hookimpl = pluggy.HookimplMarker("eggsample") | ||
"""Marker to be imported and used in plugins (and for own implementations)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pluggy | ||
|
||
hookspec = pluggy.HookspecMarker("eggsample") | ||
|
||
@hookspec | ||
def eggsample_add_ingredients(ingredients: tuple): | ||
"""Have a look at the ingredients and offer your own. | ||
:param ingredients: the ingredients, don't touch them! | ||
:return: a list of ingredients | ||
""" | ||
|
||
@hookspec | ||
def eggsample_prep_condiments(condiments: dict): | ||
"""Reorganize the condiments tray to your heart's content. | ||
:param condiments: some sauces and stuff | ||
:return: a witty comment about your activity | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import itertools | ||
import random | ||
|
||
import pluggy | ||
|
||
from eggsample import hookspecs, lib | ||
|
||
condiments_tray = {"pickled walnuts": 13, "steak sauce": 4, "mushy peas": 2} | ||
|
||
def main(): | ||
pm = get_plugin_manager() | ||
cook = EggsellentCook(pm.hook) | ||
cook.add_ingredients() | ||
cook.prepare_the_food() | ||
cook.serve_the_food() | ||
|
||
def get_plugin_manager(): | ||
pm = pluggy.PluginManager("eggsample") | ||
pm.add_hookspecs(hookspecs) | ||
pm.load_setuptools_entrypoints("eggsample") | ||
pm.register(lib) | ||
return pm | ||
|
||
class EggsellentCook: | ||
FAVORITE_INGREDIENTS = ("egg", "egg", "egg") | ||
|
||
def __init__(self, hook): | ||
self.hook = hook | ||
self.ingredients = None | ||
|
||
def add_ingredients(self): | ||
results = self.hook.eggsample_add_ingredients( | ||
ingredients=self.FAVORITE_INGREDIENTS) | ||
my_ingredients = list(self.FAVORITE_INGREDIENTS) | ||
# Each hook returns a list - so we chain this list of lists | ||
other_ingredients = list(itertools.chain(*results)) | ||
self.ingredients = my_ingredients + other_ingredients | ||
|
||
def prepare_the_food(self): | ||
random.shuffle(self.ingredients) | ||
|
||
def serve_the_food(self): | ||
condiment_comments = self.hook.eggsample_prep_condiments( | ||
condiments=condiments_tray) | ||
print(f"Your food. Enjoy some {', '.join(self.ingredients)}") | ||
print(f"Some condiments? We have {', '.join(condiments_tray.keys())}") | ||
if any(condiment_comments): | ||
print("\n".join(condiment_comments)) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import eggsample | ||
|
||
@eggsample.hookimpl | ||
def eggsample_add_ingredients(): | ||
spices = ["salt", "pepper"] | ||
you_can_never_have_enough_eggs = ["egg", "egg"] | ||
ingredients = spices + you_can_never_have_enough_eggs | ||
return ingredients | ||
|
||
@eggsample.hookimpl | ||
def eggsample_prep_condiments(condiments): | ||
condiments["mint sauce"] = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from setuptools import setup, find_packages | ||
|
||
setup(name="eggsample", install_requires="pluggy>=0.3,<1.0", | ||
entry_points={'console_scripts': ['eggsample=eggsample.host:main']}, | ||
packages=find_packages()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.