Skip to content

Instantly share code, notes, and snippets.

@shaunbrady
Created May 13, 2015 16:03
Show Gist options
  • Select an option

  • Save shaunbrady/51e41083cc8f3449231a to your computer and use it in GitHub Desktop.

Select an option

Save shaunbrady/51e41083cc8f3449231a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
class TestMeta(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'):
# this is the base class. Create an empty registry
print "cls is {0}".format(cls)
print "name is {0}".format(name)
print "bases is {0}".format(bases)
print "dct is {0}".format(dct)
cls.registry = {}
cls.registry['cans'] = set()
cls.registry['front_ends'] = set()
else:
# this is a derived class. Add cls to the registry
#interface_id = name.lower()
#cls.registry[interface_id] = cls
if hasattr(cls, 'cans'):
cls.registry['cans'].update(cls.cans)
if hasattr(cls, 'front_ends'):
cls.registry['front_ends'].update(cls.front_ends)
#print dir(cls)
super(TestMeta, cls).__init__(name, bases, dct)
#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, 'can_registry'):
# # this is the base class. Create an empty registry
# cls.can_registry = []
# cls.frontend_registry = []
# else:
# # this is a derived class. Add cls to the registry
# cls.can_registry.append(cls.can)
# cls.frontend_registry.append(cls.front_end)
# super(NimbisActionPluginMeta, cls).__init__(name, bases, dct)
#
class Test(object):
__metaclass__ = TestMeta
class Test1(object):
__metaclass__ = TestMeta
class Foo(Test):
def one(self):
print "one"
def two(self):
print "two"
def three(self):
print "three"
def four(self):
print "four"
cans = (one, two,)
front_ends = (three, four,)
class Bar(Test):
def five(self):
print "five {0}".format(self.uuid)
def six(self):
print "six"
def seven(self):
print "seven"
def eight(self):
print "eight"
cans = (five, six,)
front_ends = (seven, eight,)
class Both(Foo,Bar):
def __init__(self):
self.uuid = "my uuid"
for can in self.registry['cans']:
can(self)
for front_end in self.registry['front_ends']:
front_end(self)
class JustFoo(Foo):
pass
b = Both()
print b.registry
jf = JustFoo()
print jf.registry
print Test.registry
print Test1.registry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment