Skip to content

Instantly share code, notes, and snippets.

@avoinea
Last active July 28, 2016 16:07
Show Gist options
  • Save avoinea/a64702f9e904b6404ede5d7ac87d7074 to your computer and use it in GitHub Desktop.
Save avoinea/a64702f9e904b6404ede5d7ac87d7074 to your computer and use it in GitHub Desktop.
ZCA: Adapters
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from zope import interface
from zope import component
################################################################################
# Geek Model
################################################################################
class IGeek(interface.Interface):
""" Regular geek """
@interface.implementer(IGeek)
class Geek(object):
legs = 2
################################################################################
# Adapters
################################################################################
gsm = component.getGlobalSiteManager()
class ISkills(interface.Interface):
""" Skills """
def show(self):
""" Show me your skills
"""
################################################################################
# Dancing Skills
################################################################################
@interface.implementer(ISkills)
@component.adapter(IGeek)
class DancingSkills(object):
def __init__(self, context):
self.context = context
def show(self):
if not getattr(self.context, 'legs', None):
return u"Și cam cum ai vrea să dansez? În mâini?"
elif self.context.legs < 2:
return u"Nu prea am cum să dansez într-un picior. Doooh."
return u"Hai să rupem scena, frate!"
gsm.registerAdapter(DancingSkills, name=u'dancing')
################################################################################
# Super Geek Model
################################################################################
class ISuperGeek(IGeek):
""" I'm a super geek
"""
@interface.implementer(ISuperGeek)
class SuperGeek(object):
legs = u"N-ai tu treabă măh. Am câte picioare vreau eu!"
################################################################################
# Pro Dancing skills
################################################################################
@interface.implementer(ISkills)
@component.adapter(ISuperGeek)
class ProDancingSkills(object):
def __init__(self, context):
self.context = context
def show(self):
return "Eu dansez și'n mâini, da!?!"
gsm.registerAdapter(ProDancingSkills, name=u'dancing')
################################################################################
# Codding skills
################################################################################
@interface.implementer(ISkills)
@component.adapter(IGeek)
class BasicCoddingSkills(object):
def __init__(self, context):
self.context = context
def show(self):
return "Ooo, da!"
gsm.registerAdapter(BasicCoddingSkills, name=u'coding')
################################################################################
if __name__ == "__main__":
alin = Geek()
alin_dancing_skills = component.getAdapter(alin, ISkills, 'dancing')
print "HotGirl: Dansezi?"
print "Alin: " + alin_dancing_skills.show()
alin.legs = 1
print "HotGirl: Dansezi?"
print "Alin: " + alin_dancing_skills.show()
alin.legs = 0
print "HotGirl: Dansezi?"
print "Alin: " + alin_dancing_skills.show()
david = SuperGeek()
david_dancing_skill = component.getAdapter(david, ISkills, 'dancing')
print "HotGirl: Dansezi?"
print "David: " + david_dancing_skill.show()
interface.alsoProvides(alin, ISuperGeek)
print "HotGirl: Dansezi?"
print "Alin: " + alin_dancing_skills.show()
david_codding_skill = component.getAdapter(alin, ISkills, 'coding')
print "HotGirl: Codezi?"
print "David: " + david_codding_skill.show()
import pdb; pdb.set_trace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment