[Checkins] SVN: zc.extrinsicreference/trunk/src/zc/extrinsicreference/extrinsicreference.py make defining some functions conditional on being able to import the module

Benji York benji at zope.com
Tue May 30 17:13:06 EDT 2006


Log message for revision 68401:
  make defining some functions conditional on being able to import the module
  that they depend on (this would be a good place to use eggs instead of catching
  import errors)
  

Changed:
  U   zc.extrinsicreference/trunk/src/zc/extrinsicreference/extrinsicreference.py

-=-
Modified: zc.extrinsicreference/trunk/src/zc/extrinsicreference/extrinsicreference.py
===================================================================
--- zc.extrinsicreference/trunk/src/zc/extrinsicreference/extrinsicreference.py	2006-05-30 17:52:35 UTC (rev 68400)
+++ zc.extrinsicreference/trunk/src/zc/extrinsicreference/extrinsicreference.py	2006-05-30 21:13:05 UTC (rev 68401)
@@ -15,14 +15,13 @@
 
 $Id$
 """
-import persistent
 from BTrees import OOBTree
-
+from zc.extrinsicreference.interfaces import IExtrinsicReferences
 from zope import interface
+from zope.app import zapi
 from zope.app.keyreference.interfaces import IKeyReference
+import persistent
 
-from zc.extrinsicreference.interfaces import IExtrinsicReferences
-
 class ExtrinsicReferences(persistent.Persistent):
 
     interface.implements(IExtrinsicReferences)
@@ -89,20 +88,22 @@
         for kr in refs:
             yield kr()
 
-# TODO these belong elsewhere (zc.shortcut, maybe?)
-from zope.app import zapi
-from zc.shortcut.interfaces import IShortcut
+try:
+    from zc.shortcut.interfaces import IShortcut
 
-def registerShortcut(shortcut, event):
-    """Subscriber to add an extrinsic reference."""
-    registry = zapi.queryUtility(IExtrinsicReferences, 'shortcuts')
-    if registry is not None:
-        # We use raw_target because we don't want a proxy.
-        registry.add(shortcut.raw_target, shortcut)
+    def registerShortcut(shortcut, event):
+        """Subscriber to add an extrinsic reference."""
+        registry = zapi.queryUtility(IExtrinsicReferences, 'shortcuts')
+        if registry is not None:
+            # We use raw_target because we don't want a proxy.
+            registry.add(shortcut.raw_target, shortcut)
 
-def unregisterShortcut(shortcut, event):
-    """Subscriber to remove an extrinsic reference."""
-    registry = zapi.queryUtility(IExtrinsicReferences, 'shortcuts')
-    if registry is not None:
-        # We use raw_target because we don't want a proxy.
-        registry.discard(shortcut.raw_target, shortcut)
+    def unregisterShortcut(shortcut, event):
+        """Subscriber to remove an extrinsic reference."""
+        registry = zapi.queryUtility(IExtrinsicReferences, 'shortcuts')
+        if registry is not None:
+            # We use raw_target because we don't want a proxy.
+            registry.discard(shortcut.raw_target, shortcut)
+except ImportError:
+    # apparently zc.shortcut isn't around, skip it
+    pass



More information about the Checkins mailing list