def Tassadar(): _db = 'tassadar' conf = Configuration().get_section(_db) database_url = conf.get('url') try: return MySQLHelper(database_url) except Exception, e: print e.message print_stack() return None
class BaseHandler(RequestHandler): datatype = 'json' error_code = ERROR SUCCESS_FLAG = 'success' @coroutine def get(self): yield Task(self.run) self.do_response() @coroutine def post(self): yield Task(self.run) self.do_response() def do_response(self): self.add_header("Connection", "keep-alive") if self.datatype == 'json': data = { 'status': str(self.status), 'errstr': self.errstr, 'result': self.result, } jdata = json.dumps(data) self.add_header("Content-Type", "application/json") if self.call_back: self.add_header("Access-Control-Allow-Origin", "*") jdata = "%s(%s)" % (self.call_back, jdata) self.add_header("Content-Type", "application/json;charset=utf-8") self.write(jdata) else: self.write(self.result) @coroutine def run(self, *args, **kwargs): try: self.do_action() except IllegalArgumentException, ie: self.send_error(self.error_code.MISSING_ARGUMENT, ie.message) print_stack() except Exception, e: self.set_error(self.error_code.UNKNOW_ERROR, e.message) self.ext_log_data.append(e.message) print_stack()
def run(self, *args, **kwargs): try: self.do_action() except IllegalArgumentException, ie: self.send_error(self.error_code.MISSING_ARGUMENT, ie.message) print_stack()