Created
April 18, 2020 07:48
-
-
Save multun/40a67f9eb97bf6ccc7f173ecdc2facfe to your computer and use it in GitHub Desktop.
A Mixin class for making class based plugins
This file contains 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
#!/usr/bin/env python3 | |
class PluginSubclassesMixin: | |
def __init_subclass__(cls, register_base=False, **kwargs): | |
super().__init_subclass__(**kwargs) | |
if PluginSubclassesMixin not in cls.__bases__: | |
return | |
cls.subclasses = [] | |
if register_base: | |
cls.subclasses.append(cls) | |
@classmethod | |
def mixin_subclass_init(subcls, **kwargs): | |
super(cls, subcls).__init_subclass__(**kwargs) | |
cls.subclasses.append(subcls) | |
cls.__init_subclass__ = mixin_subclass_init | |
class Stuffinators(PluginSubclassesMixin, register_base=True): | |
pass | |
class Stuffluff(Stuffinators): | |
pass | |
class Stufflargh(Stuffluff): | |
pass | |
print("Stuffinators", Stuffinators.subclasses) | |
print("Stuffluff", Stuffluff.subclasses) | |
print("Stufflargh", Stufflargh.subclasses) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment