Mercurial > hg > config
annotate python/require.py @ 767:35f8751c0930
it is very annoying to have ones overrides overridden; see also http://stackoverflow.com/questions/25381304/why-type-cd-on-mac-os-states-that-cd-is-a-function
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Thu, 28 Jan 2016 14:02:17 -0800 |
| parents | 691b508084f1 |
| children |
| rev | line source |
|---|---|
|
156
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 def require(url): |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 """ |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 import a module from the web |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 url should be like scheme://host.name/path/to/module.py |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 """ |
| 157 | 6 import imp |
|
156
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 import os |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 import tempfile |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 import urllib2 |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 contents = urllib2.urlopen(url).read() |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 filename = url.rsplit('/', 1)[-1] |
| 230 | 12 module = filename.rsplit('.', 1)[0] |
| 168 | 13 dest = tempfile.mkstemp(suffix='.py', prefix=module) |
|
156
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 f = file(dest, 'w') |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 f.write(contents) |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 f.close() |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 return imp.load_source(module, dest) |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 |
|
a9c3a32d8385
add a function for loading modules from the web
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 # TODO: make an equivalent method for a tarball |
