changeset 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 f7cfd58eafe6
children f145d7b1fbf7
files python/require.py
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/require.py	Wed Jul 27 08:43:34 2011 -0700
@@ -0,0 +1,18 @@
+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