Created
June 24, 2011 19:56
-
-
Save saghul/1045544 to your computer and use it in GitHub Desktop.
Tropo WebImport example
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
# Derived from: http://paste.pocoo.org/show/407530/ | |
import sys | |
import imp | |
import urllib2 | |
import urlparse | |
class WebImporter(object): | |
def __init__(self, path): | |
url = urlparse.urlparse(path) | |
if url.scheme not in ('http', 'https'): | |
raise ImportError | |
self.path = path | |
self._cache = {} | |
@classmethod | |
def register(cls): | |
sys.path_hooks.append(cls) | |
def _get_source_and_filename(self, name): | |
rv = self._cache.get(name) | |
if rv is not None: | |
return rv | |
url_name = name.replace('.', '/') | |
if url_name.endswith('.py'): | |
urls = (url_name) | |
else: | |
urls = (url_name + '.py', url_name + '/__init__.py') | |
for filename in urls: | |
try: | |
url = urlparse.urljoin(self.path, filename) | |
try: | |
resp = urllib2.urlopen(url) | |
except urllib2.URLError: | |
continue | |
if resp.code == 404: | |
continue | |
rv = resp.read(), url | |
self._cache[name] = rv | |
return rv | |
except IOError: | |
continue | |
raise ImportError(name) | |
def get_source(self, name): | |
return self._get_source_and_filename(name)[0] | |
def get_filename(self, name): | |
return self._get_source_and_filename(name)[1] | |
def find_module(self, name, path=None): | |
try: | |
self._get_source_and_filename(name) | |
except ImportError: | |
return None | |
return self | |
def load_module(self, name): | |
source, filename = self._get_source_and_filename(name) | |
sys.modules[name] = mod = imp.new_module(name) | |
mod.__loader__ = self | |
mod.__file__ = filename | |
if filename.endswith('/__init__.py'): | |
mod.__path__ = [filename.rsplit('/', 1)[0]] | |
exec source in mod.__dict__ | |
return mod | |
# The 'ifmain trick' can't be used here, when Tropo exeutes this script | |
# __name__ is set to __builtin__ | |
answer() | |
WebImporter.register() | |
sys.path.insert(0, 'http://random.saghul.net/pymodules/') | |
import simplejson | |
try: | |
r = urllib2.urlopen('http://api.icndb.com/jokes/random') | |
except urllib2.URLError: | |
say('Error getting Chuck Norris fact') | |
else: | |
data = simplejson.loads(r.read()) | |
if data.get('type', None) == 'success': | |
say(data['value']['joke']) | |
else: | |
say('Failure!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment