本文整理汇总了Python中pydal.base.BaseAdapter.close_all_instances方法的典型用法代码示例。如果您正苦于以下问题:Python BaseAdapter.close_all_instances方法的具体用法?Python BaseAdapter.close_all_instances怎么用?Python BaseAdapter.close_all_instances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydal.base.BaseAdapter
的用法示例。
在下文中一共展示了BaseAdapter.close_all_instances方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wsgibase
# 需要导入模块: from pydal.base import BaseAdapter [as 别名]
# 或者: from pydal.base.BaseAdapter import close_all_instances [as 别名]
#.........这里部分代码省略.........
# ##################################################
# 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)
示例2: main_wsgi_app
# 需要导入模块: from pydal.base import BaseAdapter [as 别名]
# 或者: from pydal.base.BaseAdapter import close_all_instances [as 别名]
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)
示例3: isinstance
# 需要导入模块: from pydal.base import BaseAdapter [as 别名]
# 或者: from pydal.base.BaseAdapter import close_all_instances [as 别名]
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()
示例4: run
# 需要导入模块: from pydal.base import BaseAdapter [as 别名]
# 或者: from pydal.base.BaseAdapter import close_all_instances [as 别名]
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')
示例5: read_pyc
# 需要导入模块: from pydal.base import BaseAdapter [as 别名]
# 或者: from pydal.base.BaseAdapter import close_all_instances [as 别名]
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:
示例6: run
# 需要导入模块: from pydal.base import BaseAdapter [as 别名]
# 或者: from pydal.base.BaseAdapter import close_all_instances [as 别名]
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
#.........这里部分代码省略.........