src/pyams_utils/attr.py
changeset 1 3f89629b9e54
child 72 9049384a2bd4
equal deleted inserted replaced
0:16d47bd81d84 1:3f89629b9e54
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from zope.traversing.interfaces import ITraversable
       
    20 
       
    21 # import packages
       
    22 from pyams_utils.adapter import ContextAdapter, adapter_config
       
    23 from pyramid.exceptions import NotFound
       
    24 from zope.interface import Interface
       
    25 
       
    26 
       
    27 @adapter_config(name='attr', context=Interface, provides=ITraversable)
       
    28 class AttributeTraverser(ContextAdapter):
       
    29     """++attr++ namespace traverser"""
       
    30 
       
    31     def traverse(self, name, furtherpath=None):
       
    32         try:
       
    33             return getattr(self.context, name)
       
    34         except AttributeError:
       
    35             raise NotFound