[Checkins] SVN: grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/ Re-implement the grokcore directives using the new-style directives.

Jan-Wijbrand Kolman janwijbrand at gmail.com
Fri May 2 16:15:19 EDT 2008


Log message for revision 86132:
  Re-implement the grokcore directives using the new-style directives.

Changed:
  U   grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/directive.py
  U   grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/meta.py
  U   grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/adapter/classorinterface.py
  U   grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/directive/multipletimes.py

-=-
Modified: grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/directive.py
===================================================================
--- grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/directive.py	2008-05-02 20:13:20 UTC (rev 86131)
+++ grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/directive.py	2008-05-02 20:15:19 UTC (rev 86132)
@@ -29,20 +29,19 @@
                                ClassDirectiveContext,
                                ClassOrModuleDirectiveContext)
 from martian import util
-
 from martian import ndir
+from martian.ndir import baseclass
 
-class GlobalUtilityDirective(MultipleTimesDirective):
-    def check_arguments(self, factory, provides=None, name=u'',
-                        direct=False):
+class global_utility(ndir.MultipleTimesDirective):
+    scope = ndir.MODULE
+
+    def factory(self, factory, provides=None, name=u'', direct=False):
         if provides is not None and not IInterface.providedBy(provides):
-            raise GrokImportError("You can only pass an interface to the "
-                                  "provides argument of %s." % self.name)
+            raise GrokImportError(
+                "You can only pass an interface to the "
+                "provides argument of %s." % self.name)
+        return GlobalUtilityInfo(factory, provides, name, direct)
 
-    def value_factory(self, *args, **kw):
-        return GlobalUtilityInfo(*args, **kw)
-
-
 class GlobalUtilityInfo(object):
     def __init__(self, factory, provides=None, name=u'', direct=None):
         self.factory = factory
@@ -58,42 +57,40 @@
             name = util.class_annotation(factory, 'grok.name', u'')
         self.name = name
 
+class order(ndir.Directive):
+    scope = ndir.CLASS
+    store = ndir.ONCE
 
-class MultiValueOnceDirective(OnceDirective):
+    _order = 0
 
-    def check_arguments(self, *values):
-        pass
+    def factory(self, value=None):
+        order._order += 1
+        if value is not None:
+            return value, order._order
+        return super(order, self).factory(value)
 
-    def value_factory(self, *args):
-        return args
+    def default_value(self, component):
+        return 0, order._order
 
-class OrderDirective(OptionalValueDirective, OnceDirective):
+class name(ndir.Directive):
+    scope = ndir.CLASS
+    store = ndir.ONCE
+    validate = ndir.validateText
 
-    order = 0
+class context(ndir.Directive):
+    scope = ndir.CLASS_OR_MODULE
+    store = ndir.ONCE
+    validate = ndir.validateInterfaceOrClass
 
-    def value_factory(self, value=None):
-        OrderDirective.order += 1
-        if value is not None:
-            return value, OrderDirective.order
-        return super(OrderDirective, self).value_factory(value)
+class title(ndir.Directive):
+    scope = ndir.CLASS
+    store = ndir.ONCE
+    validate = ndir.validateText
 
-    def default_value(self):
-        return 0, OrderDirective.order
+class direct(ndir.MarkerDirective):
+    scope = ndir.CLASS
 
-# Define grok directives
-name = SingleTextDirective('grok.name', ClassDirectiveContext())
-context = InterfaceOrClassDirective('grok.context',
-                                    ClassOrModuleDirectiveContext())
-baseclass = MarkerDirective('grok.baseclass', ClassDirectiveContext())
-global_utility = GlobalUtilityDirective('grok.global_utility',
-                                        ModuleDirectiveContext())
-title = SingleTextDirective('grok.title', ClassDirectiveContext())
-order = OrderDirective('grok.order', ClassDirectiveContext())
-
-direct = ndir.Directive(
-    'grok', 'direct', ndir.CLASS, ndir.ONCE, default=False,
-    arg=ndir.NO_ARG)
-
-provides = ndir.Directive(
-    'grok', 'provides', ndir.CLASS, ndir.ONCE, default=None,
-    validate=ndir.validateInterface)
+class provides(ndir.Directive):
+    scope = ndir.CLASS
+    store = ndir.ONCE
+    validate = ndir.validateInterface

Modified: grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/meta.py
===================================================================
--- grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/meta.py	2008-05-02 20:13:20 UTC (rev 86131)
+++ grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/meta.py	2008-05-02 20:15:19 UTC (rev 86132)
@@ -151,7 +151,7 @@
 class GlobalUtilityDirectiveGrokker(martian.GlobalGrokker):
 
     def grok(self, name, module, module_info, config, **kw):
-        infos = module_info.getAnnotation('grok.global_utility', [])
+        infos = directive.global_utility.get(module)
 
         for info in infos:
             provides = info.provides

Modified: grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/adapter/classorinterface.py
===================================================================
--- grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/adapter/classorinterface.py	2008-05-02 20:13:20 UTC (rev 86131)
+++ grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/adapter/classorinterface.py	2008-05-02 20:15:19 UTC (rev 86132)
@@ -5,22 +5,22 @@
   >>> function_context()
   Traceback (most recent call last):
     ...
-  GrokImportError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: The 'context' directive can only be called with a class or an interface.
 
   >>> string_context()
   Traceback (most recent call last):
     ...
-  GrokImportError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: The 'context' directive can only be called with a class or an interface.
 
   >>> module_context()
   Traceback (most recent call last):
     ...
-  GrokImportError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: The 'context' directive can only be called with a class or an interface.
 
   >>> instance_context()
   Traceback (most recent call last):
     ...
-  GrokImportError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: The 'context' directive can only be called with a class or an interface.
 
 """
 import grokcore.component as grok

Modified: grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/directive/multipletimes.py
===================================================================
--- grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/directive/multipletimes.py	2008-05-02 20:13:20 UTC (rev 86131)
+++ grokcore.component/branches/jw-philipp-using-ndir-directives/src/grokcore/component/tests/directive/multipletimes.py	2008-05-02 20:15:19 UTC (rev 86132)
@@ -2,10 +2,11 @@
 Since grok.global_utility is a MultipleTimesDirective, there is a list of
 GlobalUtilityInfo objects annotated on the module.
 
+
   >>> from martian import scan
+  >>> import grokcore.component as grok
   >>> from grokcore.component.tests.directive import multipletimes
-  >>> module_info = scan.module_info_from_module(multipletimes)
-  >>> guis = module_info.getAnnotation('grok.global_utility', None)
+  >>> guis = grok.global_utility.get(multipletimes)
   >>> guis
   [<grokcore.component.directive.GlobalUtilityInfo object at 0x...>,
   <grokcore.component.directive.GlobalUtilityInfo object at 0x...>]
@@ -21,7 +22,7 @@
   True
   >>> guis[1].name
   u''
-  
+
 """
 import grokcore.component as grok
 from zope import interface



More information about the Checkins mailing list