[Checkins] SVN: grok/trunk/martian/src/martian/ Remove the 'match' method - it turns out not to be very useful in a more

Martijn Faassen faassen at infrae.com
Tue May 1 14:47:48 EDT 2007


Log message for revision 74969:
  Remove the 'match' method - it turns out not to be very useful in a more
  optimized matching algorithm.
  

Changed:
  U   grok/trunk/martian/src/martian/README.txt
  U   grok/trunk/martian/src/martian/components.py
  U   grok/trunk/martian/src/martian/core.py
  U   grok/trunk/martian/src/martian/interfaces.py

-=-
Modified: grok/trunk/martian/src/martian/README.txt
===================================================================
--- grok/trunk/martian/src/martian/README.txt	2007-05-01 18:31:21 UTC (rev 74968)
+++ grok/trunk/martian/src/martian/README.txt	2007-05-01 18:47:47 UTC (rev 74969)
@@ -124,10 +124,10 @@
 ------------------
 
 In this section we define the concept of a ``Martian``. A ``Martian``
-is an object that can *grok* other objects - execute configuration
-actions pertaining to the other object, such as registering it with
-some central registry. Different kinds of Martians can grok different
-types of objects.
+is an object that can *grok* objects - execute configuration actions
+pertaining to the grokked object, such as registering it with some
+central registry. Different kinds of martians can grok different types
+of objects (instances, classes, functions).
 
 Let's define a Martian to help us register the file type handler
 functions as seen in our previous example::
@@ -137,20 +137,20 @@
   >>> from martian import InstanceMartian
   >>> class FileTypeMartian(InstanceMartian):
   ...   component_class = types.FunctionType 
-  ...   def match(self, name, obj):
-  ...     return (super(FileTypeMartian, self).match(name, obj) and 
-  ...             name.startswith('handle_'))
   ...
   ...   def grok(self, name, obj, **kw):
-  ...       ext = name.split('_')[1]
-  ...       filehandler.extension_handlers['.' + ext] = obj
+  ...     if not name.startswith('handle_'):
+  ...       return
+  ...     ext = name.split('_')[1]
+  ...     filehandler.extension_handlers['.' + ext] = obj
 
-
 This ``InstanceMartian`` allows us to grok instances of a particular
 type (such as functions). We need to define the type of object we're
