예제 #1
0
    def test_else_finally(self):
        m = Merry()
        m.g.except_called = False
        m.g.else_called = False
        m.g.finally_called = False

        @m._except(Exception)
        def catch_all(e):
            pass

        @m._except(ArithmeticError)
        def arith_error(e):
            m.g.except_called = True

        @m._else
        def else_clause():
            m.g.else_called = True

        @m._finally
        def finally_clause():
            m.g.finally_called = True

        @m._try
        def f():
            pass

        f()
        self.assertFalse(m.g.except_called)
        self.assertTrue(m.g.else_called)
        self.assertTrue(m.g.finally_called)
예제 #2
0
    def test_except_finally(self):
        m = Merry()
        m.g.except_called = False
        m.g.else_called = False
        m.g.finally_called = False

        @m._except(ZeroDivisionError)
        def zerodiv(e):
            m.g.except_called = True

        @m._else
        def else_clause():
            m.g.else_called = True

        @m._finally
        def finally_clause():
            m.g.finally_called = True

        @m._try
        def f():
            1 / 0

        f()
        self.assertTrue(m.g.except_called)
        self.assertFalse(m.g.else_called)
        self.assertTrue(m.g.finally_called)
예제 #3
0
    def test_return_prevents_else(self):
        m = Merry()
        m.g.except_called = False
        m.g.else_called = False
        m.g.finally_called = False

        @m._except(ZeroDivisionError)
        def zerodiv(e):
            m.g.except_called = True

        @m._else
        def else_clause():
            m.g.else_called = True

        @m._finally
        def finally_clause():
            m.g.finally_called = True

        @m._try
        def f():
            return 'foo'

        f()
        self.assertFalse(m.g.except_called)
        self.assertFalse(m.g.else_called)
        self.assertTrue(m.g.finally_called)
예제 #4
0
    def test_unhandled(self):
        m = Merry()

        @m._try
        def f():
            1 / 0

        self.assertRaises(ZeroDivisionError, f)
예제 #5
0
    def test_return_value_from_finally2(self):
        m = Merry()

        @m._try
        def f():
            return 'foo'

        @m._finally
        def finally_clause():
            return 'bar'

        self.assertEqual(f(), 'bar')
예제 #6
0
    def test_return_value_from_else(self):
        m = Merry()

        @m._else
        def else_clause():
            return 'foo'

        @m._try
        def f():
            pass

        self.assertEqual(f(), 'foo')
예제 #7
0
    def test_return_value_from_except(self):
        m = Merry()

        @m._except(ZeroDivisionError)
        def zerodiv():
            return 'foo'

        @m._try
        def f():
            1 / 0

        self.assertEqual(f(), 'foo')
예제 #8
0
    def test_return_value_if_no_error(self):
        m = Merry()

        @m._try
        def f():
            return 'foo'

        @m._else
        def else_clause():
            return 'bar'

        self.assertEqual(f(), 'foo')
예제 #9
0
    def test_simple_except(self):
        m = Merry()
        m.g.except_called = False

        @m._except(ZeroDivisionError)
        def zerodiv():
            m.g.except_called = True

        @m._try
        def f():
            1 / 0

        f()
        self.assertTrue(m.g.except_called)
예제 #10
0
    def test_local_debug(self):
        m = Merry()
        m.g.except_called = False

        @m._except(ZeroDivisionError, debug=True)
        def zerodiv():
            m.g.except_called = True

        @m._try
        def f():
            1 / 0

        self.assertRaises(ZeroDivisionError, f)
        self.assertFalse(m.g.except_called)
예제 #11
0
    def test_local_debug_override(self):
        m = Merry(debug=True)
        m.g.except_called = False

        @m._except(ZeroDivisionError, debug=False)
        def zerodiv():
            m.g.except_called = True

        @m._try
        def f():
            1 / 0

        f()
        self.assertTrue(m.g.except_called)
예제 #12
0
    def test_return_value_from_finally3(self):
        m = Merry()

        @m._try
        def f():
            1 / 0

        @m._except(ZeroDivisionError)
        def zerodiv():
            return 'foo'

        @m._finally
        def finally_clause():
            return 'bar'

        self.assertEqual(f(), 'bar')
예제 #13
0
    def test_logger(self):
        m = Merry()
        m.g.except_called = False
        stream = StringIO()
        m.logger.addHandler(logging.StreamHandler(stream))

        @m._except(ZeroDivisionError)
        def zerodiv():
            m.g.except_called = True

        @m._try
        def f():
            1 / 0

        f()
        self.assertIn('Traceback', stream.getvalue())
        self.assertIn('ZeroDivisionError: ', stream.getvalue())
예제 #14
0
    def test_except_parent(self):
        m = Merry()
        m.g.except_called = False

        @m._except(Exception)
        def catch_all(e):
            pass

        @m._except(ArithmeticError)
        def arith_error(e):
            self.assertIsInstance(e, ZeroDivisionError)
            m.g.except_called = True

        @m._try
        def f():
            1 / 0

        f()
        self.assertTrue(m.g.except_called)
예제 #15
0
    def test_custom_logger(self):
        my_logger = logging.getLogger('foo')
        stream = StringIO()
        my_logger.addHandler(logging.StreamHandler(stream))

        m = Merry(logger_name='foo')
        m.g.except_called = False

        @m._except(ZeroDivisionError)
        def zerodiv():
            m.g.except_called = True

        @m._try
        def f():
            1 / 0

        f()
        self.assertIn('Traceback', stream.getvalue())
        self.assertIn('ZeroDivisionError: ', stream.getvalue())
예제 #16
0
from __future__ import unicode_literals

from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import or_

from pfamserver.models import Uniprot, UniprotRegFull, PfamA
from pfamserver.extensions import db
from pfamserver.exceptions import SentryIgnoredError
from merry import Merry

merry = Merry()


class UniprotServiceError(Exception):
    message = ''

    def __init__(self, message):
        super(UniprotServiceError, self).__init__()
        self.message = message


@merry._except(NoResultFound)
def handle_no_result_found(e):
    raise UniprotServiceError('Uniprot doesn' 't exist.')


def query_uniprot(uniprot):
    uniprot = uniprot.upper()
    query = db.session.query(Uniprot)
    query = query.join(Uniprot.pfams)
    query = query.filter(
예제 #17
0
파일: cli.py 프로젝트: GeekLiB/SLAM_Python
import shutil
import sys
import time

import boto3
import botocore
import climax
from lambda_uploader.package import build_package
from merry import Merry
import yaml

from . import plugins
from .cfn import get_cfn_template
from .helpers import render_template

merry = Merry(logger_name='slam', debug='unittest' in sys.modules)
f = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
h = logging.FileHandler('slam_error.log')
h.setFormatter(f)
merry.logger.addHandler(h)


@merry._try
@climax.group()
@climax.argument('--config-file', '-c', default='slam.yaml',
                 help='The slam configuration file. Defaults to slam.yaml.')
def main(config_file):
    return {'config_file': config_file}


@merry._except(RuntimeError, ValueError)
예제 #18
0
        :return: 分类后返回的字典
        """
        if response.get("status", None) == "success":
            return self.success_response(msg=response.get("msg", ""),
                                         data=response.get("data", None))
        else:
            if response.get("code") is not None:
                return self.error_response(msg=response.get("msg", ""),
                                           data=response.get("data", None))
            return self.error_response(msg=response.get("msg", ""),
                                       data=response.get("data", None))


response_route = CustomResponse().response_route

_merry = Merry(logger_name="exception_logger")
MerryExcept = _merry._except
MerryTry = _merry._try


@MerryExcept(Exception)
def all_exception_handler(err):
    logger.error(err)
    return jsonify(response_route({"msg": "请求异常"}))


class InterceptHandler(logging.Handler):
    def emit(self, record):
        logger_opt = logger.opt(depth=6, exception=record.exc_info)
        logger_opt.log(record.levelname, record.getMessage())