[Checkins] SVN: megrok.five/trunk/src/megrok/five/ We don't need a special Application object anymore (after grok.Application is

Philipp von Weitershausen philikon at philikon.de
Wed Feb 14 19:17:39 EST 2007


Log message for revision 72590:
  We don't need a special Application object anymore (after grok.Application is
  not a grok.Container anymore, but just a grok.Site).  We still want that
  ApplicationGrokker to register the thing as an object addable thru the ZMI, though.
  
  We now have a new base class, megrok.five.Container, which is an ObjectManager
  but also implements the IContainer interface. That way you can write Zope3-like
  code in Zope 2 much easier (who can actually remember the exact spelling of
  manage_delObjects(...)???)
  

Changed:
  U   megrok.five/trunk/src/megrok/five/__init__.py
  U   megrok.five/trunk/src/megrok/five/component.py
  U   megrok.five/trunk/src/megrok/five/grokker.py

-=-
Modified: megrok.five/trunk/src/megrok/five/__init__.py
===================================================================
--- megrok.five/trunk/src/megrok/five/__init__.py	2007-02-14 21:55:27 UTC (rev 72589)
+++ megrok.five/trunk/src/megrok/five/__init__.py	2007-02-15 00:17:38 UTC (rev 72590)
@@ -1 +1 @@
-from megrok.five.component import Model, Application
+from megrok.five.component import Model, Container

Modified: megrok.five/trunk/src/megrok/five/component.py
===================================================================
--- megrok.five/trunk/src/megrok/five/component.py	2007-02-14 21:55:27 UTC (rev 72589)
+++ megrok.five/trunk/src/megrok/five/component.py	2007-02-15 00:17:38 UTC (rev 72590)
@@ -1,9 +1,60 @@
 import grok
+import Acquisition
+from zope.interface import implements
+from zope.app.container.interfaces import IContainer
 from OFS.SimpleItem import SimpleItem
 from OFS.ObjectManager import ObjectManager
 
 class Model(SimpleItem, grok.Model):
-    pass
 
-class Application(ObjectManager, grok.Model):
-    pass
+    def __init__(self, id=None):
+        if id is not None:
+            self.id = id
+
+# This is a grok.Model only because it needs to be found as a possible
+# context for views, adapters, etc.
+class Container(ObjectManager, grok.Model):
+    implements(IContainer)
+
+    def __init__(self, id=None):
+        if id is not None:
+            self.id = id
+
+    # make Zope 2's absolute_url() happy
+    def getId(self):
+        return self.id
+
+    # fulfill IContainer interface
+
+    def keys(self):
+        return self.objectIds()
+
+    def values(self):
+        return self.objectValues()
+
+    def items(self):
+        return self.objectItems()
+
+    def get(self, name, default=None):
+        return getattr(self, name, default)
+
+    # __getitem__ is already implemented by ObjectManager
+
+    def __setitem__(self, name, obj):
+        name = str(name) # Zope 2 doesn't like unicode names
+        # TODO there should be a check here if 'name' contains
+        # non-ASCII unicode data. In this case I think we should just
+        # raise an error.
+        self._setObject(name, obj)
+
+    def __delitem__(self, name):
+        self.manage_delObjects([name])
+
+    def __contains__(self, name):
+        return self.hasObject(name)
+
+    def __iter__(self):
+        return iter(self.objectIds())
+
+    def __len__(self):
+        return len(self.objectIds())

Modified: megrok.five/trunk/src/megrok/five/grokker.py
===================================================================
--- megrok.five/trunk/src/megrok/five/grokker.py	2007-02-14 21:55:27 UTC (rev 72589)
+++ megrok.five/trunk/src/megrok/five/grokker.py	2007-02-15 00:17:38 UTC (rev 72590)
@@ -11,6 +11,7 @@
 import megrok.five
 
 class ViewGrokker(meta.ViewGrokker):
+
     priority = -1   # beat grok.meta.ViewGrokker
     # XXX this is weird, I should have to set a *higher* priority so
     # that I'm executed *before* grok.meta.ViewGrokker (which should
@@ -25,7 +26,7 @@
         old_factory = factory
         factory = type(factory.__name__, (Acquisition.Explicit, factory),
                        # deep Zope 2 traversal voodoo here (traversed
-                       # items need to have a __name__ property, apparently
+                       # items need to have a __name__ property, apparently)
                        {'__name__': property(lambda self: self.__view_name__)})
         factory.__module__ = module_info.dotted_name
 
@@ -47,8 +48,8 @@
 
 
 class ApplicationGrokker(grok.ClassGrokker):
-    component_class = megrok.five.Application
-    priority = 500
+    component_class = grok.Application
+    priority = 501
     continue_scanning = True
 
     def register(self, context, name, factory, module_info, templates):
@@ -65,7 +66,3 @@
                 'instance': factory,
                 'container_filter': None}
         Products.meta_types += (info,)
-
-        component.provideUtility(factory,
-                                 provides=grok.interfaces.IApplication,
-                                 name=full_name)



More information about the Checkins mailing list