Zope Methods with a Dot in the name
Again one of those boring work problems: I have a bunch of Zope python / filesystem products. Most of them when instantiated contain an image called "preview.jpg". One of the little critters is different though. It wants to have a Zope Page Template (ZPT) instead of "preview.jpg". But Zope (and I think python) don't allow dots in method names. I banged my head on this particular wall for a while, until first Peter Bengtsson and then Chris McDonough gave me a big push on #zope. Read on for the solution...
What I have in the end is a class definition (in the .py file of my zope product, e.g. mythingy.py), like:
class mythingy( based_on_thingy_class ):
"""
Pretends to have a preview.jpg
"""
def __init__(self, id, title):
... and so on ...
... and so forth ...
security.declareProtected( view, 'index_html' )
index_html = PageTemplateFile(_www+'/thingy_index.zpt', \
globals())
... and at the end of the file, ...
... outside the class definition, after the ...
... manage_add and manage_addForm methods, comes this:
setattr( \
mythingy, \
"preview.jpg", \
PageTemplateFile(_www+'/zoom_index_html.zpt', globals()))
The magic part is the setattr, outside the class definition. This adds the attribute "preview.jpg" to the class. The attribute is the same for all instances of this class. That is fine with me, it's a ZPT, so it adjusts to whatever the instance has to show.
In the "setattr" line, the parameters might need some explaining (they needed for me :-). mythingy is the name of the class, naked without quotes. "preview.jpg" is the name that the "faked" method should be callable by, this one is a string, it has quotes around it. The last one (PageTemplateFile...) is the object that will be called. If I had a method called "gogo_method" on my class, I could as well have said:
setattr(mythingy, "preview.jpg", mythingy.gogo_method)and the gogo_method would be called anytime someone requested preview.jpg. In my example above I could have done the same with mythingy.index_html to make it shorter.