<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2011 <NAME>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#

from random import randint
from phoxpy import xml
from phoxpy.mapping import BooleanField, IntegerField, ListField, LongField, \
                           Mapping, RefField, TextField


__all__ = ['Message', 'PhoxRequest', 'PhoxResponse',
           'AuthRequest', 'AuthResponse']

class Message(Mapping):
    """Base communication message mapping."""
    def __str__(self):
        raise NotImplementedError('Should be implemented for each message type')

    def unwrap(self, root=None):
        content = xml.Element('content')
        super(Message, self).unwrap(content)
        if root is not None:
            root.append(content)
            return root
        return content


class PhoxRequest(Message):
    """Base request message.

    :param msgtype: Message type.
    :type msgtype: str

    :param sessionid: Session id number.
    :type sessionid: str

    :param buildnumber: Client build number.
    :type buildnumber: str

    :param version: Server version number.
    :type version: str
    """
    def __init__(self, msgtype, sessionid=None, buildnumber=None, version=None,
                 **data):
        self._type = msgtype
        self._session_id = sessionid
        self._buildnumber = buildnumber
        self._version = version
        super(PhoxRequest, self).__init__(**data)

    def __str__(self):
        doctype = ('phox-request', 'SYSTEM', 'phox.dtd')
        return xml.dump(self.unwrap(), doctype=doctype)

    @property
    def type(self):
        """Request message type."""
        return self._type

    @property
    def sessionid(self):
        """Session id number for related request."""
        return self._session_id

    @property
    def buildnumber(self):
        return self._buildnumber

    @property
    def version(self):
        return self._version

    @classmethod
    def wrap(cls, xmlsrc, **defaults):
        return super(PhoxRequest, cls).wrap(xmlsrc.find('content'), **defaults)

    def unwrap(self):
        root = xml.Element('phox-request', type=self.type)
        if self.sessionid is not None:
            root.attrib['sessionid'] = self.sessionid
        if self.buildnumber is not None:
            root.attrib['buildnumber'] = self.buildnumber
        if self.version is not None:
            root.attrib['version'] = self.version
        return super(PhoxRequest, self).unwrap(root)


class PhoxResponse(Message):
    """Base phox response message. Used as answer on phox requests messages.

    :param sessionid: Session id.
    :type sessionid: str

    :param buildnumber: Build number.
    :type buildnumber: str
    """
    def __init__(self, sessionid=None, buildnumber=None, **data):
        self._session_id = sessionid
        self._buildnumber = buildnumber
        super(PhoxResponse, self).__init__(**data)

    def __str__(self):
        doctype = ('phox-response', 'SYSTEM', 'phox.dtd')
        return xml.dump(self.unwrap(), doctype=doctype)

    @property
    def sessionid(self):
        """Session id number for related response."""
        return self._session_id

    @property
    def buildnumber(self):
        return self._buildnumber

    @classmethod
    def wrap(cls, xmlsrc):
        root = xmlsrc.find('content/o')
        assert root is not None, xml.dump(xmlsrc)
        attrs = dict(xmlsrc.attrib.items())
        return super(PhoxResponse, cls).wrap(root, **attrs)

    def unwrap(self):
        root = xml.Element('phox-response')
        if self.sessionid is not None:
            root.attrib['sessionid'] = self.sessionid
        if self.buildnumber is not None:
            root.attrib['buildnumber'] = self.buildnumber
        content =  super(PhoxResponse, self).unwrap()
        if len(content):
            content.tag = 'o' # content => o
            content, obj = xml.Element('content'), content
            content.append(obj)
        root.append(content)
        return root


class AuthRequest(PhoxRequest):
    """Authentication request message.<commit_msg>add fallback for different xml structure<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2011 <NAME>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#

from random import randint
from phoxpy import xml
from phoxpy.mapping import BooleanField, IntegerField, ListField, LongField, \
                           Mapping, RefField, TextField


__all__ = ['Message', 'PhoxRequest', 'PhoxResponse',
           'AuthRequest', 'AuthResponse']

class Message(Mapping):
    """Base communication message mapping."""
    def __str__(self):
        raise NotImplementedError('Should be implemented for each message type')

    def unwrap(self, root=None):
        content = xml.Element('content')
        super(Message, self).unwrap(content)
        if root is not None:
            root.append(content)
            return root
        return content


class PhoxRequest(Message):
    """Base request message.

    :param msgtype: Message type.
    :type msgtype: str

    :param sessionid: Session id number.
    :type sessionid: str

    :param buildnumber: Client build number.
    :type buildnumber: str

    :param version: Server version number.
    :type version: str
    """
    def __init__(self, msgtype, sessionid=None, buildnumber=None, version=None,
                 **data):
        self._type = msgtype
        self._session_id = sessionid
        self._buildnumber = buildnumber
        self._version = version
        super(PhoxRequest, self).__init__(**data)

    def __str__(self):
        doctype = ('phox-request', 'SYSTEM', 'phox.dtd')
        return xml.dump(self.unwrap(), doctype=doctype)

    @property
    def type(self):
        """Request message type."""
        return self._type

    @property
    def sessionid(self):
        """Session id number for related request."""
        return self._session_id

    @property
    def buildnumber(self):
        return self._buildnumber

    @property
    def version(self):
        return self._version

    @classmethod
    def wrap(cls, xmlsrc, **defaults):
        return super(PhoxRequest, cls).wrap(xmlsrc.find('content'), **defaults)

    def unwrap(self):
        root = xml.Element('phox-request', type=self.type)
        if self.sessionid is not None:
            root.attrib['sessionid'] = self.sessionid
        if self.buildnumber is not None:
            root.attrib['buildnumber'] = self.buildnumber
        if self.version is not None:
            root.attrib['version'] = self.version
        return super(PhoxRequest, self).unwrap(root)


class PhoxResponse(Message):
    """Base phox response message. Used as answer on phox requests messages.

    :param sessionid: Session id.
    :type sessionid: str

    :param buildnumber: Build number.
    :type buildnumber: str
    """
    def __init__(self, sessionid=None, buildnumber=None, **data):
        self._session_id = sessionid
        self._buildnumber = buildnumber
        super(PhoxResponse, self).__init__(**data)

    def __str__(self):
        doctype = ('phox-response', 'SYSTEM', 'phox.dtd')
        return xml.dump(self.unwrap(), doctype=doctype)

    @property
    def sessionid(self):
        """Session id number for related response."""
        return self._session_id

    @property
    def buildnumber(self):
        return self._buildnumber

    @classmethod
    def wrap(cls, xmlsrc):
        root = xmlsrc.find('content/o')
        if root is None:
            root = xmlsrc.find('content')
        assert root is not None, xml.dump(xmlsrc)
        attrs = dict(xmlsrc.attrib.items())
        return super(PhoxResponse, cls).wrap(root, **attrs)

    def unwrap(self):
        root = xml.Element('phox-response')
        if self.sessionid is not None:
            root.attrib['sessionid'] = self.sessionid
        if self.buildnumber is not None:
            root.attrib['buildnumber'] = self.buildnumber
        content =  super(PhoxResponse, self).unwrap()
        if len(content):
            content.tag = 'o' # content => o
            content, obj = xml.Element('content'), content
            content.append(obj)
        root.append(content)
        return root


class AuthRequest(PhoxRequest):
    """Authentication request message.
{'fraction_non_alphanumeric': 0.10801598495533615, 'fraction_numerical': 0.0011753643629525152, 'mean_word_length': 2.5812289562289563, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, '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': '763508', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2337, 'n_words': 684}