forked from jurev/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlimport.py
54 lines (40 loc) · 1.55 KB
/
urlimport.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
"""
urlimport.py - Enables remote module importing by adding URLs to sys.path.
See PEP 302(http://www.python.org/dev/peps/pep-0302/) for more info.
"""
import sys, re
import knockout
from urlparse import urljoin
from urllib2 import urlopen
log = knockout.log
class UrlImporter(knockout.Importer):
re_fullpath = re.compile(''.join([
r'^',
r'(?P<path>(http|https|ftp)://[^#]+)',
r'#(?P<package>.+)',
r'$'
]))
def join(self, *parts):
return urljoin(*parts)
def get_source(self, fullname, ispkg):
"""Download the source for the new module to be loaded.
"""
# are we allowed to download this module?
name = fullname.split(".")[-1]
if self.package != '__all__' and name != self.package:
raise Exception("Not allowed to import '%s'." % fullname)
fullpath = self.fullpath(fullname, ispkg)
return urlopen(fullpath).read().replace("\r\n", "\n"), fullpath
def get_loader(self, source, fullpath, ispkg):
""" Get the loader instance to load the new module.
"""
return knockout.Loader(source, fullpath, ispkg, importer=self)
# register The Hook
def register():
if UrlImporter.register() != False:
log.info("Url importing enabled. Add urls to sys.path.")
log.info("A valid url looks like this: http://example.com/path/to/repository/#packagename")
log.info("This stuff is experimental, use at your own risk. Enjoy.")
def clear_cache():
sys.path_importer_cache.clear()
#register()