durusmail: quixote-users: [patch] _q_import
[patch] _q_import
[patch] _q_import
2002-12-02
2002-12-03
2002-12-03
2002-12-04
2002-12-07
2002-12-07
2002-12-09
2002-12-09
2002-12-05
[patch] _q_import
Greg Ward
2002-12-03
On 02 December 2002, Jonathan Corbet said:
> Fortunately, it's all easy to fix.  The following little patch makes the
> publisher look for a function called _q_import when it thinks it's time to
> try importing something.

Hmmmm.  Here's an alternative patch that just refactors
Publisher.get_component() a bit (untested):

--- publish.py  20 Nov 2002 19:43:22 -0000      1.140
+++ publish.py  3 Dec 2002 15:20:08 -0000
@@ -424,6 +424,23 @@
         pipe.write(msg)
         pipe.close()

+    def _import_submodule (self, container, component):
+        """
+        Import a sub-module of 'container' named 'component'.
+        'container' is a module object, 'component' a name that is
+        supposed to be in the namespace of 'container' but isn't.
+        Presumably, 'component' names a module that has not yet been
+        imported, so this module does the import and returns the
+        resulting module object.
+        """
+        # If we get an ImportError here we don't catch it.  It means
+        # either 1) the _q_exports list in 'container' lists something
+        # that doesn't exist (can't be imported), or 2) something raised
+        # an exception at import-time.  A traceback should be generated
+        # in either case.
+        mod_name = container.__name__ + '.' + component
+        return _get_module(mod_name)
+
     def get_component (self, container, component, path, request):
         """Get one component of a path from a namespace.
         """
@@ -445,13 +462,7 @@
             is_module = type(container) is types.ModuleType
             has_component = hasattr(container, component)
             if is_module and not has_component and component != "_q_index":
-                # get next module
-                mod_name = container.__name__ + '.' + component
-                # If we get an ImportError here we don't catch it.  It means
-                # that either someone exported something that doesn't exist or
-                # an exception was raised from deeping in the code.  A
-                # traceback should be generated in either case.
-                object = _get_module(mod_name)
+                object = self._import_submodule(container, component)

             elif has_component:
                 object = getattr(container, component)


With this, you could just override _import_submodule() and come up with
sub-module names in your own way.  Or add support for _q_import without
the indentation going crazy.

> _q_import needs to return an object for the given
> name, just like _get_module does, or it should raise an exception.  A nice
> side benefit is that _q_import does not *have* to return a module; if it
> returns a callable object instead, Quixote is happy.

Hmmm, that sounds interesting.  Vaguely similar to the generalization of
_q_getname() that I did for 0.5.

> My _q_import also
> stashes the symbol into the container's dictionary so that it doesn't get
> called again for that particular case.

Isn't that done when a sub-module is imported?  Or has Quixote been
making a silly assumption all these years?  Better go check...

        Greg
--
Greg Ward - software developer                gward@mems-exchange.org
MEMS Exchange                            http://www.mems-exchange.org

reply