[Grok-dev] manuel plugin for Martian's FakeModule?

Paul Wilson paulalexwilson at gmail.com
Thu Oct 22 07:35:29 EDT 2009


Hi,

2009/10/7 Martijn Faassen <faassen at startifact.com>:
> It looks to me like the FakeModule bit could make a very nice Manuel
> extension.
>
> The FakeModule support + manuel integration could be in a separate
> library, but since this seems to be of general utility better yet it
> seems to me would be integrating this into Manuel proper.
>
> I hope an interested hacker wants to work on this!

I've written some code and tests in an attempt to interpret what
you're meaning here. Perhaps it could form the basis of a solution.
The tests pass from within the manuel package but I haven't actually
tried it in a doc test yet (need time!!!). I've gone ahead and used
the format:

 .. module-block:: module_name
      import xyz

      class ABC: pass

Let me know if I'm close to the mark, and I'll branch manuel or something.

Hope this gets the ball rolling...

Thanks,
Paul
-------------- next part --------------
manuel.fake_modules
===================

This document discusses the fake_module plugin for manuel that allows 
doctests to define and then use modules. 

   >>> import manuel
   >>> import manuel.fake_module

   >>> source = """\
   ... This is a module doc-test:
   ...
   ... .. module-block:: fake_module
   ...      import whatever
   ...      class Something:
   ...         pass
   ...
   ... As you can see, it does much!!
   ... """
   
   >>> doc = manuel.Document(source)
   >>> manuel.fake_module.find_fakes(doc)

Let's extract the module name and its source:

   >>> for region in doc:
   ...    if region.parsed:
   ...       print region.parsed.module_name
   ...       print region.source
   fake_module
   .. module-block:: fake_module
        import whatever
        class Something:
           pass
   <BLANKLINE>
   <BLANKLINE>

The code can correctly parse multiple modules:

   >>> source = """\
   ... Our system has a few modules:
   ...
   ... .. module-block:: some_module
   ...      class Foo:
   ...         pass
   ...
   ... and:
   ... 
   ... .. module-block:: an_other_module
   ...      class Bar:
   ...          pass
   ...
   ... fin
   ... """

   >>> doc = manuel.Document(source)
   >>> manuel.fake_module.find_fakes(doc)
   
Again, we extract the various parsed regions:

   >>> for region in doc:
   ...     if region.parsed:
   ...         print region.parsed.module_name
   ...         print region.source
   some_module
   .. module-block:: some_module
        class Foo:
           pass
   <BLANKLINE>
   <BLANKLINE>
   an_other_module
   .. module-block:: an_other_module
        class Bar:
            pass
   <BLANKLINE>
   <BLANKLINE>

Let's execute these regions into a globs dictionary of our own:

  >>> glob = {}
  >>> for region in doc:
  ...     if region.parsed:
  ...         manuel.fake_module.execute_into_module(
  ...                            region, doc, glob)

Let's check that our glob contains the modules with their names:

  >>> for name, module in glob.iteritems(): 
  ...     print module.__name__, ":", type(module)
  manueltest.fake.some_module : <type 'module'>
  manueltest.fake.an_other_module : <type 'module'>

Let's also confirm that the modules contain their respective class
definitions:

  >>> for name, module in glob.iteritems():
  ...     print dir(module)
  ['Foo', '__builtins__', '__doc__', '__file__', '__name__']
  ['Bar', '__builtins__', '__doc__', '__file__', '__name__']

We would also want to ensure that we can import the module correctly 
too. The fake_module system also adds the modules under their own 
namespace 'manueltest.fake', as you can see from the previous tests:

  >>> import manueltest.fake.some_module
  >>> import manueltest.fake.an_other_module
  >>> manueltest.fake.some_module.Foo
  <class manueltest.fake.some_module.Foo at ...>
  >>> manueltest.fake.an_other_module.Bar
  <class manueltest.fake.an_other_module.Bar at ...>

Note: There is no checking done to ensure that the module definition
hasn't overridden any previous modules defined in the doctest.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fake_module.py
Type: application/octet-stream
Size: 1986 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/grok-dev/attachments/20091022/0275a1c7/attachment.obj 


More information about the Grok-dev mailing list