Skip to content

Commit 119836b

Browse files
author
goodboy
authored
Merge pull request #136 from Avira/elevator-pitch
Improve docs: introduce roles and add another example
2 parents deb62e6 + a20a667 commit 119836b

File tree

11 files changed

+271
-48
lines changed

11 files changed

+271
-48
lines changed

docs/api_reference.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Api Reference
24
=============
35

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import eggsample
2+
3+
@eggsample.hookimpl
4+
def eggsample_add_ingredients(ingredients):
5+
"""Here the caller expects us to return a list."""
6+
if "egg" in ingredients:
7+
spam = ["lovely spam", "wonderous spam"]
8+
else:
9+
spam = ["splendiferous spam", "magnificent spam"]
10+
return spam
11+
12+
@eggsample.hookimpl
13+
def eggsample_prep_condiments(condiments):
14+
"""Here the caller passes a mutable object, so we mess with it directly."""
15+
try:
16+
del condiments["steak sauce"]
17+
except KeyError:
18+
pass
19+
condiments['spam sauce'] = 42
20+
return f"Now this is what I call a condiments tray!"

docs/examples/eggsample-spam/setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from setuptools import setup
2+
3+
setup(name="eggsample-spam", install_requires="eggsample",
4+
entry_points={'eggsample': ['spam = eggsample_spam']},
5+
py_modules=['eggsample_spam'])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pluggy
2+
3+
hookimpl = pluggy.HookimplMarker("eggsample")
4+
"""Marker to be imported and used in plugins (and for own implementations)"""
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pluggy
2+
3+
hookspec = pluggy.HookspecMarker("eggsample")
4+
5+
@hookspec
6+
def eggsample_add_ingredients(ingredients: tuple):
7+
"""Have a look at the ingredients and offer your own.
8+
9+
:param ingredients: the ingredients, don't touch them!
10+
:return: a list of ingredients
11+
"""
12+
13+
@hookspec
14+
def eggsample_prep_condiments(condiments: dict):
15+
"""Reorganize the condiments tray to your heart's content.
16+
17+
:param condiments: some sauces and stuff
18+
:return: a witty comment about your activity
19+
"""
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import itertools
2+
import random
3+
4+
import pluggy
5+
6+
from eggsample import hookspecs, lib
7+
8+
condiments_tray = {"pickled walnuts": 13, "steak sauce": 4, "mushy peas": 2}
9+
10+
def main():
11+
pm = get_plugin_manager()
12+
cook = EggsellentCook(pm.hook)
13+
cook.add_ingredients()
14+
cook.prepare_the_food()
15+
cook.serve_the_food()
16+
17+
def get_plugin_manager():
18+
pm = pluggy.PluginManager("eggsample")
19+
pm.add_hookspecs(hookspecs)
20+
pm.load_setuptools_entrypoints("eggsample")
21+
pm.register(lib)
22+
return pm
23+
24+
class EggsellentCook:
25+
FAVORITE_INGREDIENTS = ("egg", "egg", "egg")
26+
27+
def __init__(self, hook):
28+
self.hook = hook
29+
self.ingredients = None
30+
31+
def add_ingredients(self):
32+
results = self.hook.eggsample_add_ingredients(
33+
ingredients=self.FAVORITE_INGREDIENTS)
34+
my_ingredients = list(self.FAVORITE_INGREDIENTS)
35+
# Each hook returns a list - so we chain this list of lists
36+
other_ingredients = list(itertools.chain(*results))
37+
self.ingredients = my_ingredients + other_ingredients
38+
39+
def prepare_the_food(self):
40+
random.shuffle(self.ingredients)
41+
42+
def serve_the_food(self):
43+
condiment_comments = self.hook.eggsample_prep_condiments(
44+
condiments=condiments_tray)
45+
print(f"Your food. Enjoy some {', '.join(self.ingredients)}")
46+
print(f"Some condiments? We have {', '.join(condiments_tray.keys())}")
47+
if any(condiment_comments):
48+
print("\n".join(condiment_comments))
49+
50+
if __name__ == '__main__':
51+
main()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import eggsample
2+
3+
@eggsample.hookimpl
4+
def eggsample_add_ingredients():
5+
spices = ["salt", "pepper"]
6+
you_can_never_have_enough_eggs = ["egg", "egg"]
7+
ingredients = spices + you_can_never_have_enough_eggs
8+
return ingredients
9+
10+
@eggsample.hookimpl
11+
def eggsample_prep_condiments(condiments):
12+
condiments["mint sauce"] = 1

docs/examples/eggsample/setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(name="eggsample", install_requires="pluggy>=0.3,<1.0",
4+
entry_points={'console_scripts': ['eggsample=eggsample.host:main']},
5+
packages=find_packages())

docs/examples/firstexample.py renamed to docs/examples/toy-example.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@
55

66

77
class MySpec(object):
8-
"""A hook specification namespace.
9-
"""
8+
"""A hook specification namespace."""
109
@hookspec
1110
def myhook(self, arg1, arg2):
12-
"""My special little hook that you can customize.
13-
"""
11+
"""My special little hook that you can customize."""
1412

1513

1614
class Plugin_1(object):
17-
"""A hook implementation namespace.
18-
"""
15+
"""A hook implementation namespace."""
1916
@hookimpl
2017
def myhook(self, arg1, arg2):
2118
print("inside Plugin_1.myhook()")
2219
return arg1 + arg2
2320

2421

2522
class Plugin_2(object):
26-
"""A 2nd hook implementation namespace.
27-
"""
23+
"""A 2nd hook implementation namespace."""
2824
@hookimpl
2925
def myhook(self, arg1, arg2):
3026
print("inside Plugin_2.myhook()")
@@ -34,11 +30,9 @@ def myhook(self, arg1, arg2):
3430
# create a manager and add the spec
3531
pm = pluggy.PluginManager("myproject")
3632
pm.add_hookspecs(MySpec)
37-
3833
# register plugins
3934
pm.register(Plugin_1())
4035
pm.register(Plugin_2())
41-
4236
# call our `myhook` hook
4337
results = pm.hook.myhook(arg1=1, arg2=2)
4438
print(results)

0 commit comments

Comments
 (0)