src/pyams_utils/attr.py
changeset 289 c8e21d7dd685
child 292 b338586588ad
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/attr.py	Wed Dec 05 12:45:56 2018 +0100
@@ -0,0 +1,43 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# 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.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from zope.traversing.interfaces import ITraversable
+
+# import packages
+from pyams_utils.adapter import ContextAdapter, adapter_config
+from pyramid.exceptions import NotFound
+from zope.interface import Interface
+
+
+@adapter_config(name='attr', context=Interface, provides=ITraversable)
+class AttributeTraverser(ContextAdapter):
+    """++attr++ namespace traverser
+
+    This custom traversing adapter can be used to access an object attribute directly from
+    an URL by using a path like this::
+
+    /path/to/object/++attr++name
+
+    Whare *name* is the name of the requested attribute
+    """
+
+    def traverse(self, name, furtherpath=None):
+        try:
+            return getattr(self.context, name)
+        except AttributeError:
+            raise NotFound