Conditional import of annotations for Zope 2.9 and 2.10: how?
Hi, In Zope 2.9 (or 3.2) the annotations module is in zope.app.annotations. In Zope 2.10 (or 3.3) it is in zope.annotations. In a product for zope 2.9 I used to do this in zcml: <include package="zope.app.annotation" /> and this in code: from zope.app.annotation.interfaces import IAnnotations from zope.app.annotation.interfaces import IAttributeAnnotatable This of course gives problems in zope 2.10/3.3. The code part is easily fixable to run in all versions: try: from zope.app.annotation.interfaces import IAnnotations from zope.app.annotation.interfaces import IAttributeAnnotatable except: # Zope 2.10+/3.3+ have a different location for annotations from zope.annotation.interfaces import IAnnotations from zope.annotation.interfaces import IAttributeAnnotatable But how should I fix up the zcml? With the zcml above, zope 2.10 does not start. With this: <include package="zope.annotation" /> zope 2.9 does not start. That is to be expected. zcml:condition to the rescue: <!-- for zope 2.9 --> <include zcml:condition="installed zope.app.annotation" package="zope.app.annotation" /> <!-- for zope 2.10 --> <include zcml:condition="installed zope.annotation" package="zope.annotation" /> Zope 2.9 is fine with this. And the tests for my product that make use of annotations pass. But now zope 2.10 cannot start: zope.configuration.xmlconfig.ZopeXMLConfigurationError: File "/home/maurits/instances/plone3/etc/site.zcml", line 15.2-15.23 ZopeXMLConfigurationError: File "/home/maurits/instances/plone3/Products/eXtremeManagement/configure.zcml", line 7.2-7.31 ZopeXMLConfigurationError: File "/home/maurits/instances/plone3/Products/eXtremeManagement/actual/configure.zcml", line 6.2-7.43 IOError: [Errno 2] No such file or directory: '/opt/zope/zope2.10.3/lib/python/zope/app/annotation/configure.zcml' So I change the code so both zopes can start: <!-- for zope 2.9 --> <include zcml:condition="have zope.app.annotation" package="zope.app.annotation" /> <!-- for zope 2.10 --> <include zcml:condition="have zope.annotation" package="zope.annotation" /> Now zope 2.10 looks fine. Its tests fail but that is due to other reasons. Within a zopectl debug session I can adapt to IAnnotations. But now the tests in 2.9 fail because adapting does not work: TypeError: ('Could not adapt', <Task at >, <InterfaceClass zope.app.annotation.interfaces.IAnnotations>) So: neither the conditions 'have' nor 'installed' work for both zopes. What other options are there to do this in zcml? Or should I take my loss and translate the zope/(app/)annotations/configure.zcml file (which only registers one adapter) into python code? -- Maurits van Rees | http://maurits.vanrees.org/ [NL] Work | http://zestsoftware.nl/ "Do not worry about your difficulties in computers, I can assure you mine are still greater."
Maurits van Rees wrote:
Hi,
[..]
What other options are there to do this in zcml?
Or should I take my loss and translate the zope/(app/)annotations/configure.zcml file (which only registers one adapter) into python code?
More a workaround than a solution but have you tried doing it the "new way" (Zope-2.10) and introducing backwards compatibility via a module alias to make zope.annotations also available as zope.app.annotations? Raphael
Raphael Ritz, on 2007-05-14:
More a workaround than a solution but have you tried doing it the "new way" (Zope-2.10) and introducing backwards compatibility via a module alias to make zope.annotations also available as zope.app.annotations?
Ah, that might work yes. Hm, I had problems getting it to work, and I think I have it the other way around than what you said. The python code and the zcml only use zope.annotation now and I make sure that in 2.9 zope.app.annotation is available under the module name zope.annotation. And I need to do the same for zope.(app.)annotation.interfaces, otherwise problems still remain. So I now have the following in my __init__.py: try: import zope.annotation except ImportError: # BBB for Zope 2.9 import zope.app.annotation import zope.app.annotation.interfaces import sys sys.modules['zope.annotation'] = zope.app.annotation sys.modules['zope.annotation.interfaces'] = zope.app.annotation.interfaces Then it works. Thanks! If someone knows a better way to do this with some condition in zcml (not the ones that failed for me in the original message) then that would still be appreciated. BTW, for the python code a conditional import is not even really necessary: in zope 2.10 (or 3.3) you simply get a DeprecationWarning that zope.app.annotation will be removed in ope 3.5. But there is no such deprecation for the zcml file: it has simply been removed. Hm: for this reason it would be nice to still have the original zcml file in there. Then including that zcml would still work though you get a DeprecationWarning. Actually, that zcml file should then look like this: <configure xmlns="http://namespaces.zope.org/zope"> <include package="zope.annotation" /> </configure> That would work for me too: I just tried it. Should I propose this on the zope 3 dev list? I do not currently read that list btw. -- Maurits van Rees | http://maurits.vanrees.org/ [NL] Work | http://zestsoftware.nl/ "Do not worry about your difficulties in computers, I can assure you mine are still greater."
Maurits van Rees, on 2007-05-14:
Hm: for this reason it would be nice to still have the original zcml file in there. Then including that zcml would still work though you get a DeprecationWarning. Actually, that zcml file should then look like this:
<configure xmlns="http://namespaces.zope.org/zope">
<include package="zope.annotation" />
</configure>
That would work for me too: I just tried it. Should I propose this on the zope 3 dev list? I do not currently read that list btw.
I just reported it here: https://bugs.launchpad.net/zope3/+bug/115243 -- Maurits van Rees | http://maurits.vanrees.org/ [NL] Work | http://zestsoftware.nl/ "Do not worry about your difficulties in computers, I can assure you mine are still greater."
participants (2)
-
Maurits van Rees -
Raphael Ritz