-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcgii_module.py
More file actions
59 lines (46 loc) · 1.56 KB
/
cgii_module.py
File metadata and controls
59 lines (46 loc) · 1.56 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
# sys_shelve_importer.py
import imp
import os
import shelve
import sys
import requests
class GithubFinder:
def __init__(self, username, repository):
self.url = 'https://raw.githubusercontent.com/{}/{}/master/'.format(username, repository)
def find_module(self, fullname, path=None):
print(fullname, path)
if fullname.split('.')[0] != "testproject":
return None
print("Assuming project path is right")
return GithubLoader(self.url, fullname.split('.')[1:])
class GithubLoader:
"""Load source for modules from shelve databases."""
def __init__(self, url, path):
self.path = "/".join(path)
self.url = url + self.path + '.py'
def get_source_for_path(self, path):
r = requests.get(self.url)
if r.status_code != 200:
# ideally should treat this as module with __init__.py
return ""
else:
return r.text
def load_module(self, fullname):
source = self.get_source_for_path(fullname)
if fullname in sys.modules:
mod = sys.modules[fullname]
else:
mod = sys.modules.setdefault(
fullname,
imp.new_module(fullname)
)
# Set a few properties required by PEP 302
mod.__file__ = 'testproject'
mod.__name__ = fullname
mod.__path__ = '/dummy/path'
mod.__loader__ = self
mod.__package__ = fullname
print('execing source...')
exec(source, mod.__dict__)
print('done')
return mod