本文整理汇总了Python中pydal.base.BaseAdapter类的典型用法代码示例。如果您正苦于以下问题:Python BaseAdapter类的具体用法?Python BaseAdapter怎么用?Python BaseAdapter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseAdapter类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_environment
def build_environment(request, response, session, store_current=True):
"""
Build the environment dictionary into which web2py files are executed.
"""
#h,v = html,validators
environment = dict(_base_environment_)
if not request.env:
request.env = Storage()
# Enable standard conditional models (i.e., /*.py, /[controller]/*.py, and
# /[controller]/[function]/*.py)
response.models_to_run = [
r'^\w+\.py$',
r'^%s/\w+\.py$' % request.controller,
r'^%s/%s/\w+\.py$' % (request.controller, request.function)
]
t = environment['T'] = translator(os.path.join(request.folder,'languages'),
request.env.http_accept_language)
c = environment['cache'] = Cache(request)
if store_current:
current.globalenv = environment
current.request = request
current.response = response
current.session = session
current.T = t
current.cache = c
global __builtins__
if is_jython: # jython hack
__builtins__ = mybuiltin()
elif is_pypy: # apply the same hack to pypy too
__builtins__ = mybuiltin()
else:
__builtins__['__import__'] = __builtin__.__import__ # WHY?
environment['request'] = request
environment['response'] = response
environment['session'] = session
environment['local_import'] = \
lambda name, reload=False, app=request.application:\
local_import_aux(name, reload, app)
BaseAdapter.set_folder(pjoin(request.folder, 'databases'))
response._view_environment = copy.copy(environment)
custom_import_install()
return environment
示例2: wsgibase
#.........这里部分代码省略.........
# ##################################################
# run controller
# ##################################################
if global_settings.debugging and app != "admin":
import gluon.debug
# activate the debugger
gluon.debug.dbg.do_debug(mainpyfile=request.folder)
serve_controller(request, response, session)
except HTTP as hr:
http_response = hr
if static_file:
return http_response.to(responder, env=env)
if request.body:
request.body.close()
if hasattr(current, 'request'):
# ##################################################
# on success, try store session in database
# ##################################################
if not env.web2py_disable_session:
session._try_store_in_db(request, response)
# ##################################################
# on success, commit database
# ##################################################
if response.do_not_commit is True:
BaseAdapter.close_all_instances(None)
elif response.custom_commit:
BaseAdapter.close_all_instances(response.custom_commit)
else:
BaseAdapter.close_all_instances('commit')
# ##################################################
# if session not in db try store session on filesystem
# this must be done after trying to commit database!
# ##################################################
if not env.web2py_disable_session:
session._try_store_in_cookie_or_file(request, response)
# Set header so client can distinguish component requests.
if request.cid:
http_response.headers.setdefault(
'web2py-component-content', 'replace')
if request.ajax:
if response.flash:
http_response.headers['web2py-component-flash'] = \
urllib2.quote(xmlescape(response.flash).replace(b'\n', b''))
if response.js:
http_response.headers['web2py-component-command'] = \
urllib2.quote(response.js.replace('\n', ''))
# ##################################################
# store cookies in headers
# ##################################################
session._fixup_before_save()
http_response.cookies2headers(response.cookies)
示例3: main_wsgi_app
def main_wsgi_app(environ, start_response):
import gluon
common_context = {key:getattr(gluon,key) for key in dir(gluon)}
have_databases = False
try:
try:
current.request = request = Request(environ)
response = session = None
request_folder = request.folder
# if client requested a static page
if request.controller == 'static':
current.response = None
current.session = None
static_folder = os_path_join(request_folder,'static')
n = 3 if request.items[2].startswith('_') else 2
filename = os_path_join(static_folder,*request.items[n:])
if not filename.startswith(static_folder+'/'): raise HTTP(404)
if not os_path_exists(filename): raise HTTP(404)
stream_file_or_304_or_206(filename, environ=environ) # raise HTTP 200
# if instead client requested a dynamic page
else:
# build context and inject variables into context
runner = CodeRunner(common_context.copy())
# inject request specific variables into context
runner.context['request'] = request
runner.context['response'] = current.response = response = Response()
runner.context['session'] = current.session = session = Session()
runner.context['T'] = current.T = translator(
os_path_join(request_folder,'languages'),
request.environ.get('HTTP_ACCEPT_LANGUAGE'))
# check if there is a database folder and set the folder
database_folder = os_path_join(request_folder,'databases')
have_databases = os_path_exists(database_folder)
if have_databases:
BaseAdapter.set_folder(os_path_join(request_folder, 'databases'))
# raise an error if the controller file is missing
controllers_folder = os_path_join(request_folder,'controllers')
controller_filename = os_path_join(controllers_folder,request.controller+'.py')
if not controller_filename.startswith(controllers_folder+'/'): raise HTTP(404)
if not os_path_exists(controller_filename): raise HTTP(404)
# import models, ugly but faster than glob
models_folder = os_path_join(request_folder,'models')
if os_path_exists(models_folder):
for filename in sorted(filter(lambda x: x[-3:]=='.py',os.listdir(models_folder))):
runner.import_code(models_folder+os.sep+filename)
# run controller action
view_context = runner.context.copy()
content = runner.import_code(controller_filename, request.function)
# optionally run view
func_ext = request.function+'.'+request.extension
if isinstance(content, dict):
view_context.update(content)
template_folder = os_path_join(request_folder,'views')
# maybe a response.view is specified
if response.view:
template_filename = os_path_join(template_folder,response.view)
# or maybe not
else:
template_filename = os_path_join(template_folder,request.controller,func_ext)
# if the view exists use it
if os_path_exists(template_filename):
content = render(filename=template_filename, path = template_folder, context = view_context)
# else but represent the context as a dict (generic views?)
else:
content = repr(view_context)
# set the content type
response.headers["Content-type"] = contenttype(func_ext)
raise HTTP(response.status, content, headers=response.headers)
# if a HTTP is raised, everything is ok, return
except HTTP, http:
if response:
# commit databases, if any
have_databases = have_databases and response.auto_commit
if have_databases:
session._try_store_in_db(request, response)
BaseAdapter.close_all_instances('commit')
have_databases = False
# save session, if changed
session._try_store_in_cookie_or_file(request, response)
# deal with cookies
if hasattr(response,'_cookies'):
http.cookies2headers(response.cookies)
return http.to(start_response, env=environ)
# there was an error
except Exception, err:
# maybe log the ticket
if isinstance(err, RestrictedError):
ticket = err.log(request)
# or maybe not
else:
print traceback.format_exc()
#request.logger.error(traceback.format_exc())
ticket = 'unknown'
# return HTTP 500
return HTTP(500, ticket).to(start_response, env=environ)
示例4: isinstance
return http.to(start_response, env=environ)
# there was an error
except Exception, err:
# maybe log the ticket
if isinstance(err, RestrictedError):
ticket = err.log(request)
# or maybe not
else:
print traceback.format_exc()
#request.logger.error(traceback.format_exc())
ticket = 'unknown'
# return HTTP 500
return HTTP(500, ticket).to(start_response, env=environ)
# there was an error in handling the above error (like IOError)
except:
print traceback.format_exc()
#request.logger.error(traceback.format_exc())
# return HTTP 500
return HTTP(500, 'unknown').to(start_response, env=environ)
# but no matter what happens if the database was not committed, rollback
# and close any file that may be open
finally:
if session:
failsafe(lambda: session and session._unlock())
if have_databases:
failsafe(lambda: BaseAdapter.close_all_instances('rollback'))
def main():
print 'starting...'
HttpServer(main_wsgi_app, port=8888).start()
示例5: run
def run(
appname,
plain=False,
import_models=False,
startfile=None,
bpython=False,
python_code=False,
cronjob=False):
"""
Start interactive shell or run Python script (startfile) in web2py
controller environment. appname is formatted like:
- a : web2py application name
- a/c : exec the controller c into the application environment
"""
(a, c, f, args, vars) = parse_path_info(appname, av=True)
errmsg = 'invalid application name: %s' % appname
if not a:
die(errmsg)
adir = os.path.join('applications', a)
if not os.path.exists(adir):
if sys.stdin and not sys.stdin.name == '/dev/null':
confirm = raw_input(
'application %s does not exist, create (y/n)?' % a)
else:
logging.warn('application does not exist and will not be created')
return
if confirm.lower() in ['y', 'yes']:
os.mkdir(adir)
w2p_unpack('welcome.w2p', adir)
for subfolder in ['models', 'views', 'controllers', 'databases',
'modules', 'cron', 'errors', 'sessions',
'languages', 'static', 'private', 'uploads']:
subpath = os.path.join(adir, subfolder)
if not os.path.exists(subpath):
os.mkdir(subpath)
db = os.path.join(adir, 'models/db.py')
if os.path.exists(db):
data = fileutils.read_file(db)
data = data.replace(
'<your secret key>', 'sha512:' + web2py_uuid())
fileutils.write_file(db, data)
if c:
import_models = True
extra_request = {}
if args:
extra_request['args'] = args
if vars:
extra_request['vars'] = vars
_env = env(a, c=c, f=f, import_models=import_models, extra_request=extra_request)
if c:
pyfile = os.path.join('applications', a, 'controllers', c + '.py')
pycfile = os.path.join('applications', a, 'compiled',
"controllers_%s_%s.pyc" % (c, f))
if ((cronjob and os.path.isfile(pycfile))
or not os.path.isfile(pyfile)):
exec read_pyc(pycfile) in _env
elif os.path.isfile(pyfile):
execfile(pyfile, _env)
else:
die(errmsg)
if f:
exec ('print %s()' % f, _env)
return
_env.update(exec_pythonrc())
if startfile:
try:
ccode = None
if startfile.endswith('.pyc'):
ccode = read_pyc(startfile)
exec ccode in _env
else:
execfile(startfile, _env)
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception, e:
print traceback.format_exc()
if import_models:
BaseAdapter.close_all_instances('rollback')
示例6: read_pyc
ccode = read_pyc(startfile)
exec ccode in _env
else:
execfile(startfile, _env)
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception, e:
print traceback.format_exc()
if import_models:
BaseAdapter.close_all_instances('rollback')
elif python_code:
try:
exec(python_code, _env)
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception, e:
print traceback.format_exc()
if import_models:
BaseAdapter.close_all_instances('rollback')
else:
if not plain:
if bpython:
try:
import bpython
bpython.embed(locals_=_env)
return
except:
logger.warning(
'import bpython error; trying ipython...')
else:
示例7: run
def run(
appname,
plain=False,
import_models=False,
startfile=None,
bpython=False,
python_code=False,
cronjob=False):
"""
Start interactive shell or run Python script (startfile) in web2py
controller environment. appname is formatted like:
- a : web2py application name
- a/c : exec the controller c into the application environment
"""
(a, c, f, args, vars) = parse_path_info(appname, av=True)
errmsg = 'invalid application name: %s' % appname
if not a:
die(errmsg)
adir = os.path.join('applications', a)
if not os.path.exists(adir):
if sys.stdin and not sys.stdin.name == '/dev/null':
confirm = raw_input(
'application %s does not exist, create (y/n)?' % a)
else:
logging.warn('application does not exist and will not be created')
return
if confirm.lower() in ['y', 'yes']:
os.mkdir(adir)
w2p_unpack('welcome.w2p', adir)
for subfolder in ['models', 'views', 'controllers', 'databases',
'modules', 'cron', 'errors', 'sessions',
'languages', 'static', 'private', 'uploads']:
subpath = os.path.join(adir, subfolder)
if not os.path.exists(subpath):
os.mkdir(subpath)
db = os.path.join(adir, 'models/db.py')
if os.path.exists(db):
data = fileutils.read_file(db)
data = data.replace(
'<your secret key>', 'sha512:' + web2py_uuid())
fileutils.write_file(db, data)
if c:
import_models = True
extra_request = {}
if args:
extra_request['args'] = args
if vars:
extra_request['vars'] = vars
_env = env(a, c=c, f=f, import_models=import_models, extra_request=extra_request)
if c:
pyfile = os.path.join('applications', a, 'controllers', c + '.py')
pycfile = os.path.join('applications', a, 'compiled',
"controllers_%s_%s.pyc" % (c, f))
if ((cronjob and os.path.isfile(pycfile))
or not os.path.isfile(pyfile)):
exec(read_pyc(pycfile), _env)
elif os.path.isfile(pyfile):
execfile(pyfile, _env)
else:
die(errmsg)
if f:
exec('print %s()' % f, _env)
return
_env.update(exec_pythonrc())
if startfile:
try:
ccode = None
if startfile.endswith('.pyc'):
ccode = read_pyc(startfile)
exec(ccode, _env)
else:
execfile(startfile, _env)
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception as e:
print(traceback.format_exc())
if import_models:
BaseAdapter.close_all_instances('rollback')
elif python_code:
try:
exec(python_code, _env)
if import_models:
BaseAdapter.close_all_instances('commit')
except Exception as e:
print(traceback.format_exc())
if import_models:
BaseAdapter.close_all_instances('rollback')
else:
if not plain:
if bpython:
try:
import bpython
#.........这里部分代码省略.........
示例8: build_environment
def build_environment(request, response, session, store_current=True):
"""
Build the environment dictionary into which web2py files are executed.
"""
# h,v = html,validators
environment = dict(_base_environment_)
if not request.env:
request.env = Storage()
# Enable standard conditional models (i.e., /*.py, /[controller]/*.py, and
# /[controller]/[function]/*.py)
response.models_to_run = [
r'^\w+\.py$',
r'^%s/\w+\.py$' % request.controller,
r'^%s/%s/\w+\.py$' % (request.controller, request.function)
]
T = environment['T'] = TranslatorFactory(pjoin(request.folder, 'languages'),
request.env.http_accept_language)
c = environment['cache'] = Cache(request)
# configure the validator to use the t translator
Validator.translator = staticmethod(lambda text: None if text is None else str(T(text)))
if store_current:
current.globalenv = environment
current.request = request
current.response = response
current.session = session
current.T = T
current.cache = c
if global_settings.is_jython:
# jython hack
class mybuiltin(object):
"""
NOTE could simple use a dict and populate it,
NOTE not sure if this changes things though if monkey patching import.....
"""
# __builtins__
def __getitem__(self, key):
try:
return getattr(builtin, key)
except AttributeError:
raise KeyError(key)
def __setitem__(self, key, value):
setattr(self, key, value)
global __builtins__
__builtins__ = mybuiltin()
environment['request'] = request
environment['response'] = response
environment['session'] = session
environment['local_import'] = \
lambda name, reload=False, app=request.application:\
local_import_aux(name, reload, app)
BaseAdapter.set_folder(pjoin(request.folder, 'databases'))
custom_import_install()
return environment