Skip to content

Instantly share code, notes, and snippets.

@toutpt
Created November 18, 2013 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toutpt/7526354 to your computer and use it in GitHub Desktop.
Save toutpt/7526354 to your computer and use it in GitHub Desktop.
def getToolByName(obj, name, default=_marker):
""" Get the tool, 'toolname', by acquiring it.
o Application code should use this method, rather than simply
acquiring the tool by name, to ease forward migration (e.g.,
to Zope3).
"""
tool_interface = _tool_interface_registry.get(name)
if tool_interface is not None:
try:
utility = getUtility(tool_interface)
# Site managers, except for five.localsitemanager, return unwrapped
# utilities. If the result is something which is acquisition-unaware
# but unwrapped we wrap it on the context.
if IAcquirer.providedBy(obj) and \
aq_parent(utility) is None and \
IAcquirer.providedBy(utility):
utility = utility.__of__(obj)
return utility
except ComponentLookupError:
# behave in backwards-compatible way
# fall through to old implementation
pass
try:
tool = aq_get(obj, name, default, 1)
except AttributeError:
if default is _marker:
raise
return default
else:
if tool is _marker:
raise AttributeError, name
return tool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment