88
99from warnet .constants import HOOK_NAME_KEY , HOOKS_API_FILE , HOOKS_API_STEM
1010
11- hook_registry : set [str ] = set ()
11+ hook_registry : set [Callable [..., Any ] ] = set ()
1212imported_modules = {}
1313
1414
@@ -26,13 +26,13 @@ def my_function():
2626 pass
2727 ```
2828 """
29- if func .__name__ in hook_registry :
29+ if func .__name__ in [ fn . __name__ for fn in hook_registry ] :
3030 print (
3131 f"Cannot re-use function names in the Warnet plugin API -- "
3232 f"'{ func .__name__ } ' has already been taken."
3333 )
3434 sys .exit (1 )
35- hook_registry .add (func . __name__ )
35+ hook_registry .add (func )
3636
3737 if not imported_modules :
3838 load_user_modules ()
@@ -66,21 +66,35 @@ def create_hooks(directory: Path):
6666 with open (init_file_path , "w" ) as file :
6767 file .write (f"# API Version: { get_version ('warnet' )} " )
6868 # For each enum variant, create a corresponding decorator function
69- for hook in hook_registry :
70- file .write (decorator_code .format (hook = hook , HOOK_NAME_KEY = HOOK_NAME_KEY ))
69+ for func in hook_registry :
70+ file .write (
71+ decorator_code .format (
72+ hook = func .__name__ , doc = func .__doc__ , HOOK_NAME_KEY = HOOK_NAME_KEY
73+ )
74+ )
7175
7276
7377decorator_code = """
7478
7579
7680def pre_{hook}(func):
77- \" \" \" Functions with this decoration run before `{hook}`.\" \" \"
81+ \" \" \"
82+ Functions with this decoration run before `{hook}`.
83+
84+ `{hook}` documentation:
85+ {doc}
86+ \" \" \"
7887 func.__annotations__['{HOOK_NAME_KEY}'] = 'pre_{hook}'
7988 return func
8089
8190
8291def post_{hook}(func):
83- \" \" \" Functions with this decoration run after `{hook}`.\" \" \"
92+ \" \" \"
93+ Functions with this decoration run after `{hook}`.
94+
95+ `{hook}` documentation:
96+ {doc}
97+ \" \" \"
8498 func.__annotations__['{HOOK_NAME_KEY}'] = 'post_{hook}'
8599 return func
86100"""
0 commit comments