Commits

Anonymous committed b048a1c

Implemented the ExposeItems utility class. It was originally proposed as a workaround for #65. It allows to publish generic getitem-aware objects (such as lists and dicts) in such a way as not to mess up with the object mapping algorithm.

Comments (0)

Files changed (1)

+"""
+CherryPy Standard Library
+"""
+
+class ExposeItems:
+    """
+    Utility class that exposes a getitem-aware object. It does not provide
+    index() or default() methods, and it does not expose the individual item
+    objects - just the list or dict that contains them. User-specific index()
+    and default() methods can be implemented by inheriting from this class.
+    
+    Use case:
+    
+    from cherrypy.lib import ExposeItems
+    ...
+    cpg.root.foo = ExposeItems(mylist)
+    cpg.root.bar = ExposeItems(mydict)
+    """
+    exposed = True
+    def __init__(self, items):
+        self.items = items
+    def __getattr__(self, key):
+        return self.items[key]