|
| 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() |
0 commit comments