Mercurial > hg > config
comparison 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 |
comparison
equal
deleted
inserted
replaced
155:f7cfd58eafe6 | 156:a9c3a32d8385 |
---|---|
1 def require(url): | |
2 """ | |
3 import a module from the web | |
4 url should be like scheme://host.name/path/to/module.py | |
5 """ | |
6 import os | |
7 import tempfile | |
8 import urllib2 | |
9 contents = urllib2.urlopen(url).read() | |
10 filename = url.rsplit('/', 1)[-1] | |
11 module = filename.rsplit('.', 1)[-1] | |
12 dest = os.path.join(tempfile, filename) | |
13 f = file(dest, 'w') | |
14 f.write(contents) | |
15 f.close() | |
16 return imp.load_source(module, dest) | |
17 | |
18 # TODO: make an equivalent method for a tarball |