try: # bottle , . @bottle.hook('before_request') def pre_init(): # , ini_environment() # # options_session # bottle app = bottle.app() app.catchall = False # . debugger = DebuggerM(app) # . session = SessionMiddleware(debugger, options_session) # application wsgi' application = session except Exception as e: # # . exc = sys.exc_info() def error_apps(environ, start_response): start_response('500 INTERNAL SERVER ERROR', [('Content-Type', 'text/html; charset=utf-8')]) return view_error(exc, environ, e) application = error_apps
class DebuggerM(object): """ """ __app = None def __init__ (self, app): # bottle # app- , application middleware self.__app = app def __call__(self, environ, start_response): try: # ( ) app_iter = self.__app(environ, start_response) for item in app_iter: # # yield item except Exception as e: # . start_response('500 INTERNAL SERVER ERROR', [('Content-Type', 'text/html; charset=utf-8')]) yield view_error(sys.exc_info(), environ, e)
def view_error(exc, environ, e): import cgi, traceback ee = dict(environ) text= '' text='<h1>ERROR "%s"</h1>' % str(e) text+='<style>pre{border:red solid 1px; max-height:240px; overflow:auto;}</style>' text+='<h2>Stack trace</h2><pre>%s</pre>' % ''.join(reversed(traceback.format_exception(*exc))) text+='<h2>Env</h2><pre">%s </pre>' % ' '.join(['%s=%s' %(k, ee[k])for k in ee]) text+='</pre>' return text
@route('/hello/:name') def index(name='World'): return '<b>Hello %s</b>' % name
from app1 import * routes = { 'function': ('/hello/', hello, 'GET'), 'function2':('/hello/<_id>', hello_post, 'POST') }
all_routes = {} def union_routes(dir): routes = {} # . if __builtin__.__import__('routes', globals=globals()): # module = sys.modules['routes'] # routes routes.update(module.routes) # for name in os.listdir(dir): path = os.path.join(dir, name) # routes.py if os.path.isdir(path) and os.path.isfile(path+'/routes.py'): # name = 'app.'+path[len(dir)+1:]+'.routes' if __builtin__.__import__(name, globals=globals()): module = sys.modules[name] routes.update(module.routes) return routes def setup_routes(routes): # bottle . all_routes.clear() all_routes.update(routes) for name in routes: path, func, method = routes[name] route(path, method=method, name=name)(func)
routes = {} # routes.update(union_routes(lib_path)) # routes.update(union_routes(app_path)) setup_routes(routes)
from app1 import * routes = { 'function2': ('/hello/<_id>', hello_post, 'POST'), 'function3': ('/base/1', lambda: test(True, u''), 'GET') }
def make_link(name, params, full=False): """ <> """ # templ_link = all_routes[name][0][1:] # <> r = re.compile(r'<([^:>]+)(:[^>]+)?>') # , . while True: rs = r.search(templ_link) if not rs: break sub = rs.group(0) name = rs.group(1) templ_link = re.sub(sub, params[name], teml_link) link = os.path.sep+ templ_link # , . if full: link = 'http://'+get_name_host()+link return link
link = make_link('test', {'id':id, 'doc_id':doc_id}, True)
from beaker.middleware import SessionMiddleware
options_session = { 'session.cookie_expires': 30000, 'session.timeout': 30000, 'session.type': 'file', # 'session.data_dir': './s_data' # }
session.options['session.cookie_domain'] = get_name_host()
def session(): # s = request.environ.get('beaker.session') return s
s = session() s['test'] = '123' s.save()
# bottle , . @route('/static/<component>/<fname:re:.*>') def st_file(component, fname): # , bottle path = os.path.join( settings.lib_path, component, 'static') + os.path.sep if not os.path.exists(path + fname): path = os.path.join( settings.lib_path, 'app', component,'static')+ os.path.sep if not os.path.exists( path + fname): path = os.path.join( os.getcwd(), 'app', component, 'static')+ os.path.sep if not os.path.exists(path + fname) and component == 'static': path = os.path.join( os.getcwd(), 'static')+ os.path.sep return static_file(fname, root=path)
def add_resourse(name, append=True): env = get_environment(); t = '' if name.endswith('.css'): t = 'css' if name.endswith('.js'): t = 'js' if not t: return if not name in env['res_list'][t]: if append: env['res_list'][t].append(name) else: env['res_list'][t].prepend(name)
add_resourse(/static/base/base.js, True)
{% for res in env.res_list['js'] %} <script type="text/javascript" src="{{res}}"></script> {% endfor %} {% for res in env.res_list['css'] %} <link rel="stylesheet" type="text/css" href="{{res}}" /> {% endfor %}
from app.app_one.view import * routes = { 'hello': ('/', hello, 'GET') }
def hello(): text = 'It works' return templ('hello', text=text)
{{text}}
Source: https://habr.com/ru/post/148261/
All Articles