Skip to content

Instantly share code, notes, and snippets.

@shaunbrady
Created May 14, 2015 22:10
Show Gist options
  • Save shaunbrady/c5b3cb5e590785631ee3 to your computer and use it in GitHub Desktop.
Save shaunbrady/c5b3cb5e590785631ee3 to your computer and use it in GitHub Desktop.
POC for Plugin model
#!/usr/bin/python
import pprint
import inspect
from copy import deepcopy
class NimbisActionPluginMeta(type):
# we use __init__ rather than __new__ here because we want
# to modify attributes of the class *after* they have been
# created
def __init__(cls, name, bases, dct):
if not hasattr(cls, 'registry'):
cls.registry = {}
for base in inspect.getmro(cls):
empty_action = {
'can_hooks': [],
'frontend_hooks': [],
'backend_hooks': []}
if hasattr(base, 'actions'):
#pprint.pprint(base.actions)
for action in base.actions:
if action not in cls.registry:
cls.registry[action] = deepcopy(empty_action)
for hook_type in empty_action:
if hook_type not in base.actions[action]:
continue
for func in base.actions[action][hook_type]:
if func not in cls.registry[action][hook_type]:
cls.registry[action][hook_type].append(func)
super(NimbisActionPluginMeta, cls).__init__(name, bases, dct)
class DesktopMixin(object):
def one(self):
print "one"
def two(self):
print "two"
def three(self):
print "three"
def four(self):
print "four"
def nine(self):
print "other nine"
actions = {
'change_resolution': {
'can_hooks': (one,),
'frontend_hooks': (three, four),},
'start': {
'can_hooks': (two, nine)}}
class StuffMixin(object):
def nine(self):
print "nine"
actions = {
'start': {
'can_hooks': (nine,)}}
class Ec2Mixin(object):
def five(self):
print "five {0}".format(self.uuid)
def six(self):
print "six"
def seven(self):
print "seven"
def eight(self):
print "eight"
actions = {
'start': {
'can_hooks': (five, six),
'frontend_hooks': (seven, eight)}}
#class Session(DesktopMixin, StuffMixin):
class Session(StuffMixin, DesktopMixin):
pass
class Ec2Session(Session,Ec2Mixin):
__metaclass__ = NimbisActionPluginMeta
def __init__(self):
self.uuid = "my uuid"
for can in self.registry['start']['can_hooks']:
can(self)
for front_end in self.registry['start']['frontend_hooks']:
front_end(self)
class JustDesktop(Session):
__metaclass__ = NimbisActionPluginMeta
pass
#print "###### Session ######"
#session = Session()
#print "Session registry: {0}".format(session.registry)
print "###### Ec2Session ######"
ec2session = Ec2Session()
pprint.pprint(ec2session.registry)
print "###### JustDesktop ######"
jf = JustDesktop()
pprint.pprint(jf.registry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment