Created
May 11, 2009 06:40
-
-
Save csytan/109891 to your computer and use it in GitHub Desktop.
webimport.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import imp | |
import urllib2 | |
PATHS = [ | |
"http://www.crummy.com/software/BeautifulSoup/download/", | |
] | |
for path in PATHS: | |
sys.path.append(path) | |
class WebImporter: | |
def __init__(self, path): | |
if path.startswith("http://"): | |
self.path = path | |
else: | |
raise ImportError | |
def find_module(self, fullname): | |
fullname = fullname.replace('.', '/') | |
urls = [self.path + fullname + ".py", self.path + fullname + "/__init__.py"] | |
for url in urls: | |
try: | |
source = urllib2.urlopen(url).read() | |
except urllib2.HTTPError: | |
pass | |
else: | |
self.source = source.replace("\r\n", "\n") | |
self.url = url | |
return self | |
def load_module(self, fullname): | |
mod = sys.modules.setdefault(fullname, imp.new_module(fullname)) | |
mod.__file__ = self.url | |
if self.url.endswith("__init__.py"): | |
mod.__path__ = [self.path] | |
exec self.source in mod.__dict__ | |
return mod | |
sys.path_hooks.append(WebImporter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment