hikyuu2/hikyuu/util/check.py

133 lines
4.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#==========================================================
# History
# 1. 20200821, Added by fasiondog
#==========================================================
import sys
import traceback
import functools
from .mylog import hku_logger
class HKUCheckError(Exception):
def __init__(self, expression, message):
self.expression = expression
self.message = message
def __str__(self):
return str(self.message)
class HKUIngoreError(Exception):
def __init__(self, expression, message=None):
self.expression = expression
self.message = message if message is not None else 'ignore'
def __str__(self):
return str(self.message)
def checkif(expression, message, excepion=None, **kwargs):
"""如果 expression 为 True则抛出异常。注意该函数的判定和 assert 是相反的。
:param boolean expression:
:param str message:
:param Exception exception: None时 HKUCheckError
"""
if expression:
if excepion is None:
raise HKUCheckError(expression, message)
else:
raise excepion(message, **kwargs)
def hku_check(exp, msg, *args, **kwargs):
if not exp:
st = traceback.extract_stack()[-2]
check_exp = st._line.split(',')[0]
errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, msg.format(*args, **kwargs), st.name, st.filename, st.lineno)
raise HKUCheckError(exp, errmsg)
def hku_check_throw(expression, message, excepion=None, **kwargs):
"""如果 expression 为 False则抛出异常。
:param boolean expression:
:param str message:
:param Exception exception: None时 HKUCheckError
"""
if not expression:
st = traceback.extract_stack()[-2]
check_exp = st._line.split(',')[0]
errmsg = "{}) {} [{}] [{}:{}]".format(check_exp, message, st.name, st.filename, st.lineno)
if excepion is None:
raise HKUCheckError(expression, errmsg)
else:
raise excepion(errmsg, **kwargs)
def hku_check_ignore(exp, *args, **kwargs):
"""可忽略的检查"""
if not exp:
st = traceback.extract_stack()[-2]
check_exp = st._line.split(',')[0]
msg = kwargs.pop("msg") if "msg" in kwargs else ''
if msg:
errmsg = "{}) {} [{}] [{}:{}]".format(
check_exp, msg.format(*args, **kwargs), st.name, st.filename, st.lineno
)
elif args:
msg = args[0]
errmsg = "{}) {} [{}] [{}:{}]".format(
check_exp, msg.format(*args[1:], **kwargs), st.name, st.filename, st.lineno
)
raise HKUIngoreError(exp, errmsg)
def get_exception_info():
info = sys.exc_info()
return "{}: {}".format(info[0].__name__, info[1])
def hku_catch(ret=None, trace=False, callback=None, retry=1, with_msg=False):
"""捕获发生的异常, 包装方式: @hku_catch()
:param ret: , with_msg为True时 (ret, errmsg)
:param boolean trace:
:param func callback: func
:param int retry:
:param boolean with_msg: , True时 (ret, errmsg)
"""
def hku_catch_wrap(func):
@functools.wraps(func)
def wrappedFunc(*args, **kargs):
for i in range(retry):
errmsg = ""
try:
val = func(*args, **kargs)
return (val, errmsg) if with_msg else val
except HKUIngoreError:
errmsg = "{} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
hku_logger.debug(errmsg)
except Exception:
errmsg = "{} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
hku_logger.error(errmsg)
if trace:
traceback.print_exc()
if callback and i == (retry - 1):
callback(*args, **kargs)
except:
errmsg = "Unknown error! {} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
hku_logger.error(errmsg)
if trace:
traceback.print_exc()
if callback and i == (retry - 1):
callback(*args, **kargs)
return (ret, errmsg) if with_msg else ret
return wrappedFunc
return hku_catch_wrap