Created
August 26, 2018 19:11
-
-
Save Deepakkothandan/82ad451e02114c360a7f4b2b733a6017 to your computer and use it in GitHub Desktop.
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
import abc | |
import importlib | |
class Plugins(abc.ABCMeta): | |
plugins = dict() | |
def __new__(metaclass, name, bases, namespace): | |
cls = abc.ABCMeta.__new__( | |
metaclass, name, bases, namespace) | |
if isinstance(cls.name, str): | |
metaclass.plugins[cls.name] = cls | |
return cls | |
@classmethod | |
def get(cls, name): | |
if name not in cls.plugins: | |
print('Loading plugins from plugins.%s' % name) | |
importlib.import_module('plugins.%s' % name) | |
return cls.plugins[name] | |
class PluginBase(metaclass=Plugins): | |
@property | |
@abc.abstractmethod | |
def name(self): | |
raise NotImplemented() | |
class plugin(PluginBase): | |
name = 'plugin1' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment