def __map_request(self, url, methods, func_callback, produces='text/html'): # TODO: make a dict that uses class+method name as key if util.is_callable(func_callback) and not hasattr(func_callback, 'is_web_mapped'): regex_obj = re.compile("^%s$" % url) for m in methods: if log_method_mapping: print ' mapping url-path:' + url + ' = ' + func_callback.__name__ + '()' self.__map[m].append((regex_obj, url, func_callback)) func_callback.is_web_mapped = True func_callback.produces = produces
def login_check(check_func, failed_func=None): """Utility decorator to wrapped a method with login process""" if not check_func or not util.is_callable(check_func): raise Exception('Developer Error: check_func parameter must point to an existing validation function') if not check_func and not util.is_callable(check_func): raise Exception('Developer Error: failed_func parameter must point to an existing function') def _decorate_func_of(wrapped_func): def _wrap_call_with(*a, **ka): _auth = check_func(*a, **ka) if not _auth: if check_func: failed_func(*a, **ka) else: raise HttpRequestException("Page you are accessing requires successful Login", status=401) return wrapped_func(*a, **ka) functools.update_wrapper(_decorate_func_of, wrapped_func, updated=[]) return __wrap_call_with return _decorate_func_of
def start_server(server='wsgiref', host=None, port=None, debug=False, config=None): if not server in WSGI_ADAPTERS: raise RuntimeError("Server '%s' is not a valid server. Valid servers are: " % server , ",".join(k for (k,v) in WSGI_ADAPTERS.items())) if config is None: config = {} if not host is None: config.update({'host':str(host)}) else: config.update({'host':'localhost'}) if not port is None: config.update({'port':int(port)}) else: config.update({'port':8080}) config.update({'debug':debug}) if not 'port' in config: raise RuntimeError("Startup error. Server Port must be specified") if not 'host' in config: raise RuntimeError("Startup error. Host Address must be specified") http_handler = None try: if not 'request_handler' in config: http_handler = DefaultHttpRequestHandler(debug_enabled=debug) else: http_handler = config['request_handler'] if not util.is_callable(http_handler) or not isinstance(http_handler, HttpRequestHandler): raise RuntimeError("Startup error. Specified request_handler is not valid adbobopy.web.HttpRequestHandler.") config.update({'request_handler':http_handler}) server_adapter = WSGI_ADAPTERS[server] config.update({'server':server}) print print 'Starting AdoboPy Webserver' print 'Using run config options:' for k,v in config.items(): if 'request_handler' == k: v = v.__class__.__name__ else: v = str(v) print ' %s = %s' % (k, v) print print 'Use Ctrl-C to quit.' print server_adapter(config) except KeyboardInterrupt: print 'Server has been shutdowned' except (Exception, SystemExit, MemoryError): exc_type, exc_value, exc_traceback = sys.exc_info() print 'Shutting down due error encountered : ' traceback.print_exc()