Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal for more generic function call interceptions #5254

Merged
merged 1 commit into from
Feb 23, 2022

Conversation

ThiefMaster
Copy link
Member

Recently we've got a bunch of PRs adding signals that are pretty much just providing a way to tamper with the arguments of certain function calls. And while we're open to accepting such PRs, I had the feeling that this was getting a bit messy, so I thought about providing a more standard way to handle this. While it would still require PRs to adjust a function call in order to provide this type of hook, it'd be much simpler than building a dict and updating it based on the signal return values...

Feedback welcome!


Also, for our UN colleagues @OmeGak @vasantvohra @SegiNyn, maybe you can have a look as well and see if you'd find this useful? Here's an example on how I'd use this instead of the recently-added draw_item_on_badge signal:

diff --git a/indico/modules/events/registration/badges.py b/indico/modules/events/registration/badges.py
index 918edd21fa..baec526e3a 100644
--- a/indico/modules/events/registration/badges.py
+++ b/indico/modules/events/registration/badges.py
@@ -13,12 +13,11 @@ from reportlab.lib.units import cm
 from reportlab.lib.utils import ImageReader
 from werkzeug.exceptions import BadRequest

-from indico.core import signals
 from indico.modules.designer.pdf import DesignerPDFBase
 from indico.modules.events.registration.settings import DEFAULT_BADGE_SETTINGS
 from indico.util.i18n import _
 from indico.util.placeholders import get_placeholders
-from indico.util.signals import values_from_signal
+from indico.util.signals import make_interceptable


 FONT_SIZE_RE = re.compile(r'(\d+)(pt)?')
@@ -101,15 +100,9 @@ class RegistrantsListToBadgesPDF(DesignerPDFBase):
             else:
                 continue

-            item_data = {'item': item, 'text': text, 'pos_x': pos_x, 'pos_y': pos_y}
-            for update in values_from_signal(
-                signals.event.designer.draw_item_on_badge.send(registration, items=items, height=self.height,
-                                                               width=self.width, data=item_data),
-                as_list=True
-            ):
-                item_data.update(update)
-            self._draw_item(canvas, item_data['item'], tpl_data, item_data['text'], item_data['pos_x'],
-                            item_data['pos_y'])
+            draw_item = make_interceptable(self._draw_item, ctx={'registration': registration, 'items': items,
+                                                                 'height': self.height, 'width': self.width})
+            draw_item(canvas, item, tpl_data, text, pos_x, pos_y)

and the plugin could be doing something like this then:

    def init(self):
        super().init()
        from indico.modules.events.registration.badges import RegistrantsListToBadgesPDF
        from indico.util.signals import interceptable_sender
        self.connect(signals.plugin.interceptable_function, self._intercept_draw_item,
                     sender=interceptable_sender(RegistrantsListToBadgesPDF._draw_item))

    def _intercept_draw_item(self, sender, args, ctx, RETURN_NONE, **kwargs):
        if args.arguments['content'] == 'CERN':
            # this skips the item - if we force a return value, the original function won't get called
            return RETURN_NONE
        elif args.arguments['content']:
            args.arguments['content'] = f"[{args.arguments['content'].strip()}]"
        args.arguments['item']['color'] = '#ff00cc'
        args.arguments['item']['background_color'] = '#000000'

@ThiefMaster ThiefMaster added this to the v3.1 milestone Feb 14, 2022
@ThiefMaster ThiefMaster merged commit bf50833 into indico:3.1.x Feb 23, 2022
@ThiefMaster ThiefMaster deleted the interceptable-call-signal branch February 23, 2022 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant