Example #1
0
 def wrapper(*args, **kw):
     try:
         return func(*args, **kw)
     except Exception as e:
         i('sql alchemy ctx error??? %s' % e)
         with app.app_context():
             return func(*args, **kw)
Example #2
0
 def wrapper(*args, **kw):
     try:
         if is_file:
             return func(*args, **kw)
         r = func(*args, **kw)
         # The view function did not return a valid response.
         # The return type must be a string, dict, tuple,Response instance, or WSGI callable,
         # but it was a int.
         if isNotEmpty(r) and \
                 (isinstance(r, Response) or
                  isinstance(r, str) or
                  isinstance(r, dict) or
                  isinstance(r, dict) or
                  isinstance(r, tuple)):
             return r
         raise Exception('返回的数据格式不对%s' % type(r))
     except Exception:
         error_e = traceback.format_exc()
         i('try_except %s --- %s' % (func, error_e))
         from libs.response_standard import response
         result = response(501, 'service error',
                           is_response=False) if ob is None else ob
         if isinstance(result, type(response(is_response=False))):
             result['extra'] = '%s() %s (501-2)' % (str(
                 func.__name__), error_e)
             return result
         return error_e
Example #3
0
def send_mail(recipients: list,
              title,
              content_txt: str = None,
              content_html=None):
    from config import app
    if app.config['SEND_MAIL']:
        print('DEBUG状态,邮件被禁止发送了 \r\n \t%s \r\n \t%s \r\n \t%s' %
              (recipients, title,
               content_txt if content_html is None else content_html))
        return
    from flask_mail import Message
    from config import mail

    msg = Message(title,
                  sender=('oneself - service', app.config['MAIL_USERNAME']),
                  recipients=recipients)
    msg.body = content_txt
    msg.html = content_html
    try:
        mail.send(msg)
    except Exception:
        try:
            with app.app_context():
                mail.send(msg)
        except Exception as e:
            import traceback
            i('邮件发送失败%s' % traceback.format_exc())
Example #4
0
 def wrapper(*args, **kw):
     try:
         return func(*args, **kw)
     except Exception:
         error_e = traceback.format_exc()
         i('try_except %s --- %s' % (func, error_e))
         if return_except:
             return error_e
Example #5
0
 def wrapper(*args, **kw):
     import time
     start = time.time()
     r = func(*args, **kw)
     i("%s%s方法执行时间为:%s" %
       ('' if txt is None else '%s ' % txt, func.__name__,
        '%.3fs' % float(time.time() - start)))
     return r
Example #6
0
File: db.py Project: zwping/py_libs
 def commit():
     try:
         db.session.commit()
         return True
     except Exception as e:
         i('db commit 报错 %s' % e)
         # i(traceback.format_exc())
         db.session.rollback()
         raise Exception(e)  # 异常上报,让sqlalchemy_ctx补捉处理
Example #7
0
        def wrapper(*args, **kw):
            import time
            start = time.time()
            r = func(*args, **kw)
            t = time.time() - start

            if t > min_time:
                i("方法执行超时(%.2fs) %s --- %s --- %s" %
                  (min_time, func.__name__, args, int(t * 1000)))
            return r
Example #8
0
 def wrapper(*args, **kw):
     browser: WebDriver = cus_webdriver(chromedriverPath, headless)
     try:
         kw.update({'browser': browser})
         r = func(*args, **kw)
         if not debug:
             browser.implicitly_wait(3)
             browser.quit()
         return r
     except Exception as e:
         i('selenium崩溃:%s' % e)
         browser.quit()
Example #9
0
 def wrapper(*args, **kw):
     __call_size = kw.get('__call_size')
     if isNotEmpty(__call_size) and __call_size >= loop_size:
         kw.pop('__call_size')
         return func(*args, **kw)
     try:
         if isNotEmpty(__call_size):
             kw.pop('__call_size')
         return func(*args, **kw)
     except Exception as e:
         i('死循环 Exception??%s' % e)
         if isEmpty(__call_size):
             __call_size = 0
         __call_size += 1
         kw.update({'__call_size': __call_size})
         time.sleep(1)
         return wrapper(*args, **kw)
