<commit_before>import inspect

from . import fields
from .utils import fn_name_to_pretty_description


class BaseActions(object):
    """ Classes that hold a collection of actions to use with the rules
    engine should inherit from this.
    """
    @classmethod
    def get_all_actions(cls):
        methods = inspect.getmembers(cls)
        return [{'name': m[0],
                 'description': m[1].description,
                 'params': m[1].params
                } for m in methods if getattr(m[1], 'is_rule_action', False)]

def rule_action(description=None, params=None):
    """ Decorator to make a function into a rule action
    """
    def wrapper(func):
        # Verify field name is valid
        valid_fields = [getattr(fields, f) for f in dir(fields) \
                if f.startswith("FIELD_")]
        for param_name, field_type in params.items():
            if param_name not in func.func_code.co_varnames:
                raise AssertionError("Unknown parameter name {0} specified for action {1}".format(param_name, func.__name__))

            if field_type not in valid_fields:
                raise AssertionError("Unknown field type {0} specified for"\
                        " action {1} param {2}".format(
                        field_type, func.__name__, param_name))

        func.is_rule_action = True
        func.description = description \
                or fn_name_to_pretty_description(func.__name__)
        func.params = params
        return func
    return wrapper
<commit_msg>Change func_code to __code__ to work with python 3<commit_after>import inspect

from . import fields
from .utils import fn_name_to_pretty_description


class BaseActions(object):
    """ Classes that hold a collection of actions to use with the rules
    engine should inherit from this.
    """
    @classmethod
    def get_all_actions(cls):
        methods = inspect.getmembers(cls)
        return [{'name': m[0],
                 'description': m[1].description,
                 'params': m[1].params
                } for m in methods if getattr(m[1], 'is_rule_action', False)]

def rule_action(description=None, params=None):
    """ Decorator to make a function into a rule action
    """
    def wrapper(func):
        # Verify field name is valid
        valid_fields = [getattr(fields, f) for f in dir(fields) \
                if f.startswith("FIELD_")]
        for param_name, field_type in params.items():
            if param_name not in func.__code__.co_varnames:
                raise AssertionError("Unknown parameter name {0} specified for action {1}".format(param_name, func.__name__))

            if field_type not in valid_fields:
                raise AssertionError("Unknown field type {0} specified for"\
                        " action {1} param {2}".format(
                        field_type, func.__name__, param_name))

        func.is_rule_action = True
        func.description = description \
                or fn_name_to_pretty_description(func.__name__)
        func.params = params
        return func
    return wrapper

{'fraction_non_alphanumeric': 0.08760545100584036, 'fraction_numerical': 0.006164828033744322, 'mean_word_length': 2.297326203208556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1428590', 'n_tokens_mistral': 840, 'n_tokens_neox': 816, 'n_words': 262}