-looking for with the ``component_class`` attribute. In addition, we've
-amended the ``match`` method to make sure we only match those
-instances that have a name that starts with ``handle_`.
+looking for with the ``component_class`` attribute. In the ``grok``
+method, we first make sure we only grok functions that have a name
+that starts with ``handle_``. Then we determine the used extension
+from the name and register the funcion in the ``extension_handlers``
+dictionary of the ``filehandler`` module.
 
 An instance will provide the IMartian interface::
 
@@ -159,37 +159,10 @@
   >>> IMartian.providedBy(filetype_martian)
   True
 
-The martian will match function objects that have a name that starts
-with ``'handle_'``::
-
-  >>> filetype_martian.match('handle_txt', filehandler.handle_txt)
-  True
-  >>> filetype_martian.match('handle_xml', filehandler.handle_xml)
-  True
-  >>> filetype_martian.match('handle_png', pnghandler.handle_png)
-  True
-
-It won't match ``handle``, as that does not start with ``handle_``::
-
-  >>> filetype_martian.match('handle', filehandler.handle)
-  False
-
-It also won't match non-function objects that happen to be prefixed
-with ``handle_``::
-
-  >>> class handle_foo(object):
-  ...   pass
-  >>> filetype_martian.match('handle_foo', handle_foo)
-  False
-  >>> filetype_martian.match('handle_foo', handle_foo())
-  False
-
 Now let's use the martian to grok a new handle function::
 
   >>> def handle_jpg(filepath):
   ...   return "JPG file"
-  >>> filetype_martian.match('handle_jpg', handle_jpg)
-  True
   >>> filetype_martian.grok('handle_jpg', handle_jpg)
 
 After we grokked, we have have registered a handler for ``.jpg`` files
@@ -204,6 +177,15 @@
   >>> filehandler.handle('image2.jpg')
   'JPG file'
 
+If we try to grok a function that doesn't start with ``handle_`` in its
+name, nothing will happen::
+
+  >>> def something(filepath):
+  ...   return 'Something'
+  >>> filetype_martian.grok('something', something)
+  >>> 'something' in filehandler.extension_handlers
+  False
+
 Grokking a module
 -----------------
 
@@ -234,11 +216,6 @@
   ...     return "SVG file"
   >>> lotsofhandlers = fake_import(lotsofhandlers)
 
-Our module martian matches this module::
-
-  >>> module_martian.match('lotsofhandlers', lotsofhandlers)
-  True
-
 Let's grok it::
 
   >>> module_martian.grok('lotsofhandlers', lotsofhandlers)
@@ -312,19 +289,11 @@
   ...   def grok(self, name, obj):
   ...       color.all_colors[name] = obj
 
-An ``InstanceMartian`` matches only instances of the given
-``component_class``. Let's try that::
 
+Let's create ``color_martian`` and grok a color::
+
   >>> color_martian = ColorMartian()
   >>> black = color.Color(0, 0, 0) # we DO consider black as a color :)
-  >>> color_martian.match('black', black)
-  True
-  >>> not_a_color = object()
-  >>> color_martian.match('foo', not_a_color)
-  False
-
-Now let's grok the color::
-
   >>> color_martian.grok('black', black)
 
 It ends up in the ``all_colors`` dictionary::

Modified: grok/trunk/martian/src/martian/components.py
===================================================================
--- grok/trunk/martian/src/martian/components.py	2007-05-01 18:31:21 UTC (rev 74968)
+++ grok/trunk/martian/src/martian/components.py	2007-05-01 18:47:47 UTC (rev 74969)
@@ -21,13 +21,7 @@
 
 class MartianBase(object):
     implements(IMartian)
-    
-    priority = 0
-    continue_scanning = False
 
-    def match(self, name, obj):
-        raise NotImplementedError
-
     def grok(self, name, obj, **kw):
         raise NotImplementedError
 
@@ -36,10 +30,6 @@
     """Martian that groks once per module.
     """
 
-    def match(self, name, obj):
-        # we never match with any object
-        return False
-
     def grok(self, name, obj, **kw):
         raise NotImplementedError
     
@@ -56,12 +46,9 @@
 class ClassMartian(ComponentMartianBase):
     """Martian that groks classes in a module.
     """
-    def match(self, name, obj):
-        return util.check_subclass(obj, self.component_class)
+    pass
 
-
 class InstanceMartian(ComponentMartianBase):
     """Martian that groks instances in a module.
     """
-    def match(self, name, obj):
-        return isinstance(obj, self.component_class)
+    pass

Modified: grok/trunk/martian/src/martian/core.py
===================================================================
--- grok/trunk/martian/src/martian/core.py	2007-05-01 18:31:21 UTC (rev 74968)
+++ grok/trunk/martian/src/martian/core.py	2007-05-01 18:47:47 UTC (rev 74969)
@@ -15,10 +15,7 @@
 
     def __init__(self, martian):
         self._martian = martian
-
-    def match(self, name, module):
-        return isinstance(module, types.ModuleType)
-    
+   
     def grok(self, name, module, **kw):
         martian = self._martian
         
@@ -34,8 +31,6 @@
                 continue
             if is_baseclass(name, obj):
                 continue
-            if not martian.match(name, obj):
-                continue
             martian.grok(name, obj, **kw)
 
 class MultiMartianBase(components.MartianBase):
@@ -50,13 +45,6 @@
         if martian not in martians:
             martians.append(martian)
     
-    def match(self, name, obj):
-        for martians in self._martians.values():
-            for martian in martians:
-                if martian.match(name, obj):
-                    return True
-        return False
-
     def grok(self, name, obj, **kw):
         used_martians = set()
         for base in self.get_bases(obj):
@@ -73,13 +61,23 @@
         # XXX how to work with old-style classes?
         return obj.__class__.__mro__
 
+class MultiOldStyleInstanceMartian(MultiMartianBase):
+    # XXX to be written
+    pass
+
 class MultiClassMartian(MultiMartianBase):
     def get_bases(self, obj):
         # XXX how to work with old-style classes?
         return obj.__mro__
 
+class MultiOldStyleClassMartian(MultiMartianBase):
+    # XXX to be written
+    pass
+
 class MultiMartian(components.MartianBase):
     implements(IMultiMartian)
+
+    # XXX extend with old-style class support
     
     def __init__(self):
         self._multi_instance_martian = MultiInstanceMartian()

Modified: grok/trunk/martian/src/martian/interfaces.py
===================================================================
--- grok/trunk/martian/src/martian/interfaces.py	2007-05-01 18:31:21 UTC (rev 74968)
+++ grok/trunk/martian/src/martian/interfaces.py	2007-05-01 18:47:47 UTC (rev 74969)
@@ -15,22 +15,14 @@
 from zope.interface import Interface, Attribute
 
 class IMartian(Interface):
-    priority = Attribute('Integer indicating relative priority')
-    continue_scanning = Attribute('If true, continue scanning after match')
-
-    def match(name, obj):
-        """Returns True if this Martian can grok obj.
-
-        name - name of object (in module)
-        obj - object to potentially grok
-        """
-
     def grok(name, obj, **kw):
         """Grok obj.
 
         name - name of object (in module)
         obj - object to grok
         **kw - optional parameters passed along the grokking process.
+
+        May do extra filtering based on name or obj.
         """
 
 class IComponentMartian(IMartian):



More information about the Checkins mailing list