annotate python/require.py @ 694:ebca6d85213a

File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module> from .application import * File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module> from IPython.config.configurable import SingletonConfigurable File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module> from IPython.utils.text import indent, wrap_paragraphs File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module> from IPython.external.path import path File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module> from path import * File "/home/jhammel/python/path.py", line 25 print root(path) ^
author Jeff Hammel <k0scist@gmail.com>
date Wed, 09 Jul 2014 16:26:49 -0700
parents 691b508084f1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
Jeff Hammel <jhammel@mozilla.com>
parents: 156
diff changeset
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
691b508084f1 fix module
Jeff Hammel <jhammel@mozilla.com>
parents: 168
diff changeset
12 module = filename.rsplit('.', 1)[0]
168
09c748a71b1b handle overlapping files
Jeff Hammel <jhammel@mozilla.com>
parents: 157
diff changeset
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