def run( self, server=None, host=None, port=None, enable_pretty_logging=True ): """ 运行 WeRoBot。 :param server: 传递给 Bottle 框架 run 方法的参数,详情见\ `bottle 文档 <https://bottlepy.org/docs/dev/deployment.html#switching-the-server-backend>`_ :param host: 运行时绑定的主机地址 :param port: 运行时绑定的主机端口 :param enable_pretty_logging: 是否开启 log 的输出格式优化 """ if enable_pretty_logging: from werobot.logger import enable_pretty_logging enable_pretty_logging(self.logger) if server is None: server = self.config["SERVER"] if host is None: host = self.config["HOST"] if port is None: port = self.config["PORT"] try: self.wsgi.run(server=server, host=host, port=port) except KeyboardInterrupt: exit(0)
def run(self, server=None, host=None, port=None, enable_pretty_logging=True): """ 运行 WeRoBot。 :param server: 传递给 Bottle 框架 run 方法的参数,详情见\ `bottle 文档 <https://bottlepy.org/docs/dev/deployment.html#switching-the-server-backend>`_ :param host: 运行时绑定的主机地址 :param port: 运行时绑定的主机端口 :param enable_pretty_logging: 是否开启 log 的输出格式优化 """ if enable_pretty_logging: from werobot.logger import enable_pretty_logging enable_pretty_logging(self.logger) if server is None: server = self.config["SERVER"] if host is None: host = self.config["HOST"] if port is None: port = self.config["PORT"] try: self.wsgi.run(server=server, host=host, port=port) except KeyboardInterrupt: exit(0)
def init(self, env): dbname = env.cr.dbname global WxEnvDict if dbname in WxEnvDict: del WxEnvDict[dbname] WxEnvDict[dbname] = self try: config = env['wx.config'].sudo().get_cur() action = config.action except: import traceback traceback.print_exc() action = None if action: self.subscribe_auto_msg = config.action.get_wx_reply() Param = env['ir.config_parameter'].sudo() self.wx_token = Param.get_param('wx_token') or '' self.wx_appid = Param.get_param('wx_appid') or '' self.wx_AppSecret = Param.get_param('wx_AppSecret') or '' #robot.config["TOKEN"] = self.wx_token self.wxclient.appid = self.wx_appid self.wxclient.appsecret = self.wx_AppSecret try: # 刷新 AccessToken self.wxclient._token = None _ = self.wxclient.token except: import traceback traceback.print_exc() _logger.error(u'初始化微信客户端token失败,请在微信对接配置中填写好相关信息!') session_storage = MemoryStorage() robot = WeRoBot(token=self.wx_token, enable_session=True, logger=_logger, session_storage=session_storage) enable_pretty_logging(robot.logger) self.robot = robot try: users = env['wx.user'].sudo().search([('last_uuid', '!=', None)]) for obj in users: if obj.last_uuid_time: self.recover_uuid( obj.openid, obj.last_uuid, fields.Datetime.from_string(obj.last_uuid_time)) except: env.cr.rollback() import traceback traceback.print_exc() print('wx client init: %s %s' % (self.OPENID_UUID, self.UUID_OPENID))
def run(self, server=None, host=None, port=None, enable_pretty_logging=True): if enable_pretty_logging: from werobot.logger import enable_pretty_logging enable_pretty_logging(self.logger) if server is None: server = self.config["SERVER"] if host is None: host = self.config["HOST"] if port is None: port = self.config["PORT"] self.wsgi.run(server=server, host=host, port=port)
def init(self, env): dbname = env.cr.dbname global WxEnvDict if dbname in WxEnvDict: del WxEnvDict[dbname] WxEnvDict[dbname] = self Param = env['ir.config_parameter'].sudo() self.wx_token = Param.get_param('wx_token') or '' self.wx_appid = Param.get_param('wx_appid') or '' self.wx_AppSecret = Param.get_param('wx_AppSecret') or '' #robot.config["TOKEN"] = self.wx_token self.wxclient.appid = self.wx_appid self.wxclient.appsecret = self.wx_AppSecret try: # 刷新 AccessToken self.wxclient._token = None _ = self.wxclient.token except: import traceback traceback.print_exc() _logger.error(u'初始化微信客户端token失败,请在微信对接配置中填写好相关信息!') session_storage = MemoryStorage() robot = WeRoBot(token=self.wx_token, enable_session=True, logger=_logger, session_storage=session_storage) enable_pretty_logging(robot.logger) self.robot = robot try: users = env['wx.user'].sudo().search([('last_uuid', '!=', None)]) for obj in users: self.OPENID_UUID[obj.openid] = obj.last_uuid self.UUID_OPENID[obj.last_uuid] = obj.openid except: env.cr.rollback() import traceback traceback.print_exc() print('wx client init: %s %s' % (self.OPENID_UUID, self.UUID_OPENID))
from openerp import http from openerp.http import request _logger = logging.getLogger(__name__) def abort(code): return werkzeug.wrappers.Response('Unknown Error: Application stopped.', status=code, content_type='text/html;charset=utf-8') class WeRoBot(BaseRoBot): pass robot = WeRoBot(token='K5Dtswpte', enable_session=True, logger=_logger) enable_pretty_logging(robot.logger) class WxController(http.Controller): ERROR_PAGE_TEMPLATE = """ <!DOCTYPE html> <html> <head> <meta charset="utf8" /> <title>Error: {{e.status}}</title> <style type="text/css"> html {background-color: #eee; font-family: sans;} body {background-color: #fff; border: 1px solid #ddd; padding: 15px; margin: 15px;} pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;} </style>
def abort(code): return werkzeug.wrappers.Response('Unknown Error: Application stopped.', status=code, content_type='text/html;charset=utf-8') class WeRoBot(BaseRoBot): pass robot = WeRoBot(token='K5Dtswpte', enable_session=True, logger=_logger, session_storage=session_storage) enable_pretty_logging(robot.logger) class WxController(http.Controller): ERROR_PAGE_TEMPLATE = """ <!DOCTYPE html> <html> <head> <meta charset="utf8" /> <title>Error: {{e.status}}</title> <style type="text/css"> html {background-color: #eee; font-family: sans;} body {background-color: #fff; border: 1px solid #ddd; padding: 15px; margin: 15px;} pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
def init(self, env): dbname = env.cr.dbname global WxEnvDict if dbname in WxEnvDict: del WxEnvDict[dbname] WxEnvDict[dbname] = self try: config = env['wx.config'].sudo().get_cur() action = config.action except: import traceback traceback.print_exc() action = None if action: self.subscribe_auto_msg = config.action.get_wx_reply() Param = env['ir.config_parameter'].sudo() self.wx_token = Param.get_param('wx_token') or '' self.wx_appid = Param.get_param('wx_appid') or '' self.wx_AppSecret = Param.get_param('wx_AppSecret') or '' self.server_url = Param.get_param('server_url') or '' self.session_storage = Param.get_param('session_storage') or '' self.wxclient.config["APP_ID"] = self.wx_appid self.wxclient.config["APP_SECRET"] = self.wx_AppSecret self.wxclient.config["server_url"] = self.server_url if not self.session_storage: session_storage = MemoryStorage() _logger.info("启用MemoryStorage") else: _logger.info("启用RedisStorage%s" % self.wx_appid) db = redis.Redis(host=self.session_storage, port=6379) session_storage = RedisStorage(db, prefix=self.wx_appid) try: # 获取以前的token是否需要获取新的Token AccessToken self.wxclient.session = session_storage # self.wxclient._token = session_storage.get(self.access_token_key) _ = self.wxclient.token except Exception as e: print(e) import traceback traceback.print_exc() _logger.error(u'初始化微信客户端token失败,请在微信对接配置中填写好相关信息!') robot = WeRoBot(token=self.wx_token, enable_session=True, logger=_logger, session_storage=session_storage) enable_pretty_logging(robot.logger) self.robot = robot try: wechatpy_client = WeChatClient(self.wx_appid, self.wx_AppSecret, access_token=self.wxclient.token, session=session_storage) self.wechatpy_client = wechatpy_client except Exception as e: print(e) _logger.error("加载微信token错误。") try: users = env['wx.user'].sudo().search([('last_uuid', '!=', None)]) for obj in users: if obj.last_uuid_time: self.recover_uuid( obj.openid, obj.last_uuid, fields.Datetime.from_string(obj.last_uuid_time)) except: env.cr.rollback() import traceback traceback.print_exc() print('wx client init: %s %s' % (self.OPENID_UUID, self.UUID_OPENID))
def test_handlers(): logger = get_new_logger() enable_pretty_logging(logger) assert isinstance(logger.handlers[0].formatter, _LogFormatter)
def test_logger_level(): for level in ('debug', 'info', 'warning', 'error'): logger = get_new_logger() enable_pretty_logging(logger, level=level) assert logger.level == getattr(logging, level.upper())