forked from mitsuhiko/badideas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubimporter.py
91 lines (76 loc) · 2.53 KB
/
githubimporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""
githubimporter
~~~~~~~~~~~~~~
Imports code directly from github.
:copyright: (c) Copyright 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import sys
import imp
import urllib
import urlparse
class GithubImporter(object):
url_template = 'https://raw.github.com/%(user)s/%(project)s/master/%(file)s'
def __init__(self, path):
url = urlparse.urlparse(path)
if url.scheme != 'github':
raise ImportError()
self.user = url.netloc
self.project = url.path.strip('/')
if '/' in self.project:
self.project, self.path = self.project.split('/', 1)
else:
self.path = ''
self._cache = {}
def get_source_and_filename(self, name):
rv = self._cache.get(name)
if rv is not None:
return rv
url_name = name.replace('.', '/')
for filename in url_name + '.py', url_name + '/__init__.py':
try:
url = self.url_template % dict(
user=self.user,
project=self.project,
file=urlparse.urljoin(self.path, filename)
)
resp = urllib.urlopen(url)
if resp.code == 404:
continue
rv = resp.read(), 'github://%s/%s' % (
self.user,
filename
)
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
def install_hook():
sys.path_hooks.append(GithubImporter)
if __name__ == '__main__':
install_hook()
sys.path.append('github://mitsuhiko/markupsafe')
import markupsafe
print markupsafe.__file__
print markupsafe.Markup.escape('<foo>')