<commit_before>from __future__ import unicode_literals

from rest_framework.fields import *
from rest_framework.serializers import ListSerializer

from rest_framework_webdav.serializers import WebDAVResponseSerializer
from rest_framework_webdav.resources import BaseResource
# import all prop class so find_subclasses can find them
from rest_framework_webdav.serializers.props import *
# TODO we need a way to discover all third-party props and import them,
# so find_subclasses can find them.
from rest_framework_webdav.serializers.utils import find_subclasses
from rest_framework_webdav.namespaces import DAVNS

"""
Elements that only appear in WebDAV client requests

Only to_representation() needs to be implemented.
"""

class PropstatSerializer(WebDAVResponseSerializer):
    # TODO XML namespace for all serializers and fields
    """
    <!ELEMENT propstat (prop, status, error?, responsedescription?) >

    Determines each prop's status code, group them by status code,
    to_representation() returns a list of <propstat>, each propstat in the list
    contain the props that are with the same status code.
    """


    def to_representation(self, instance):
        """
        This implementation copies mostly from Serializer.to_representation() ,
        with the required additional feature: group fields (props) into
        propstat dictionaries.

        Sample output:
        {
            'HTTP/1.1 200 OK': [
                {'getlastmodified': 'Sat, 10 Dec 2016 16:31:16 -0000'}
            ]
        }

        TODO use _readable_fields just as in Serializer
        """
        out = {}
        for key, field in self.fields.items():
            try:
                attribute = field.get_attribute(instance) # get_attribute will check source='*'
            except SkipField:
                continue
            if field.status not in out:
                out[field.status] = {DAVNS.prepend('prop'): []}
            out[field.status][DAVNS.prepend('prop')].append({key: field.to_representation(attribute)})

        return out


    def get_fields(self):
        """
        Fields are determined dynamically.
        This will be invoked by @property fields() ,<commit_msg>Prepend XML namespaces for prop<commit_after>from __future__ import unicode_literals

from rest_framework.fields import *
from rest_framework.serializers import ListSerializer

from rest_framework_webdav.serializers import WebDAVResponseSerializer
from rest_framework_webdav.resources import BaseResource
# import all prop class so find_subclasses can find them
from rest_framework_webdav.serializers.props import *
# TODO we need a way to discover all third-party props and import them,
# so find_subclasses can find them.
from rest_framework_webdav.serializers.utils import find_subclasses
from rest_framework_webdav.namespaces import DAVNS

"""
Elements that only appear in WebDAV client requests

Only to_representation() needs to be implemented.
"""

class PropstatSerializer(WebDAVResponseSerializer):
    """
    <!ELEMENT propstat (prop, status, error?, responsedescription?) >

    Determines each prop's status code, group them by status code,
    to_representation() returns a list of <propstat>, each propstat in the list
    contain the props that are with the same status code.
    """


    def to_representation(self, instance):
        """
        This implementation copies mostly from Serializer.to_representation() ,
        with the required additional feature: group fields (props) into
        propstat dictionaries.

        Sample output:
        {
            'HTTP/1.1 200 OK': [
                {'getlastmodified': 'Sat, 10 Dec 2016 16:31:16 -0000'}
            ]
        }

        TODO use _readable_fields just as in Serializer
        """
        out = {}
        for key, field in self.fields.items():
            try:
                attribute = field.get_attribute(instance) # get_attribute will check source='*'
            except SkipField:
                continue
            if field.status not in out:
                out[field.status] = {DAVNS.prepend('prop'): []}
            out[field.status][DAVNS.prepend('prop')].append({DAVNS.prepend(key): field.to_representation(attribute)})

        return out


    def get_fields(self):
        """
        Fields are determined dynamically.
        This will be invoked by @property fields() ,
{'fraction_non_alphanumeric': 0.07483458818161078, 'fraction_numerical': 0.009582477754962354, 'mean_word_length': 2.967420814479638, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2003193', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1101, 'n_words': 390}