[Checkins] SVN: grokcore.component/branches/1.x/ Integrate the order directive from grokcore.viewlet.

Sylvain Viollon sylvain at infrae.com
Mon Nov 1 11:21:27 EDT 2010


Log message for revision 118052:
  Integrate the order directive from grokcore.viewlet.
  

Changed:
  U   grokcore.component/branches/1.x/CHANGES.txt
  U   grokcore.component/branches/1.x/src/grokcore/component/__init__.py
  U   grokcore.component/branches/1.x/src/grokcore/component/directive.py
  U   grokcore.component/branches/1.x/src/grokcore/component/interfaces.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/arg_orderdirective.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/combined_orderdirective.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/combinednoorder_orderdirective.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter1.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter2.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/noarg_orderdirective.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/order/nodirective.py
  U   grokcore.component/branches/1.x/src/grokcore/component/tests/test_grok.py
  A   grokcore.component/branches/1.x/src/grokcore/component/util.py

-=-
Modified: grokcore.component/branches/1.x/CHANGES.txt
===================================================================
--- grokcore.component/branches/1.x/CHANGES.txt	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/CHANGES.txt	2010-11-01 15:21:26 UTC (rev 118052)
@@ -4,7 +4,7 @@
 1.10 (unreleased)
 -----------------
 
-* ...
+* Move the order directive from the package grokcore.viewlet into this one.
 
 1.9 (2010-07-15)
 ----------------

Modified: grokcore.component/branches/1.x/src/grokcore/component/__init__.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/__init__.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/__init__.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -18,12 +18,15 @@
 from zope.component import adapts
 
 from martian import ClassGrokker, InstanceGrokker, GlobalGrokker
-from grokcore.component.components import Adapter, GlobalUtility, MultiAdapter, Context
+from grokcore.component.components import (
+    Adapter, GlobalUtility, MultiAdapter, Context)
 
 from martian import baseclass
 from grokcore.component.directive import (
-    context, name, title, description, provides, global_utility, global_adapter, direct)
-from grokcore.component.decorators import subscribe, adapter, implementer, provider
+    context, name, title, description, provides, global_utility,
+    global_adapter, direct, order)
+from grokcore.component.decorators import (
+    subscribe, adapter, implementer, provider)
 from martian.error import GrokError, GrokImportError
 
 # Import this module so that it's available as soon as you import the

Modified: grokcore.component/branches/1.x/src/grokcore/component/directive.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/directive.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/directive.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -19,6 +19,7 @@
 from martian.error import GrokImportError
 from grokcore.component.scan import UnambiguousComponentScope
 
+
 class global_utility(martian.MultipleTimesDirective):
     scope = martian.MODULE
 
@@ -36,6 +37,7 @@
             name = grokcore.component.name.bind().get(factory)
         return (factory, provides, name, direct)
 
+
 class global_adapter(martian.MultipleTimesDirective):
     scope = martian.MODULE
 
@@ -47,45 +49,63 @@
 
         if provides is None:
             provides = grokcore.component.provides.bind().get(factory)
-        
+
         if adapts is None:
             adapts = getattr(factory, '__component_adapts__', None)
             if adapts is None:
                 adapts = grokcore.component.context.bind().get(factory)
-        
+
         if not isinstance(adapts, (list, tuple,)):
             adapts = (adapts,)
         elif isinstance(adapts, list):
             adapts = tuple(adapts)
-        
+
         if not name:
             name = grokcore.component.name.bind().get(factory)
-        
+
         return (factory, adapts, provides, name)
 
+
 class name(martian.Directive):
     scope = martian.CLASS
     store = martian.ONCE
     default = u''
     validate = martian.validateText
 
+
 class context(martian.Directive):
     scope = UnambiguousComponentScope('context')
     store = martian.ONCE
     validate = martian.validateInterfaceOrClass
 
+
 class title(martian.Directive):
     scope = martian.CLASS
     store = martian.ONCE
     validate = martian.validateText
 
+
 class description(title):
     pass
 
+
 class direct(martian.MarkerDirective):
     scope = martian.CLASS
 
+
 class provides(martian.Directive):
     scope = martian.CLASS
     store = martian.ONCE
-    validate = martian.validateInterface
\ No newline at end of file
+    validate = martian.validateInterface
+
+
+class order(martian.Directive):
+    scope = martian.CLASS
+    store = martian.ONCE
+    default = 0, 0
+
+    _order = 0
+
+    def factory(self, value=0):
+        order._order += 1
+        return value, order._order

Modified: grokcore.component/branches/1.x/src/grokcore/component/interfaces.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/interfaces.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/interfaces.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -40,7 +40,7 @@
     GlobalGrokker = Attribute("Base class to define a module grokker.")
 
     Context = Attribute("Base class for automatically associated contexts.")
- 
+
     Adapter = Attribute("Base class for adapters.")
     MultiAdapter = Attribute("Base class for multi-adapters.")
     GlobalUtility = Attribute("Base class for global utilities.")
@@ -57,13 +57,13 @@
 
     def implements(*interfaces):
         """Declare that a class implements the given interfaces."""
-    
+
     def implementsOnly(*interfaces):
         """Declare that a class implements only the given interfaces.
-        
+
         Interfaces implemented by base classes are explicitly not inherited.
         """
-    
+
     def classProvides(*interfaces):
         """Declare that a class (as opposed to instances of the class)
         directly provides the given interfaces.
@@ -130,7 +130,26 @@
         utility, or an instance of it.
         """
 
