multiversion flag and auto requiring import hook

Discussion in 'Python' started by Andrea Crotti, Jan 19, 2012.

  1. I'm using the multiversion flag in setuptools (-m) to be able to run
    many different projects, without modifying the global environment.

    Then thanks to pkg_resources magic and setuptools I can get
    automatically everything loaded.

    Now the problem is that we want to be able to run tests. The best way
    would be to use python setup.py test, which would make everything needed
    for the tests available.

    The problem is that it's very slow to run and harder to integrate in
    Eclipse, so I created an import hook, that tries to do a
    pkg_resources.require first.

    The test runner should do something like

    with AutoRequire():
    # run tests

    But now I'm fighting with nose and plugins, and I'm not sure this
    approach is really correct...

    Anyone has a better idea??

    Here is the import hook for the auto_import...

    --8<---------------cut here---------------start------------->8---
    class AutoRequireImport(object):
    """This require import automatically requires with pkg_resources
    all the modules that need to be imported.
    """

    def find_module(self, module_name, package=None):
    #TODO: try to filter to only the important modules
    mod_parent = module_name.split('.')[0]
    try:
    find_module(mod_parent)
    except ImportError:
    try:
    pkg_resources.require(module_name)
    except pkg_resources.DistributionNotFound:
    logger.error("not able to require the module %s, you
    need to run a related project or develop the egg with dev_main" %
    mod_parent)
    #TODO: raise the right error to be catched from
    somewhere else?
    else:
    logger.info("%s required correctly" % mod_parent)


    class AutoRequire(object):
    """Context manager to enable and disable the auto-require import hook
    """

    def __init__(self):
    self.ar = AutoRequireImport()

    def __enter__(self):
    logger.debug("enable the auto require import hook")
    sys.meta_path.append(self.ar)
    return self

    def __exit__(self, type, value, traceback):
    logger.debug("disable the auto require import hook")
    sys.meta_path.remove(self.ar)
    --8<---------------cut here---------------end--------------->8---
     
    Andrea Crotti, Jan 19, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noam Raphael
    Replies:
    0
    Views:
    285
    Noam Raphael
    Jun 21, 2004
  2. Torsten Mohr

    import hook, overwrite import?

    Torsten Mohr, Jan 26, 2005, in forum: Python
    Replies:
    2
    Views:
    455
    Steve Holden
    Jan 27, 2005
  3. Jeremy Sanders

    import hook

    Jeremy Sanders, Jun 11, 2006, in forum: Python
    Replies:
    4
    Views:
    399
    Jeremy Sanders
    Jun 18, 2006
  4. linkswanted
    Replies:
    1
    Views:
    991
  5. Zach Dennis

    Requiring in a Java-like Import Way

    Zach Dennis, Feb 1, 2005, in forum: Ruby
    Replies:
    3
    Views:
    111
    Zach Dennis
    Feb 1, 2005
Loading...

Share This Page