Skip to content

Commit 6283b3b

Browse files
committed
Rework the docs intro and add another example
Rework the beginning of the docs answering three fundamental questions. Instead of trying to cram everything into a highly compressed role definition, do the following: Use 4 roles to introduce the basic concept from a bird's eye perspective - 3 of them are programs and one of them is a human (because why not?). Instead of having only one example have two: the first example becomes the "toy example" (because that's what it is). Add another example that is showing two minimal projects (host and plugin), demonstrating the concepts that would overwhelm the first toy example but are important to understand conventions and get a complete picture. Some additional changes in the docs: * Flesh out the motivation for a system like pluggy a bit more. * Turn "The pytest plugin system" from a heading into a tag-line. * fix a pre existing title level problem * add orphan tag to api reference to be able to build with warnings as errors * turn doc build warnings into errors * Made title casing more consistent
1 parent 2b39339 commit 6283b3b

File tree

10 files changed

+246
-113
lines changed

10 files changed

+246
-113
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import eggsample
2+
3+
@eggsample.hookimpl
4+
def eggsample_add_ingredients(ingredients):
5+
if len([e for e in ingredients if e == "egg"]) > 2:
6+
ingredients.remove("egg")
7+
spam = ["lovely spam", "wonderous spam", "splendiferous spam"]
8+
print(f"add {spam}")
9+
ingredients.extend(spam)
10+
11+
@eggsample.hookimpl
12+
def eggsample_prep_condiments(condiments):
13+
try:
14+
del condiments["steak sauce"]
15+
except KeyError:
16+
pass

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pluggy
2+
3+
hookimpl = pluggy.HookimplMarker("eggsample")
4+
"""Marker to be imported and used in plugins"""
5+
6+
pm = pluggy.PluginManager("eggsample")
7+
"""The manager ... you know? to manage the plugins!"""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pluggy
2+
3+
hookspec = pluggy.HookspecMarker("eggsample")
4+
5+
@hookspec
6+
def eggsample_add_ingredients(ingredients):
7+
"""Change the used ingredients.
8+
9+
:param list ingredients: the ingredients to be modified
10+
"""
11+
12+
@hookspec
13+
def eggsample_prep_condiments(condiments):
14+
"""Reorganize the condiments tray.
15+
16+
:param dict condiments: some sauces and stuff
17+
"""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import random
2+
3+
from eggsample import hookimpl, hookspecs, lib, pm
4+
5+
condiments_tray = {"pickled walnuts": 13, "steak sauce": 4, "mushy peas": 2}
6+
7+
class FoodMaster2000:
8+
def __init__(self):
9+
self.ingredients = []
10+
11+
def add_ingredients(self):
12+
pm.hook.eggsample_add_ingredients(ingredients=self.ingredients)
13+
14+
def prepare_the_food(self):
15+
random.shuffle(self.ingredients)
16+
17+
def serve_the_food(self):
18+
pm.hook.eggsample_prep_condiments(condiments=condiments_tray)
19+
print(f"Your food: {', '.join(self.ingredients)}")
20+
print(f"Some condiments: {', '.join(condiments_tray.keys())}")
21+
22+
def main():
23+
pm.add_hookspecs(hookspecs)
24+
pm.load_setuptools_entrypoints("eggsample")
25+
pm.register(lib)
26+
fm2000 = FoodMaster2000()
27+
fm2000.add_ingredients()
28+
fm2000.prepare_the_food()
29+
fm2000.serve_the_food()
30+
31+
if __name__ == '__main__':
32+
main()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import eggsample
2+
3+
@eggsample.hookimpl
4+
def eggsample_add_ingredients(ingredients):
5+
basics = ["egg", "egg", "salt", "pepper"]
6+
print(f"Add {basics}")
7+
ingredients.extend(basics)
8+
9+
@eggsample.hookimpl
10+
def eggsample_prep_condiments(condiments):
11+
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())

0 commit comments

Comments
 (0)