+    def order(value=None):
+        """Control the ordering of components.
 
+        If the value is specified, the order will be determined by sorting on
+        it.
+        If no value is specified, the order will be determined by definition
+        order within the module.
+        If the directive is absent, the order will be determined by class name.
+        (unfortunately our preferred default behavior on absence which would
+        be like grok.order() without argument is hard to implement in Python)
+
+        Inter-module order is by dotted name of the module the
+        components are in; unless an explicit argument is specified to
+        ``grok.order()``, components are grouped by module.
+
+        The function grok.util.sort_components can be used to sort
+        components according to these rules.
+        """
+
+
 class IDecorators(Interface):
 
     def subscribe(*classes_or_interfaces):
@@ -146,7 +165,7 @@
         """Describes that a function that's used as an adapter
         implements an interface or a number of interfaces.
         """
-    
+
     def provider(*interfaces):
         """Describes that a function directly provides an interface or a
         number of interfaces.

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/arg_orderdirective.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/arg_orderdirective.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/arg_orderdirective.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -5,7 +5,7 @@
 
   >>> components = [First(), Second(), Third(), Fourth(), Fifth()]
 
-  >>> from grokcore.viewlet.util import sort_components
+  >>> from grokcore.component.util import sort_components
   >>> sort_components(components)
   [<...Fifth object at ...>,
    <...Fourth object at ...>,
@@ -15,7 +15,7 @@
 
 """
 
-import grokcore.viewlet as grok
+import grokcore.component as grok
 
 class First(object):
     grok.order(5)

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/combined_orderdirective.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/combined_orderdirective.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/combined_orderdirective.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -6,7 +6,7 @@
 
   >>> components = [First(), Second(), Third(), Fourth(), Fifth()]
 
-  >>> from grokcore.viewlet.util import sort_components
+  >>> from grokcore.component.util import sort_components
   >>> sort_components(components)
   [<...Third object at ...>,
    <...Fourth object at ...>,
@@ -16,7 +16,7 @@
 
 """
 
-import grokcore.viewlet as grok
+import grokcore.component as grok
 
 class First(object):
     grok.order(2)

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/combinednoorder_orderdirective.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/combinednoorder_orderdirective.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/combinednoorder_orderdirective.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -6,7 +6,7 @@
 
   >>> components = [First(), Second(), Third(), Fourth(), Fifth()]
 
-  >>> from grokcore.viewlet.util import sort_components
+  >>> from grokcore.component.util import sort_components
   >>> sort_components(components)
   [<...Fifth object at ...>,
    <...Third object at ...>,
@@ -16,7 +16,7 @@
 
 """
 
-import grokcore.viewlet as grok
+import grokcore.component as grok
 
 class First(object):
     grok.order()

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter1.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter1.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter1.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -12,7 +12,7 @@
   >>> from inter2 import Four, Five, Six
   >>> components = [One(), Two(), Three(), Four(), Five(), Six()]
 
-  >>> from grokcore.viewlet.util import sort_components
+  >>> from grokcore.component.util import sort_components
   >>> sort_components(components)
   [<...Three object at ...>,
    <...One object at ...>,
@@ -23,7 +23,7 @@
 
 """
 
-import grokcore.viewlet as grok
+import grokcore.component as grok
 
 class One(object):
     grok.order()

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter2.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter2.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/inter2.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -2,7 +2,7 @@
 This module used by inter1 tests
 """
 
-import grokcore.viewlet as grok
+import grokcore.component as grok
 
 class Four(object):
     grok.order(1)

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/noarg_orderdirective.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/noarg_orderdirective.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/noarg_orderdirective.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -5,7 +5,7 @@
 
   >>> components = [First(), Second(), Third(), Fourth(), Fifth()]
 
-  >>> from grokcore.viewlet.util import sort_components
+  >>> from grokcore.component.util import sort_components
   >>> sort_components(components)
   [<...First object at ...>,
    <...Second object at ...>,
@@ -15,7 +15,7 @@
 
 """
 
-import grokcore.viewlet as grok
+import grokcore.component as grok
 
 class First(object):
     grok.order()

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/order/nodirective.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/order/nodirective.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/order/nodirective.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -5,7 +5,7 @@
 
   >>> components = [First(), Second(), Third(), Fourth(), Fifth()]
 
-  >>> from grokcore.viewlet.util import sort_components
+  >>> from grokcore.component.util import sort_components
   >>> sort_components(components)
   [<...Fifth object at ...>,
    <...First object at ...>,

Modified: grokcore.component/branches/1.x/src/grokcore/component/tests/test_grok.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/tests/test_grok.py	2010-11-01 15:15:18 UTC (rev 118051)
+++ grokcore.component/branches/1.x/src/grokcore/component/tests/test_grok.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -54,7 +54,7 @@
 def test_suite():
     suite = unittest.TestSuite()
     for name in ['adapter', 'directive', 'grokker', 'utility', 'view',
-                 'event']:
+                 'event', 'order']:
         suite.addTest(suiteFromPackage(name))
 
     api = doctest.DocFileSuite('api.txt')

Added: grokcore.component/branches/1.x/src/grokcore/component/util.py
===================================================================
--- grokcore.component/branches/1.x/src/grokcore/component/util.py	                        (rev 0)
+++ grokcore.component/branches/1.x/src/grokcore/component/util.py	2010-11-01 15:21:26 UTC (rev 118052)
@@ -0,0 +1,29 @@
+##############################################################################
+#
+# Copyright (c) 2006-2007 Zope Foundation 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.
+#
+##############################################################################
+"""Grok utility functions.
+"""
+from grokcore.component import directive
+
+
+def _sort_key(component):
+    # If components have a grok.order directive, sort by that.
+    explicit_order, implicit_order = directive.order.bind().get(component)
+    return (explicit_order,
+            component.__module__,
+            implicit_order,
+            component.__class__.__name__)
+
+
+def sort_components(components):
+    return sorted(components, key=_sort_key)



More information about the checkins mailing list