Skip to content

Instantly share code, notes, and snippets.

@Deepakkothandan
Created August 26, 2018 19:11
Show Gist options
  • Save Deepakkothandan/82ad451e02114c360a7f4b2b733a6017 to your computer and use it in GitHub Desktop.
Save Deepakkothandan/82ad451e02114c360a7f4b2b733a6017 to your computer and use it in GitHub Desktop.
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