an introduction to the zope 3 component architecture · to the zope 3 component architecture...

156
An Introduction to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008

Upload: others

Post on 07-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

An Introductionto the Zope 3 Component 

ArchitectureBrandon Craig Rhodes

Georgia Tech

NOLA Plone Symposium 2008

Page 2: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

This talk, on the whole, is divided into four parts

Page 3: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1. Enhancing objects

Page 4: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1. Enhancing objects2. Adapting objects

Page 5: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1. Enhancing objects2. Adapting objects

3. Component framework

Page 6: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1. Enhancing objects2. Adapting objects

3. Component framework4. Adapting for the web

Page 7: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Let's go!

Page 8: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Many programminglanguages use static 

typing

Page 9: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

float half(int n){   return n / 2.0;}

Page 10: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

float half(int n){   return n / 2.0;}

Page 11: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Python typing is dynamic

Page 12: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

def half(n):    return n / 2.0

Page 13: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

You don't worry about whether an object is of 

the right type

Page 14: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

You simply try using it

Page 15: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

“Duck Typing”

(Alex Martelli)

Page 16: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

“Duck Typing”

Walks like a duck?Quacks like a duck?

It's a duck!

Page 17: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

def half(n):    return n / 2.0

Page 18: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

def half(n):    return n / 2.0

