[Zope-dev] Proxy Object / __getattr__ / Acquisition

Johan Carlsson [Torped] johanc@torped.se
Fri, 30 Aug 2002 08:06:03 +0200


At 14:28 2002-08-29 -0700, sean.upton@uniontrib.com said:
>I am trying to implement a proxy class (specifically for the purposes of
>multi-versioned document objects (folderish proxies that contain the object

Hi Sean,
We've been "stealing" some code from CMFCore.Skinnable to
do similar things (multi-language objects):
Skinnable gets the skins when the objects is being wrapped (e.g.
in the objects __of__ method) and overrides __getattr__ which
uses the skins or falls back to an unbound superGetAttr method
that points to the inherited __getattr__.
(The superGetAttr have pussled us because it seams like superGetAttr is=
 None,
but everything still work as expected.)

We replace the skin with a VeryTinyDataWrapper which (of course) implements=
=20
Acquisition.Implicit.

Would this work better that the way your doing it?
We currently implementing this so I don't know for sure that it
is faster, but it's from CMF (a key part to) so it shouldn't be to slow or?



     def setupCurrentLanguageData(self):
         #the request part is a rest from Skinnable, I don't think it can=20
be removed.
         request=3Dself.request
         #replace this with anything that returns
         lang_code=3Dself.EasyLanguageService.getCurrentLanguage()
         ob =3D self._current_language.get(lang_code, VerySmallData())
         self._v_c_language =3D (request, ob, {})

     def __getattr__(self, name, marker=3DNone):
         # OK, see if we can find the language service:
         if not name.startswith('_') and not name.startswith('aq_'):
             cl =3D self._v_c_language
             if cl is not None:
                 request, ob, ignore =3D cl
                 if not ignore.has_key(name):
                     subob =3D getattr(ob, name, _marker)
                     if subob is not _marker:
                         # Return it in context of self, forgetting
                         # its location and acting as if it were located
                         # in self.
                         return aq_base(subob)
                     else:
                         ignore[name] =3D 1
         if superGetAttr is None:
             raise AttributeError, name
         return superGetAttr(self, name)

     def __of__(self, parent):
         '''
         Sneakily sets up the current language then returns the wrapper
         that Acquisition.Implicit.__of__() would return.
         '''
         w_self =3D ImplicitAcquisitionWrapper(aq_base(self), parent)
         try:
             w_self.setupCurrentLanguageData()
         except:
             # This shouldn't happen, even if the requested current language
             # does not exist.
             import sys
             from zLOG import LOG, ERROR
             LOG('CMFCore', ERROR, 'Unable to setupCurrentLanguageData()',
                 error=3Dsys.exc_info())
         return w_self

Regards,
Johan Carlsson


--=20
Torped Strategi och Kommunikation AB
Johan Carlsson
johanc@torped.se

Mail:
Birkagatan 9
SE-113 36  Stockholm
Sweden

Visit:
V=E4stmannagatan 67, Stockholm, Sweden

Phone +46-(0)8-32 31 23
Fax +46-(0)8-32 31 83
Mobil +46-(0)70-558 25 24
http://www.torped.se
http://www.easypublisher.com