Created
February 15, 2022 15:21
-
-
Save mathieureguer/ac74f97e1405778692b35dda67e78f48 to your computer and use it in GitHub Desktop.
A little helper function to toggle Subscriber on and off
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mojo.subscriber import * | |
def toggleSubscriberClass(SubscriberClass, register_function): | |
""" | |
Will register a SubscriberClass with the suplied register_function if that SubscriberClass is not already registered. | |
Will unregister a SubscriberClass with the corresponding unregister_function if that SubscriberClass is already registered. | |
Usage: | |
considering the Neighbours.py example in the Robofont documentation | |
if __name__ == '__main__': | |
toggleSubscriberClass(Neighbours, registerGlyphEditorSubscriber) | |
""" | |
register_function_map = { | |
registerRoboFontSubscriber: unregisterRoboFontSubscriber, | |
registerFontOverviewSubscriber: unregisterFontOverviewSubscriber, | |
registerGlyphEditorSubscriber: unregisterGlyphEditorSubscriber, | |
registerSpaceCenterSubscriber: unregisterSpaceCenterSubscriber, | |
registerCurrentFontSubscriber: unregisterCurrentFontSubscriber, | |
registerCurrentGlyphSubscriber: unregisterCurrentGlyphSubscriber, | |
} | |
assert register_function in register_function_map, f"{register_function} appears not to be a supported Subscriber registration method" | |
registered_subscribers = listRegisteredSubscribers(SubscriberClassName=SubscriberClass.__name__) | |
if len(registered_subscribers) > 0: | |
unregister_function = register_function_map.get(register_function) | |
for target_subscriber in registered_subscribers: | |
unregister_function(target_subscriber) | |
print(f"Toggle {SubscriberClass.__name__} off") | |
else: | |
register_function(SubscriberClass) | |
print(f"Toggle {SubscriberClass.__name__} on") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment