forked from imlyj/itranswarp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.py
122 lines (107 loc) · 3.6 KB
/
loader.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Michael Liao'
' Loader module that load modules dynamic. '
import os, logging, functools
from transwarp.web import ctx, forbidden, notfound
from transwarp import db, i18n
from auth import extract_session_cookie, http_basic_auth
from apiexporter import cached
@cached(key='website', use_ctx=False)
def _get_site(host):
wss = db.select('select * from websites where domain=?', host)
if wss:
ws = wss[0]
if ws.disabled:
logging.debug('website is disabled: %s' % host)
raise forbidden()
return ws
raise notfound()
def load_site(func):
@functools.wraps(func)
def _wrapper(*args, **kw):
website = None
host = ctx.request.host.lower()
n = host.find(u':')
if n!=(-1):
host = host[:n]
logging.debug('try load website: %s' % host)
ws = _get_site(host)
ctx.website = ws
try:
return func(*args, **kw)
finally:
del ctx.website
return _wrapper
def load_user(func):
@functools.wraps(func)
def _wrapper(*args, **kw):
user = extract_session_cookie()
if user is None:
auth = ctx.request.header('AUTHORIZATION')
logging.debug('get authorization header: %s' % auth)
if auth and auth.startswith('Basic '):
user = http_basic_auth(auth[6:])
if user and ctx.website.id!=user.website_id:
user = None
logging.info('bind ctx.user')
ctx.user = user
try:
return func(*args, **kw)
finally:
del ctx.user
return _wrapper
def load_i18n(func):
@functools.wraps(func)
def _wrapper(*args, **kw):
lc = 'en'
al = ctx.request.header('ACCEPT-LANGUAGE')
if al:
lcs = al.split(',')
lc = lcs[0].strip().lower()
with i18n.locale(lc):
return func(*args, **kw)
return _wrapper
def load_module(module_name):
'''
Load a module and return the module reference.
'''
pos = module_name.rfind('.')
if pos==(-1):
return __import__(module_name, globals(), locals(), [module_name])
return __import__(module_name, globals(), locals(), [module_name[pos+1:]])
def scan_submodules(module_name):
'''
Scan sub modules and import as dict (key=module name, value=module).
>>> ms = scan_submodules('apps')
>>> type(ms['article'])
<type 'module'>
>>> ms['article'].__name__
'apps.article'
>>> type(ms['website'])
<type 'module'>
>>> ms['website'].__name__
'apps.website'
'''
web_root = os.path.dirname(os.path.abspath(__file__))
mod_path = os.path.join(web_root, *module_name.split('.'))
if not os.path.isdir(mod_path):
raise IOError('No such file or directory: %s' % mod_path)
dirs = os.listdir(mod_path)
mod_dict = {}
for name in dirs:
if name=='__init__.py':
continue
p = os.path.join(mod_path, name)
if os.path.isfile(p) and name.endswith('.py'):
pyname = name[:-3]
mod_dict[pyname] = __import__('%s.%s' % (module_name, pyname), globals(), locals(), [pyname])
if os.path.isdir(p) and os.path.isfile(os.path.join(mod_path, name, '__init__.py')):
mod_dict[name] = __import__('%s.%s' % (module_name, name), globals(), locals(), [name])
return mod_dict
def load_navigations():
navs = db.select('select * from navigations where website_id=? order by display_order', ctx.website.id)
return navs
if __name__=='__main__':
import doctest
doctest.testmod()