Converts an unload hook into an application processor. >>> app = auto_application() >>> def f(): "something done after handling request" ... >>> app.add_processor(unloadhook(f))
(h)
| 681 | |
| 682 | |
| 683 | def unloadhook(h): |
| 684 | """ |
| 685 | Converts an unload hook into an application processor. |
| 686 | |
| 687 | >>> app = auto_application() |
| 688 | >>> def f(): "something done after handling request" |
| 689 | ... |
| 690 | >>> app.add_processor(unloadhook(f)) |
| 691 | """ |
| 692 | |
| 693 | def processor(handler): |
| 694 | try: |
| 695 | result = handler() |
| 696 | except: |
| 697 | # run the hook even when handler raises some exception |
| 698 | h() |
| 699 | raise |
| 700 | |
| 701 | if result and hasattr(result, "__next__"): |
| 702 | return wrap(result) |
| 703 | else: |
| 704 | h() |
| 705 | return result |
| 706 | |
| 707 | def wrap(result): |
| 708 | def next_hook(): |
| 709 | try: |
| 710 | return next(result) |
| 711 | except: |
| 712 | # call the hook at the and of iterator |
| 713 | h() |
| 714 | raise |
| 715 | |
| 716 | result = iter(result) |
| 717 | while True: |
| 718 | try: |
| 719 | yield next_hook() |
| 720 | except StopIteration: |
| 721 | return |
| 722 | |
| 723 | return processor |
| 724 | |
| 725 | |
| 726 | def autodelegate(prefix=""): |