-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebimport.py
More file actions
69 lines (61 loc) · 2.01 KB
/
webimport.py
File metadata and controls
69 lines (61 loc) · 2.01 KB
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
"""
Stupid Python Trick - import modules over the web.
Author: Christian Wyglendowski
License: MIT (http://dowski.com/mit.txt)
"""
import requests
import imp
import sys
import os
def register_domain(name):
WebImporter.registered_domains.add(name)
parts = reversed(name.split('.'))
whole = []
for part in parts:
whole.append(part)
WebImporter.domain_modules.add(".".join(whole))
class WebImporter(object):
domain_modules = set()
registered_domains = set()
def find_module(self, fullname, path=None):
print(self.domain_modules)
if fullname in self.domain_modules:
return self
if fullname.rsplit('.')[0] not in self.domain_modules:
return None
try:
r = self._do_request(fullname, method="HEAD")
except ValueError:
return None
else:
if r.status == 200:
return self
return None
def load_module(self, fullname):
if fullname in sys.modules:
return sys.modules[fullname]
mod = imp.new_module(fullname)
mod.__loader__ = self
sys.modules[fullname] = mod
print(fullname, self.domain_modules)
if fullname not in self.domain_modules:
url = "http://%s%s" % self._get_host_and_path(fullname)
mod.__file__ = url
r = self._do_request(fullname)
code = r.read()
assert r.status == 200
for code in mod.__dict__:
exec(code)
else:
mod.__file__ = "[fake module %r]" % fullname
mod.__path__ = []
return mod
def _do_request(self, fullname, method="GET"):
host, path = self._get_host_and_path(fullname)
r = requests.get(os.path.join(host, path))
return r.raw
def _get_host_and_path(self, fullname):
tld, domain, rest = fullname.split('.', 2)
path = "/%s.py" % rest.replace('.', '/')
return ".".join([domain, tld]), path
sys.meta_path = [WebImporter()]