/usr/share/pyshared/zope/browsermenu/field.py is in python-zope.browsermenu 3.9.1-0ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Menu field
$Id: field.py 103270 2009-08-27 13:56:02Z nadako $
"""
__docformat__ = 'restructuredtext'
from zope.component import queryUtility
from zope.component.interfaces import ComponentLookupError
from zope.configuration.exceptions import ConfigurationError
from zope.configuration.fields import GlobalObject
from zope.schema import ValidationError
from zope.browsermenu.interfaces import IMenuItemType
class MenuField(GlobalObject):
r"""This fields represents a menu (item type).
Besides being able to look up the menu by importing it, we also try
to look up the name in the site manager.
>>> from zope.interface import directlyProvides
>>> from zope.interface.interface import InterfaceClass
>>> menu1 = InterfaceClass('menu1', (),
... __doc__='Menu Item Type: menu1',
... __module__='zope.app.menus')
>>> directlyProvides(menu1, IMenuItemType)
>>> menus = None
>>> class Resolver(object):
... def resolve(self, path):
... if path.startswith('zope.app.menus') and \
... hasattr(menus, 'menu1') or \
... path == 'zope.browsermenu.menus.menu1':
... return menu1
... raise ConfigurationError('menu1')
>>> field = MenuField()
>>> field = field.bind(Resolver())
Test 1: Import the menu
-----------------------
>>> field.fromUnicode('zope.browsermenu.menus.menu1') is menu1
True
Test 2: We have a shortcut name. Import the menu from `zope.app.menus1`.
------------------------------------------------------------------------
>>> from types import ModuleType as module
>>> import sys
>>> menus = module('menus')
>>> old = sys.modules.get('zope.app.menus', None)
>>> sys.modules['zope.app.menus'] = menus
>>> setattr(menus, 'menu1', menu1)
>>> field.fromUnicode('menu1') is menu1
True
>>> if old is not None:
... sys.modules['zope.app.menus'] = old
Test 3: Get the menu from the Site Manager
------------------------------------------
>>> from zope.component import provideUtility
>>> provideUtility(menu1, IMenuItemType, 'menu1')
>>> field.fromUnicode('menu1') is menu1
True
"""
def fromUnicode(self, u):
name = str(u.strip())
try:
value = queryUtility(IMenuItemType, name)
except ComponentLookupError:
# The component architecture is not up and running.
pass
else:
if value is not None:
self.validate(value)
return value
try:
value = self.context.resolve('zope.app.menus.'+name)
except ConfigurationError, v:
try:
value = self.context.resolve(name)
except ConfigurationError, v:
raise ValidationError(v)
self.validate(value)
return value
|