[Zope-dev] Overriding __getattr__ for requests only.

Jeff K. Hoffman Jeff K. Hoffman" <jkhoffman@carolina.rr.com
Fri, 12 Nov 1999 15:57:00 -0500 (EST)


Hello, everyone.

I am trying to write a new subclass of OFS.Folder (as a python product) to
do the following: When a user requests /myFolder/index_html, myFolder
will search itself, then its list of 'parents', returning the attribute
from one of them if they have it, and raising an AttributeError if not.

I have had some moderate success by overloading __getattr__ as follows:

  def __getattr__(self, name):
      try:
          return OFS.Folder.Folder.__getattr__(self, name)
      except:
          for parent in self.parentList:
              try:
                  return getattr(parent, name)
              except AttributeError:
                  pass
          raise AttributeError, name

This works, in the sense that I can "inherit" features from the parents,
but I think there are some fundamental flaws in my approach for the
following reasons:

  o I would like for the user to still be able to create attributes in my
    folder with the same name as one of the attributes in the parents.
    This does not work, currently. If I define index_html in a "parent"
    folder and attempt to do the same in a "child", I get a 'The id
    index_html is invalid - it is already in use.' error.
  o Whenever I click on a folder which has one or more "parents" in the
    top-level management interface (http://localhost/manage), I get a
    NameError for 'n_'. This does not occur, however, when I manage my
    folder via http://localhost/myFolder/manage.

So, my question is, is there any way for me to achieve this behavior
without causing the problems above? I am thinking there must be a way to
search the parents if AND ONLY IF it is for an incoming web request.

I have posted the code to zope.org at:

  http://www.zope.org/Members/jkhoffman/TransFolder.tgz

Once you install the product, click the 'Parents' tab to assign a parent.
This thing is barely working, and is one of my first python products, so
please don't laugh at me too much. :)

If anyone can give me a push in the right direction I would be extremely
grateful.

--Jeff