view python/require.py @ 156:a9c3a32d8385

add a function for loading modules from the web
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 27 Jul 2011 08:43:34 -0700
parents
children f145d7b1fbf7
line wrap: on
line source

def require(url):
    """
    import a module from the web
    url should be like scheme://host.name/path/to/module.py
    """
    import os
    import tempfile
    import urllib2
    contents = urllib2.urlopen(url).read()
    filename = url.rsplit('/', 1)[-1]
    module = filename.rsplit('.', 1)[-1]
    dest = os.path.join(tempfile, filename)
    f = file(dest, 'w')
    f.write(contents)
    f.close()
    return imp.load_source(module, dest)

# TODO: make an equivalent method for a tarball