utils.py 1.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (C) 2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15 16 17
from functools import wraps
import webob.exc
import config
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34


def patch_class_methods(cls, function_map):
    """Replace the methods of a class with different ones
    :param cls: the class to patch its methods
    :param function_map: {'class_method_name': replace_method, ...}
    """
    for method_name, new_method in function_map.items():
        setattr(cls, method_name, new_method)


def empty_list_200(cls, req):
    """return OK 200 and empty list"""
    req.environ['service_type'] = 'mock'
    req.environ['method_name'] = 'empty_list'
    response = req.get_response(cls.app)
    return cls.get_from_response(response, 'empty list', [])
35 36 37 38 39 40 41 42 43 44 45 46 47


def check_activation(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        disabled_methods = getattr(config, 'DISABLED_METHODS', None)
        func_full_name = func.__module__ + '.' + func.__name__
        if disabled_methods and func_full_name in disabled_methods:
            raise webob.exc.HTTPNotImplemented(
                explanation="Method: {0} is disabled".
                format(func_full_name))
        return func(*args, **kwargs)
    return wrapper