paula.testing: test for finding txt files + minor bugfix
[paula/paual.testing.git] / paula.examples / src / paula / examples / adapters.py
blobeb3f1220a8183c1376efbbe83d7f727e9bebeef9
1 # Copyright (c) 2008 by Florian Friesdorf
3 # GNU Affero General Public License (AGPL)
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
18 """
19 """
20 __author__ = "Florian Friesdorf <flo@chaoflow.net>"
21 __docformat__ = "plaintext"
23 from zope.component import adapts
24 from zope.interface import alsoProvides, implements, Interface
25 from zope.interface import providedBy
27 from paula.authentication.interfaces import IAuthProvider
29 from paula.grouputil.interfaces import IMembershipProvider
31 from paula.properties.interfaces import IPropertyProvider
32 from paula.properties.interfaces import IPropertyInterface
34 from paula.examples.interfaces import IBasicUser
35 from paula.examples.interfaces import IBasicGroup
38 class AuthProviderFromBasicUser(object):
39 """Basically a read-only wrapper demonstrating the concept
41 >>> content = Mock(title='user', password='correct')
42 >>> adapter = AuthProviderFromBasicUser(content)
43 >>> adapter.id == content.title
44 True
45 >>> adapter.validate(login='foo', password='correct')
46 True
47 >>> adapter.validate(login='foo', password='wrong')
48 False
49 """
50 adapts(IBasicUser)
51 implements(IAuthProvider)
53 @property
54 def id(self):
55 return self.context.title
57 def __init__(self, context):
58 self.context = context
60 def validate(self, login=u"", password=u""):
61 return password == self.context.password
64 class BasicPropertyProvider(object):
65 """This provides the same IPropertyInterface-providing interfaces as the
66 adpated context provides, and all attributes not defined on self,
67 are passed on to the context.
69 >>> from zope.interface import alsoProvides, Attribute
70 >>> content = Mock(
71 ... title='login',
72 ... password='pass',
73 ... email='foo@bar',
74 ... )
76 >>> class IA(Interface):
77 ... email = Attribute('email')
79 >>> alsoProvides(content, IA)
80 >>> alsoProvides(IA, IPropertyInterface)
82 >>> adapter = BasicPropertyProvider(content)
83 >>> adapter.id == content.title
84 True
85 >>> adapter.email == content.email
86 True
87 >>> getattr(adapter, 'password', 'foo')
88 'foo'
89 >>> IPropertyProvider.providedBy(adapter)
90 True
91 >>> IA.providedBy(adapter)
92 True
93 """
94 adapts(Interface)
95 implements(IPropertyProvider)
97 @property
98 def id(self):
99 return self.context.title
101 def __init__(self, context):
104 propifs = filter(
105 IPropertyInterface.providedBy,
106 list(providedBy(context)),
108 alsoProvides(self, propifs)
109 self.context = context
111 def __getattr__(self, name):
112 props = []
113 for x in list(providedBy(self)):
114 props += list(x)
115 if name not in props:
116 raise AttributeError
117 return getattr(self.context, name)
120 class MembershipProviderFromBasicGroup(object):
123 adapts(IBasicGroup)
124 implements(IMembershipProvider)
126 def __init__(self, context):
127 self.context = context
129 @property
130 def groups(self):
131 return (self.context.title,)
133 @property
134 def members(self):
135 return tuple(self.context.members)