<commit_before># Copyright (C) 2014 Linaro Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""A generic response object that handlers can pass along."""

import pymongo.cursor
import types


class HandlerResponse(object):
    """A custom response object that handlers should use to communicate.

    This has to be used to pass custom reason or set custom headers after
    an action has been performed.

    The result of the action must be stored in the object `result` attribute.
    This attribute will always be a list.

    `count` and `limit` should be stored in their own attributes as well.
    By default they are set to None and will not be included in the
    serializable view.

    To send this response on the wire, serialize the object by calling
    `to_dict()` or `repr`. They will return a dictionary view of the object.
    """
    def __init__(self, status_code=200):
        """Create a new HandlerResponse.

        By default the status code is set to 200.
        """
        if not isinstance(status_code, types.IntType):
            raise ValueError("Value must be an integer")

        self._status_code = status_code
        self._reason = None
        self._count = None
        self._limit = None
        self._result = None
        self._headers = None
        self._errors = []

    @property
    def status_code(self):
        """The response status code."""
        return self._status_code

    @status_code.setter
    def status_code(self, value):
        """The response status code.

        :param value: The status code, must be an int.
        """
        if not isinstance(value, types.IntType):
            raise ValueError("Value must be an integer")

        self._status_code = value

    @property
    def reason(self):
        """The response reason."""
        return self._reason

    @reason.setter
    def reason(self, value):
        """The response reason.

        :param value: The reason as string.
        """
        if not isinstance(value, types.StringTypes):
            raise ValueError("Value must be a string")

        self._reason = value

    @property
    def headers(self):
        """The custom headers for this response."""
        return self._headers

    @headers.setter
    def headers(self, value):
        """The headers that should be added to this response.

        :param value: A dictionary with the headers to set.
        """
        if not isinstance(value, types.DictionaryType):
            raise ValueError("Value must be a dictionary")

        self._headers = value

    @property
    def count(self):
        """How many results are included."""
        return self._count

    @count.setter
    def count(self, value):
        """Set the number of results included."""
        if not isinstance(value, types.IntType):
            raise ValueError("Value must be a integer")
        self._count = value

    @property
    def limit(self):
        """The number of results requested."""
        return self._limit

    @limit.setter
    def limit(self, value):
        """Set the number of results requested."""
        if not isinstance(value, types.IntType):
            raise ValueError("Value must be an integer")
        self._limit = value

    @property
    def result(self):
        """The result associated with this response.

        :return A list containing the results or None if specifically set to.
        """
        return self._result

    @result.setter
    def result(self, value):
        """Set the result value.

        It can be set to None to control the `to_dict()` method and its output.
        If set to None, it will not be included in the dictionary view of the
        object.

        All passed values, if not of type list, will be wrapped around a list.
        """
        if value is None:
            self._result = value
        else:
            # The pymongo cursor is an iterable.
            if not isinstance(value, (types.ListType, pymongo.cursor.Cursor)):
                value = [value]
            elif isinstance(value, pymongo.cursor.Cursor):
                value = [r for r in value]
            self._result = value

    @property
    def errors(self):
        """The errors that this response might have."""
        return self._errors

    @errors.setter
    def errors(self, value):
        """Set the errors of this response."""
        if value:
            if isinstance(value, types.ListType):
                self._errors.extend(value)
            else:
                self._errors.append(value)

    def to_dict(self):
        """Create a view of this object as a dictionary.

        The `headers` property is not included.

        :return The object as a dictionary.
        """
        dict_obj = {}

        dict_obj["code"] = self.status_code
        if self.count is not None:
            dict_obj["count"] = self.count

        if self.limit is not None:
            dict_obj["limit"] = self.limit

        if self.result is not None:
            dict_obj["result"] = self.result

        if self.reason is not None:
            dict_obj["reason"] = self.reason

        if self.errors:
            dict_obj["errors"] = self.errors

        return dict_obj

    def __repr__(self):
        return self.to_dict()
<commit_msg>Add support for extra messages in the response.<commit_after># Copyright (C) 2014 Linaro Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""A generic response object that handlers can pass along."""

import pymongo.cursor
import types


class HandlerResponse(object):
    """A custom response object that handlers should use to communicate.

    This has to be used to pass custom reason or set custom headers after
    an action has been performed.

    The result of the action must be stored in the object `result` attribute.
    This attribute will always be a list.

    `count` and `limit` should be stored in their own attributes as well.
    By default they are set to None and will not be included in the
    serializable view.

    To send this response on the wire, serialize the object by calling
    `to_dict()` or `repr`. They will return a dictionary view of the object.
    """
    def __init__(self, status_code=200):
        """Create a new HandlerResponse.

        By default the status code is set to 200.
        """
        if not isinstance(status_code, types.IntType):
            raise ValueError("Value must be an integer")

        self._status_code = status_code
        self._reason = None
        self._count = None
        self._limit = None
        self._result = None
        self._headers = None
        self._errors = []
        self._messages = []

    @property
    def status_code(self):
        """The response status code."""
        return self._status_code

    @status_code.setter
    def status_code(self, value):
        """The response status code.

        :param value: The status code, must be an int.
        """
        if not isinstance(value, types.IntType):
            raise ValueError("Value must be an integer")

        self._status_code = value

    @property
    def reason(self):
        """The response reason."""
        return self._reason

    @reason.setter
    def reason(self, value):
        """The response reason.

        :param value: The reason as string.
        """
        if not isinstance(value, types.StringTypes):
            raise ValueError("Value must be a string")

        self._reason = value

    @property
    def headers(self):
        """The custom headers for this response."""
        return self._headers

    @headers.setter
    def headers(self, value):
        """The headers that should be added to this response.

        :param value: A dictionary with the headers to set.
        """
        if not isinstance(value, types.DictionaryType):
            raise ValueError("Value must be a dictionary")

        self._headers = value

    @property
    def count(self):
        """How many results are included."""
        return self._count

    @count.setter
    def count(self, value):
        """Set the number of results included."""
        if not isinstance(value, types.IntType):
            raise ValueError("Value must be a integer")
        self._count = value

    @property
    def limit(self):
        """The number of results requested."""
        return self._limit

    @limit.setter
    def limit(self, value):
        """Set the number of results requested."""
        if not isinstance(value, types.IntType):
            raise ValueError("Value must be an integer")
        self._limit = value

    @property
    def result(self):
        """The result associated with this response.

        :return A list containing the results or None if specifically set to.
        """
        return self._result

    @result.setter
    def result(self, value):
        """Set the result value.

        It can be set to None to control the `to_dict()` method and its output.
        If set to None, it will not be included in the dictionary view of the
        object.

        All passed values, if not of type list, will be wrapped around a list.
        """
        if value is None:
            self._result = value
        else:
            # The pymongo cursor is an iterable.
            if not isinstance(value, (types.ListType, pymongo.cursor.Cursor)):
                value = [value]
            elif isinstance(value, pymongo.cursor.Cursor):
                value = [r for r in value]
            self._result = value

    @property
    def errors(self):
        """The errors that this response might have."""
        return self._errors

    @errors.setter
    def errors(self, value):
        """Set the errors of this response."""
        if value:
            if isinstance(value, types.ListType):
                self._errors.extend(value)
            else:
                self._errors.append(value)

    @property
    def messages(self):
        """The response messages."""
        return self._messages

    @messages.setter
    def messages(self, value):
        """Set the messages for this response."""
        if value:
            if isinstance(value, types.ListType):
                self._messages.extend(value)
            else:
                self._messages.append(value)

    def to_dict(self):
        """Create a view of this object as a dictionary.

        The `headers` property is not included.

        :return The object as a dictionary.
        """
        dict_obj = {}

        dict_obj["code"] = self.status_code
        if self.count is not None:
            dict_obj["count"] = self.count

        if self.limit is not None:
            dict_obj["limit"] = self.limit

        if self.result is not None:
            dict_obj["result"] = self.result

        if self.reason is not None:
            dict_obj["reason"] = self.reason

        if self.errors:
            dict_obj["errors"] = self.errors

        if self.messages:
            dict_obj["messages"] = self.messages

        return dict_obj

    def __repr__(self):
        return self.to_dict()

{'fraction_non_alphanumeric': 0.08311035433390092, 'fraction_numerical': 0.0017838319954593368, 'mean_word_length': 2.1593237704918034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '737506', 'n_tokens_mistral': 3180, 'n_tokens_neox': 3064, 'n_words': 1324}