(Is n willing to be divided by two?Then it's number­ish enough for us!)

Page 19: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Now, imagine...

Page 20: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Imagine a wonderful duck­processing library to which you want to pass 

an object

Page 21: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

But...

The object you want to pass isn't a duck?

Page 22: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What if it doesn'talready quack?

Page 23: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What if it bearsnot the least resemblance

to a duck!?

Page 24: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Example!

Page 25: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

You have a “Message” object from the Python 

“email” module

Page 26: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

>>> from email import message_from_file>>> e = message_from_file(open('msg.txt'))>>> print e<email.message.Message instance at ...>>>> e.is_multipart()True>>> for part in e.get_payload():... print part.get_content_type()text/plaintext/html

Page 27: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

multipart/mixed

text/plain

multipart/alternative

text/plain text/html

image/jpeg

Messagescan be

recursive

Page 28: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Imagine that we are writing a GUI email client

Page 29: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

And we want to show the parts in a TreeWidget

Page 30: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

The Tree widget needs:

method name() - returns name under which this tree node should be displayed

method children() - returns list of child nodes in the tree

method __len__() - returns number of child nodes beneath this one

Page 31: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

How can we add these behaviors to our 

Message?

Page 32: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

(How can we make an object which is not a duck 

behave like a duck?)

Page 33: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1. Subclassing

Page 34: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Create a “TreeMessage” class that inherits from the “Message” class...

Page 35: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

class TreeMessage(Message):

def name(self): return self.get_content_type()

def children(self): if not self.is_multipart(): return [] return [ TreeMessage(part) for part in self.get_payload() ]

def __len__(self): return len(self.children())

Page 36: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What will the test suite look like?

Page 37: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Remember:

“Untested code  is broken code”— Philipp von Weitershausen,

Martin Aspeli

Page 38: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Your test suitemust instantiate a 

“TreeMessage” and verify its tree­like behavior...

Page 39: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

txt = “ ” ” From: [email protected]: [email protected]: what an article!

Did you read Arts & Letters Daily today?“ ” ”m = message_from_string(txt, TreeMessage)assert m.name() == 'text/plain'assert m.children == []assert m.__len__() == 0

Page 40: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

We were lucky!

Page 41: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Our test can cheaply instantiate Messages.

Page 42: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

txt = “ ” ” From: [email protected]: [email protected]: what an article!

Did you read Arts & Letters Daily today?“ ” ”m = message_from_string(txt, TreeMessage)assert m.name() == 'text/plain'assert m.children == []assert m.__len__() == 0

Page 43: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What if we were subclassing an LDAP 

connector?!

We'd need an LDAP server just to run unit tests!

Page 44: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

We were lucky (#2)!

Page 45: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

The “message_from_string()” method let us specify an 

alternate factory!

Page 46: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

txt = “ ” ” From: [email protected]: [email protected]: what an article!

Did you read Arts & Letters Daily today?“ ” ”m = message_from_string(txt, TreeMessage)assert m.name() == 'text/plain'assert m.children == []assert m.__len__() == 0

Page 47: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Final note: we have just broken the “Message” 

class's behavior!

Page 48: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Python library manual7.1.1 defines “Message”:__len__():

Return the total number of headers, including duplicates.

Page 49: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

>>> t = “ ” ” From: [email protected]: [email protected]: what an article!

Did you read Arts & Letters Daily today?“ ” ”>>> m = message_from_file(t, Message)>>> print len(m)3>>> m = message_from_file(t, TreeMessage)>>> print len(m)0

Page 50: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

So how doessubclassing

score?

Page 51: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Page 52: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base classCannot test in isolation

Page 53: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Need control of factoryCannot test in isolation

Page 54: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Breaks if names collideNeed control of factoryCannot test in isolation

Page 55: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Breaks if names collideNeed control of factoryCannot test in isolation

Subclassing: D

Page 56: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

2. Using a mixin

Page 57: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Create a “TreeMessage” class that inherits from both “Message” and a 

“Mixin”...

Page 58: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

class Mixin(object): def name(self): return self.get_content_type() def children(self): if not self.is_multipart(): return [] return [ TreeMessage(part) for part in self.get_payload() ] def __len__(self): return len(self.children())

class TreeMessage(Message, Mixin): pass

Page 59: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Your test suite can then inherit from a mocked­up 

“message”...

Page 60: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

class FakeMessage(Mixin): def get_content_type(self): return 'text/plain' def is_multipart(self): return False def get_payload(self): return ''

m = FakeMessage()

assert m.name() == 'text/plain'assert m.children() == []assert m.__len__() == 0

Page 61: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

How doesa mixin rate?

Page 62: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Page 63: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base classCan test mixin by itself

Page 64: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Need control of factoryCan test mixin by itself

Page 65: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Breaks if names collideNeed control of factoryCan test mixin by itself

Page 66: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Breaks if names collideNeed control of factoryCan test mixin by itself

Mixin: C

Page 67: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

3. Monkey patching

Page 68: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

To “monkey patch” a class, you add or change 

its methods dynamically...

Page 69: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

def name(self): return self.get_content_type()def children(self): if not self.is_multipart(): return [] return [ Message(part) for part in self.get_payload() ]def __len__(self): return len(self.children())

Message.name = nameMessage.children = childrenMessage.__len__ = __len__

Page 70: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Is this desirable?

Page 71: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Don't care about factory

Page 72: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Changes class itselfDon't care about factory

Page 73: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Changes class itselfBroken by collisions

Don't care about factory

Page 74: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Changes class itselfBroken by collisions

Don't care about factory

Patches fight each other

Page 75: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Changes class itselfBroken by collisions

Don't care about factory

Ruby people do thisPatches fight each other

Page 76: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Changes class itselfBroken by collisions

Don't care about factory

Ruby people do thisMonkey patching: F

Patches fight each other

Page 77: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

4. Adapter

Page 78: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Touted in the Gang of Four book

(1994)

Page 79: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Idea: provide “Tree” functions through an entirely separate class

Message MessageTreeAdapter

get_content_type() name()is_multipart() call children()get_payload() __len__()

Page 80: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

class MessageTreeAdapter(object): def __init__(self, message): self.m = message def name(self): return self.m.get_content_type() def children(self): if not self.m.is_multipart(): return [] return [ TreeMessageAdapter(part) for part in self.m.get_payload() ] def __len__(self): return len(self.children())

Page 81: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

How does wrapping look in your code?

Page 82: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

IMAP library (or whatever)

TreeWidget

tw = TreeWidget(MessageTreeAdapter(msg))

Message object

Adapted object

Page 83: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Test suite can try adapting a mock­up object

Page 84: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

class FakeMessage(object): def get_content_type(self): return 'text/plain' def is_multipart(self): return True def get_payload(self): return []

m = MessageTreeAdapter(FakeMessage())assert m.name() == 'text/plain'assert m.children == []assert m.__len__() == 0

Page 85: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

How does the Adapterdesign pattern stack up?

Page 86: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Page 87: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base classCan test with mock­up

Page 88: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Don't need factoriesCan test with mock­up

Page 89: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Don't need factoriesCan test with mock­up

No collision worries

Page 90: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Don't need factoriesCan test with mock­up

Wrapping is annoyingNo collision worries

Page 91: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Don't need factoriesCan test with mock­up

Wrapping is annoyingAdapter: B

No collision worries

Page 92: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Q: Why call wrapping“annoying”?

Page 93: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

The example makesit look so easy!

Page 94: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

IMAP library (or whatever)

TreeWidget

tw = TreeWidget(TreeMessageAdapter(msg))

Message object

Adapted object

Page 95: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

A: The example looks easy because it only does 

adaptation once!

Page 96: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

But in a real application,it happens all through

your code...

Page 97: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

C(msg)

DBGUI

A(famtree)objects objectsIMAP

email

msg

Web

WidgetB(msg)Genealogy

3rd partyProducers

Adapters

A B C

3rd partyConsumers

C(msg)

Your application

Page 98: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

How can you avoid repeating yourself, and scattering information 

about adapters and consumers everywhere?

Page 99: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

IMAP library (or whatever)

TreeWidget

tw = TreeWidget(TreeMessageAdapter(msg))

Message object

Adapted object

Page 100: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

tw = TreeWidget(TreeMessageAdapter(msg))

Page 101: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

tw = TreeWidget(TreeMessageAdapter(msg))

The key is seeing that this code conflates two issues!

Page 102: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

tw = TreeWidget(TreeMessageAdapter(msg))

Why does this line work?

Page 103: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

tw = TreeWidget(TreeMessageAdapter(msg))

It works because a TreeWidget needs what our adapter provides.

Page 104: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

tw = TreeWidget(TreeMessageAdapter(msg))

But if we call the adapter then the need = want is hidden inside of our head!

Page 105: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

We need to define what the TreeWidget needs that 

our adapter provides!

Page 106: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

An interface is how we 

specify a set of behaviors

Page 107: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

(1988)

(1995)

An interface is how we 

specify a set of behaviors

Page 108: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,
Page 109: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

For the moment, forget Zope­the­web­framework

Page 110: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Instead, look at Zope the component framework:

zope.interfacezope.component

Page 111: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

With three simple steps, Zope will rid your code of 

manual adaptation

Page 112: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1. Define an interface2. Register our adapter3. Request adaptation

Page 113: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

from zope.interface import Interface

class ITree(Interface): def name(): “ ” ” Return this tree node's name.” ” ” def children(): “ ” ” Return this node's children.” ” ” def __len__(): “ ” ” Return how many children.” ” ”

Define

Page 114: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

from zope.component import provideAdapter

provideAdapter(MessageTreeAdapter, adapts=Message, provides=ITree)

Register

Page 115: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

from your_interfaces import ITreeclass TreeWidget(...): def __init__(self, arg): tree = ITree(arg) ...

Request

Page 116: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

from your_interfaces import ITreeclass TreeWidget(...): def __init__(self, arg): tree = ITree(arg) ... Zope will: 1. Recognize need 2. Find the registered adapter 3. Wrap and return the Message

Request

Page 117: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

from your_interfaces import ITreeclass TreeWidget(...): def __init__(self, arg): tree = ITree(arg) ... (Look! Zope is Pythonic!) i = int(32.1) l = list('abc') f = float(1024)

Request

Page 118: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

And that's it!

Page 119: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

And that's it!

Define an interfaceRegister our adapterRequest adaptation

Page 120: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

No harm to base class

Don't need factoriesCan test with mock­up

Adapters now dynamic!Registered adapter: A

No collision worries

Page 121: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

C(msg)

DBGUI

A(famtree)objects objectsIMAP

email

msg

Web

WidgetB(msg)Genealogy

3rd partyProducers

Adapters

A B C

3rd partyConsumers

C(msg)

Your application

Page 122: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

C(msg)

DBGUI

A(famtree)IMAP

email

msg

Web

WidgetB(msg)Genealogy

A B C

C(msg)

Whatadaptersprovide

Whatconsumers

need

Page 123: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

DBGUI

IMAP

email

Web

WidgetGenealogy

A B C

Page 124: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

The finale

Adapting for the Web

Page 125: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

dum ... dum ... dum ...

DAH DUM!

Page 126: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,
Page 127: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Grok

Page 128: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Web frameworkbuilt atop Zope 3

component architecture

Page 129: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Grok makesZope 3 simple to use

(and to present!)

Page 130: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Imagine a Person class

Page 131: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

The Person class was written by someone else

Page 132: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

The Person class is full of business logic, and stores instances in a database

Page 133: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

We want to browse Person objects on the Web

Page 134: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What might the Webneed the object to do?

Page 135: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

<HTML><HEAD><TITLE>Person JOE</TITLE></HEAD><BODY> This page presents the basic data we have regarding Joe. ...

Person

1. What's at a URL

3. What is its URL

2. HTML document

http://person_app/Joe

http://person_app/Joe

Page 136: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

1.

Page 137: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What's at this URL?

Page 138: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What's at this URL?http://person_app/Joe

# how Zope processes this URL:r = rootj = ITraverser(r).traverse('person_app')k = ITraverser(j).traverse('Joe')return k

Page 139: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

# what we write:class PersonTraverser(grok.Traverser): grok.context(PersonApp) def traverse(self, name): if person_exists(name): return get_person(name) return None

What's at this URL?http://person_app/Joe

Page 140: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

# what we write:class PersonTraverser(grok.Traverser): grok.context(PersonApp) def traverse(self, name): if person_exists(name): return get_person(name) return None

What's at this URL?http://person_app/Joe

Page 141: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

# what we write:class PersonTraverser(grok.Traverser): grok.context(PersonApp) def traverse(self, name): if person_exists(name): return get_person(name)

What's at this URL?http://person_app/Joe

Page 142: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

2.

Page 143: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

How does a Person render?

Page 144: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

app.pyclass PersonIndex(grok.View): grok.context(Person) grok.name('index')

app_templates/personindex.pt<html><head><title>All about <tal tal:replace=” context/name” /></title></head>...

How does a Person render?

Page 145: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

3.

Page 146: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

What is a person's URL?

Page 147: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

class PersonURL(grok.MultiAdapter): grok.adapts(Person, IHTTPRequest) grok.implements(IAbsoluteURL) def __init__(self, person, req): self.person, self.req = person, req def __call__(self): base = grok.url(grok.getSite()) return base + '/' + self.person.name

What is a person's URL?

Page 148: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

5 + 3 + 8 = 16 lines

Page 149: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

<HTML><HEAD><TITLE>Person JOE</TITLE></HEAD><BODY> This page presents the basic data we have regarding Joe. ...

Person

1. What's at a URL

3. What is its URL

2. HTML Document

http://person_app/Joe

http://person_app/Joe

PersonTraverser

PersonURL

PersonIndex

Page 150: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Other Zope adapter uses

Page 151: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Other Zope adapter usesIndexing — Index, Query, Search, ...Data schemas — Schema, Vocabulary, DublinCore ...Form generation — AddForm, EditForm, ...Security — SecurityPolicy, Proxy, Checker, ...Authentication — Login, Logout, Allow, Require, ...Copy and paste — ObjectMover, ObjectCopier, ...I18n — TranslationDomain, Translator, ...Appearance — Skins, macros, viewlets, ...

Much, much more!

Page 152: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Adapters can be local!

Global adapters

http://person_app/Joe

http:// person_app Joe

Local adapters

Page 153: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Coming Attraction

Page 154: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

five.grok

Page 155: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

five.grokLennart Regebro

Page 156: An Introduction to the Zope 3 Component Architecture · to the Zope 3 Component Architecture Brandon Craig Rhodes Georgia Tech NOLA Plone Symposium 2008. This talk, on the whole,

Thank you!

http://zope.org/Products/Zope3http://grok.zope.org/http://rhodesmill.org/brandon/adaptershttp://regebro.wordpress.com/zope­[email protected] mailing listgrok­[email protected] mailing listWeb Component Development with Zope 3 by PvW