11"""A simple entrypoint-free plugin system for python"""
2+
23from __future__ import annotations
34
45import sys
@@ -338,6 +339,7 @@ def __init__(
338339 self .name = spec .__name__
339340 self .required = required
340341 self .result = result
342+ self .debug = False
341343 self .warn_sync_impl_on_async = warn_sync_impl_on_async
342344
343345 def _get_results (
@@ -350,6 +352,11 @@ def _get_results(
350352 result = self .result if result is None else result
351353
352354 if callable (result ):
355+ if self .debug :
356+ print (
357+ "[simplug] - Gathering results using "
358+ f"custom function { result .__name__ } "
359+ )
353360 return result (calls )
354361
355362 if isinstance (result , SimplugResult ):
@@ -366,51 +373,75 @@ def _get_results(
366373 if result & 0b010_0000 :
367374 out = [makecall (call ) for call in calls ]
368375 if result == SimplugResult .ALL .value :
376+ if self .debug :
377+ print ("[simplug] - Returning all results" )
369378 return out
370379 if result == SimplugResult .ALL_AVAILS .value :
380+ if self .debug :
381+ print ("[simplug] - Returning all available (non-None) results" )
371382 return [x for x in out if x is not None ]
372383 if result == SimplugResult .ALL_FIRST .value :
373384 if not out :
374385 raise ResultUnavailableError
386+ if self .debug :
387+ print ("[simplug] - Returning first result" )
375388 return out [0 ]
376389 if result == SimplugResult .ALL_LAST .value :
377390 if not out :
378391 raise ResultUnavailableError
392+ if self .debug :
393+ print ("[simplug] - Returning last result" )
379394 return out [- 1 ]
380395 if result == SimplugResult .ALL_FIRST_AVAIL .value :
381396 if not out or all (x is None for x in out ):
382397 raise ResultUnavailableError
398+ if self .debug :
399+ print ("[simplug] - Returning first available (non-None) result" )
383400 return next (x for x in out if x is not None )
384401 if result == SimplugResult .ALL_LAST_AVAIL .value :
385402 if not out or all (x is None for x in out ):
386403 raise ResultUnavailableError
404+ if self .debug :
405+ print ("[simplug] - Returning last available (non-None) result" )
387406 return next (x for x in reversed (out ) if x is not None )
388407
389408 if result == SimplugResult .FIRST .value :
390409 if not calls :
391410 raise ResultUnavailableError
411+ if self .debug :
412+ print ("[simplug] - Returning first result" )
392413 return makecall (calls [0 ])
393414 if result == SimplugResult .LAST .value :
394415 if not calls :
395416 raise ResultUnavailableError
417+ if self .debug :
418+ print ("[simplug] - Returning last result" )
396419 return makecall (calls [- 1 ])
397420 if result == SimplugResult .FIRST_AVAIL .value :
398421 for call in calls :
399422 ret = makecall (call )
400423 if ret is not None :
424+ if self .debug :
425+ print ("[simplug] - Returning first available (non-None) result" )
401426 return ret
402427 raise ResultUnavailableError
403428 if result == SimplugResult .LAST_AVAIL .value :
404429 for call in reversed (calls ):
405430 ret = makecall (call )
406431 if ret is not None :
432+ if self .debug :
433+ print ("[simplug] - Returning last available (non-None) result" )
407434 return ret
408435 raise ResultUnavailableError
409436 if result == SimplugResult .SINGLE .value :
410437 if not calls :
411438 raise ResultUnavailableError
412439 for call in calls :
413440 if call .plugin == plugin :
441+ if self .debug :
442+ print (
443+ f"[simplug] - Returning single result from plugin { plugin } "
444+ )
414445 return makecall (call )
415446 if plugin is not None :
416447 raise ResultUnavailableError
@@ -420,6 +451,11 @@ def _get_results(
420451 "but a single result is expected. Using the last one." ,
421452 MultipleImplsForSingleResultHookWarning ,
422453 )
454+ if self .debug :
455+ print (
456+ f"[simplug] - Returning single result from the last plugin "
457+ f"{ calls [- 1 ].plugin } "
458+ )
423459 return makecall (calls [- 1 ])
424460
425461 def __call__ (self , * args , ** kwargs ):
@@ -449,6 +485,12 @@ def __call__(self, *args, **kwargs):
449485 "Cannot use __plugin with non-SimplugResult.(TRY_)SINGLE hooks"
450486 )
451487
488+ if self .debug :
489+ print (
490+ f"[simplug] Calling hook { self .name } with args={ args } , kwargs={ kwargs } , "
491+ f"result={ self .result } "
492+ )
493+
452494 _plugin = kwargs .pop ("__plugin" , None )
453495 calls = []
454496 for plugin in self .simplug_hooks ._registry .values ():
@@ -458,6 +500,9 @@ def __call__(self, *args, **kwargs):
458500
459501 if hook is not None :
460502 plugin_args = (plugin .plugin , * args ) if hook .has_self else args
503+ if self .debug :
504+ print (f"[simplug] - Pushing call { plugin .name } .{ self .name } " )
505+
461506 calls .append (
462507 SimplugImplCall (plugin .name , hook .impl , plugin_args , kwargs )
463508 )
@@ -478,6 +523,11 @@ async def _get_results(
478523 result = self .result if result is None else result
479524
480525 if callable (result ):
526+ if self .debug :
527+ print (
528+ "[simplug] - Gathering results using "
529+ f"custom async function { result .__name__ } "
530+ )
481531 return await result (calls )
482532
483533 if isinstance (result , SimplugResult ):
@@ -498,51 +548,77 @@ async def _get_results(
498548 if result & 0b010_0000 :
499549 out = [await makecall (call , True ) for call in calls ]
500550 if result == SimplugResult .ALL .value :
551+ if self .debug :
552+ print ("[simplug] - Returning all results" )
501553 return out
502554 if result == SimplugResult .ALL_AVAILS .value :
555+ if self .debug :
556+ print ("[simplug] - Returning all available (non-None) results" )
503557 return [x for x in out if x is not None ]
504558 if result == SimplugResult .ALL_FIRST .value :
505559 if not out :
506560 raise ResultUnavailableError
561+ if self .debug :
562+ print ("[simplug] - Returning first result" )
507563 return out [0 ]
508564 if result == SimplugResult .ALL_LAST .value :
509565 if not out :
510566 raise ResultUnavailableError
567+ if self .debug :
568+ print ("[simplug] - Returning last result" )
511569 return out [- 1 ]
512570 if result == SimplugResult .ALL_FIRST_AVAIL .value :
513571 if not out or all (x is None for x in out ):
514572 raise ResultUnavailableError
573+ if self .debug :
574+ print ("[simplug] - Returning first available (non-None) result" )
515575 return next (x for x in out if x is not None )
516576 if result == SimplugResult .ALL_LAST_AVAIL .value :
517577 if not out or all (x is None for x in out ):
518578 raise ResultUnavailableError
579+ if self .debug :
580+ print ("[simplug] - Returning last available (non-None) result" )
519581 return next (x for x in reversed (out ) if x is not None )
520582
521583 if result == SimplugResult .FIRST .value :
522584 if not calls :
523585 raise ResultUnavailableError
586+ if self .debug :
587+ print ("[simplug] - Returning first result" )
524588 return await makecall (calls [0 ], True )
525589 if result == SimplugResult .LAST .value :
526590 if not calls :
527591 raise ResultUnavailableError
592+ if self .debug :
593+ print ("[simplug] - Returning last result" )
528594 return await makecall (calls [- 1 ], True )
529595 if result == SimplugResult .FIRST_AVAIL .value :
530596 for call in calls :
531597 ret = await makecall (call , True )
532598 if ret is not None :
599+ if self .debug :
600+ print (
601+ "[simplug] - Returning first available (non-None) result"
602+ )
533603 return ret
534604 raise ResultUnavailableError
535605 if result == SimplugResult .LAST_AVAIL .value :
536606 for call in reversed (calls ):
537607 ret = await makecall (call , True )
538608 if ret is not None :
609+ if self .debug :
610+ print ("[simplug] - Returning last available (non-None) result" )
539611 return ret
540612 raise ResultUnavailableError
541613 if result == SimplugResult .SINGLE .value :
542614 if not calls :
543615 raise ResultUnavailableError
544616 for call in calls :
545617 if call .plugin == plugin :
618+ if self .debug :
619+ print (
620+ f"[simplug] - Returning single result from plugin { plugin } "
621+ )
546622 return await makecall (call , True )
547623 if plugin is not None :
548624 raise ResultUnavailableError
@@ -552,6 +628,11 @@ async def _get_results(
552628 "but no plugin was specified. Using the last one." ,
553629 MultipleImplsForSingleResultHookWarning ,
554630 )
631+ if self .debug :
632+ print (
633+ f"[simplug] - Returning single result from the last plugin "
634+ f"{ calls [- 1 ].plugin } "
635+ )
555636 return await makecall (calls [- 1 ], True )
556637
557638 async def __call__ (self , * args , ** kwargs ):
@@ -581,6 +662,12 @@ async def __call__(self, *args, **kwargs):
581662 "Cannot use __plugin with non-SimplugResult.(TRY_)SINGLE hooks"
582663 )
583664
665+ if self .debug :
666+ print (
667+ f"[simplug] Calling async hook { self .name } with args={ args } , "
668+ f"kwargs={ kwargs } , result={ self .result } "
669+ )
670+
584671 _plugin = kwargs .pop ("__plugin" , None )
585672 calls = []
586673 for plugin in self .simplug_hooks ._registry .values ():
@@ -591,6 +678,8 @@ async def __call__(self, *args, **kwargs):
591678 continue
592679
593680 plugin_args = (plugin .plugin , * args ) if hook .has_self else args
681+ if self .debug :
682+ print (f"[simplug] - Pushing call { plugin .name } .{ self .name } " )
594683 calls .append (SimplugImplCall (plugin .name , hook .impl , plugin_args , kwargs ))
595684
596685 return await self ._get_results (calls , plugin = _plugin )
0 commit comments