Created
November 18, 2013 11:31
-
-
Save toutpt/7526354 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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