Example #10
0
File: db.py Project: zwping/py_libs
    def retrieveWholes(sql: str,
                       column_names: tuple = None,
                       fetchone=False,
                       bind=None):
        """ 查询整张表字段数据,返回JSON
        :param sql
        :param column_names 需要返回的json key ,需和sql中查询字段对于
        :param fetchone 只取一个
         """
        if isEmpty(column_names):
            import re
            re1 = re.search('from.\w*', sql)
            if isEmpty(re1):
                raise Exception('正则错误,获取表名失败,不支持的sql语句 %s' % sql)
            table_name = re1.group().replace('from ', '')
            t = DB.retrieve(
                ConstantSql.column_name(table_name, app.config['DATABASE_NAME']
                                        )  # todo appConfig中需要增加DATABASE_NAME
                ,
                bind)  # 获取表字段
            if isEmpty(t):
                i('查询表列名出错 %s' % traceback.format_exc())
                raise Exception('查询表列名出错')
            column_names = tuple(s[0] for s in t)
        elif '*' in sql:
            sql = sql.replace(
                '*',
                str(column_names).replace('(',
                                          '').replace(')',
                                                      '').replace("'", ''))

        r = DB.retrieve(sql, fetchone, bind)
        if isEmpty(r):
            return None if fetchone else []
        if fetchone:
            return DBSup.__tuple_cov_json(column_names, r)
        else:
            return [DBSup.__tuple_cov_json(column_names, d) for d in r]
Example #11
0
        def wrapper(*args, **kw):
            # url = ''.join(list(args))
            headers = kw['headers'] if 'headers' in kw else None
            params = kw['params'] if 'params' in kw else None
            body = kw['body'] if 'body' in kw else None

            opt = kw['opt'] if 'opt' in kw else {}

            if 'max_retries' not in opt:
                opt['max_retries'] = 3  # 默认重连3次
            if 'json' not in opt:
                opt['json'] = True  # 默认转换为json
            if 'kw' not in opt:
                opt['kw'] = {
                }  # 请求kw参数包装对象kw={'timeout':1,'proxies': {'http':'xxx'}...}
            # if headers:
            #     opt['kw'].update({'headers': headers})
            # if params:
            #     opt['kw'].update({'params': params})
            # if body:
            #     opt['kw'].update({'body': body})

            if 'timeout' not in opt['kw']:
                opt['kw'].update({'timeout': 10})  # 该请求框架应用于spider

            requests_session = requests.Session()
            requests_session.mount('http://',
                                   HTTPAdapter(max_retries=opt['max_retries']))
            requests_session.mount('https://',
                                   HTTPAdapter(max_retries=opt['max_retries']))
            opt.update({'requests_session': requests_session})
            kw.update({'opt': opt})
            try:
                stime = ctime()
                r = func(*args, **kw)
                if opt.get('log'):  # 是否打印日志
                    i('HTTP %s: %s %s %s毫秒' %
                      (r.request.method, r.request.url, r.status_code,
                       (ctime() - stime)))
                    if headers:
                        i('headers: %s' % headers)
                    if body:
                        i('body: %s' % body)
                    if params:
                        i('params: %s' % params)
                if 'encoding' in opt:
                    r.encoding = opt['encoding']
                if opt.get('original'):  # 返回最原始的
                    return r
                elif opt.get('bare'):  # 默认不裸露响应结果(response包装)
                    return r.json() if opt['json'] else r.text
                elif r.status_code == 200:
                    return response(result=r.json() if opt['json'] else r.text,
                                    is_response=False)  # 默认返回包装值
                else:
                    return response(501,
                                    "service error (501)",
                                    '%s---%s' % (r.status_code, r.text),
                                    is_response=False)
            except Exception as e:
                i(e)
                import traceback
                return response(501,
                                "service error (501-1)",
                                traceback.format_exc(),
                                is_response=False)
Example #12
0
File: db.py Project: zwping/py_libs
 def execute(sql, bind):
     """ 执行sql语句
     """
     i('%s %s' % (sql, 'bind={}'.format(bind) if bind else ''))
     return db.session.execute(
         sql, bind=_binds(bind))  # 目前只有子线程执行sql才会报错,sqlalchemy_ctx会处理子线程异常