[Zope-dev] Namespaces, ZPublisher, DocumentTemplate question
Dieter Maurer
dieter@handshake.de
Sun, 22 Sep 2002 19:34:10 +0200
Andreas Kostyrka writes:
> I've got a product that inherits from Folder, and creates
> some magic "content" by overriding __bobo_traverse__.
>
> Now I've got the problem that I cannot access the "magic" items from
> within a DocumentTemplate like in <dtml-var special>.
>
> The interesting thing is, that I've overloaded __getattr__ and
> __getitem__, and it seems both are never called. (Actual code snippets
> at the end)
>
> So I'm wondering how DocumentTemplates (and say Page Templates) access
> the items and properties?
>
> Andreas
>
> code snippet:
> def __getattr__(self,k,*args):
> r=Folder.Folder.__getattr__(self,k,*args)
> print "__getattr__(%r,%r)==%r" % (self,key,r)
This will not work as you expect!
"__getattr__" is only called, when the normal lookup
does not succeed.
"Folder.__getattr__" is inherited from "Persistent"
and will serve only very few attributes.
For all others, you will get an "AttributeError", before
you print anything.
Furthermore, "__getattr__" takes only one (non-self)
arguement. Your code above (with this strange "*args" code)
will pass two. You will get a "TypeError" before you print
anything.
> def has_key(self,key):
> r=Folder.Folder.has_key(self,key)
"Folder" implements the mapping interface only very partially.
It does not implement "has_key".
> ...
> def __getitem__(self,key):
> print "__getitem__(%r,%r)" % (self,key)
> try:
> return Folder.Folder.__getitem__(self,key)
> except KeyError:
> try:
> return self.__bobo_traverse__(self,None,key)
> except AttributeError:
> raise KeyError,key
"__getitem__" is only called, when you explicitely use "instance[key]".
Dieter