<commit_before>from django.conf import settings
from django.db.models import (
    SubfieldBase,
    Field
)
from django.utils.translation import get_language

from lxml import objectify, etree

from multilingualfield import LANGUAGES
from multilingualfield.forms import (
    MultiLingualTextFieldForm,
    MultiLingualCharFieldForm
)

if 'south' in settings.INSTALLED_APPS:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules(
        [],
        [
            "^multilingualfield\.fields\.MultiLingualTextField",
            "^multilingualfield\.fields\.MultiLingualCharField",
        ]
    )

class MultiLingualText(object):
    """A class that aggregates manually-written translations for the same piece of text."""

    def __init__(self, xml=None):
        """
        `xml` : An optional block of XML formatted like this:
        <languages>
            <language code="en">
                Hello
            </language>
            <language code="es">
                Hola
            </language>
        </languages>

        If the above block of XML was passed (as `xml`) to an instance of MultiLingualText
        that instance would have two attributes:
        `en` with a value of 'Hello'
        `es` with a value of 'Hola' 

        If `xml` is not passed to a MultiLingualText instance an attribute for each
        language in settings.LANGUAGES will be built
        """
        self.languages = LANGUAGES
        # Converting XML (passed-in as `xml`) to a python object via lxml
        if xml:
            # If xml is passed in, build out attributes accordingly.
            try:
                xml_as_python_object = objectify.fromstring(xml)
            except etree.XMLSyntaxError:
                raise Exception("Invalid XML was passed to MultiLingualText")
            else:
                # Creating a dictionary of all the languages passed in the value XML
                # with the language code (i.e. 'en', 'de', 'fr') as the key
                language_text_as_dict = {}
                try:
                    for language in xml_as_python_object.language:
                        if language.text:
                            language_text = unicode(language.text)
                        else:
                            language_text = ''
                        language_text_as_dict[unicode(language.get('code'))] = language_text
                except AttributeError:
                    # Empty fields throw-off lxml and cause an AttributeError
                    pass
                for language_code, language_verbose in LANGUAGES:
                    if language_code in language_text_as_dict:
                        text = language_text_as_dict[language_code]
                    else:
                        text = ""

                    setattr(self, language_code, text)
        else:
            for language_code, language_verbose in LANGUAGES:
                setattr(self, language_code, "")

    def __repr__(self):
        default_language_code = get_language()
        return getattr(self, default_language_code)

class MultiLingualTextField(Field):
    """
    A django TextField for storing multiple manually-written
    translations of the same piece of text.
    """
    description = "Stores multiple manually-written translations of the same piece of text."

    __metaclass__ = SubfieldBase
    
    def __init__(self, *args, **kwargs):
        self.individual_widget_max_length = kwargs.get('max_length', None)
        if self.individual_widget_max_length:
            # Removing max_length so syncdb/south don't make a DB column
            # that's too small for future language additions
            del kwargs['max_length'] 
        super(MultiLingualTextField, self).__init__(*args, **kwargs)

    def db_type(self, connection):
        return 'text'

    def to_python(self, value):
        if isinstance(value, MultiLingualText):
            return value
        else:
            return MultiLingualText(value)

    def get_prep_value(self, value):
        """
        Compresses an instance of MultiLingualText into XML in the following format:
        <languages>
            <language code="en">
                Hello
            </language>
            <language code="es">
                Hola
            </language>
        </languages>
        """
        if isinstance(value, MultiLingualText):
            xml_to_return = etree.Element("languages")
            for language_code, language_verbose in LANGUAGES:
                language = etree.Element("language", code=language_code)
                language.text = getattr(value, language_code)
                xml_to_return.append(language)

            return etree.tostring(xml_to_return)
        else:
            try:
                xml_as_python_object = objectify.fromstring(value)
            except etree.XMLSyntaxError:
                raise Exception("""Multi Lingual field instances must be created with either an instance of `multilingualfield.fields.MultiLingualText` or a block of XML in the following format:
<languages>
    <language code="en">
        Hello
    </language>
    <language code="es">
        Hola
    </language>
</languages> 
""")
            else:
                return value

    def formfield(self, **kwargs):
        # This is a fairly standard way to set up some defaults
        # while letting the caller override them.
        defaults = {
            'form_class': MultiLingualTextFieldForm,
            'individual_widget_max_length':self.individual_widget_max_length
        }
        defaults.update(kwargs)
        return super(MultiLingualTextField, self).formfield(**defaults)

class MultiLingualCharField(MultiLingualTextField):
    """
    A django CharField for storing multiple manually-written
    translations of the same piece of text.
    """
    def formfield(self, **kwargs):
        # This is a fairly standard way to set up some defaults
        # while letting the caller override them.
        defaults = {
            'form_class': MultiLingualCharFieldForm,
            'individual_widget_max_length':self.individual_widget_max_length
        }
        defaults.update(kwargs)
        return super(MultiLingualCharField, self).formfield(**defaults)<commit_msg>Removing unnecessary variable assignment.<commit_after>from django.conf import settings
from django.db.models import (
    SubfieldBase,
    Field
)
from django.utils.translation import get_language

from lxml import objectify, etree

from multilingualfield import LANGUAGES
from multilingualfield.forms import (
    MultiLingualTextFieldForm,
    MultiLingualCharFieldForm
)

if 'south' in settings.INSTALLED_APPS:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules(
        [],
        [
            "^multilingualfield\.fields\.MultiLingualTextField",
            "^multilingualfield\.fields\.MultiLingualCharField",
        ]
    )

class MultiLingualText(object):
    """A class that aggregates manually-written translations for the same piece of text."""

    def __init__(self, xml=None):
        """
        `xml` : An optional block of XML formatted like this:
        <languages>
            <language code="en">
                Hello
            </language>
            <language code="es">
                Hola
            </language>
        </languages>

        If the above block of XML was passed (as `xml`) to an instance of MultiLingualText
        that instance would have two attributes:
        `en` with a value of 'Hello'
        `es` with a value of 'Hola' 

        If `xml` is not passed to a MultiLingualText instance an attribute for each
        language in settings.LANGUAGES will be built
        """
        self.languages = LANGUAGES
        # Converting XML (passed-in as `xml`) to a python object via lxml
        if xml:
            # If xml is passed in, build out attributes accordingly.
            try:
                xml_as_python_object = objectify.fromstring(xml)
            except etree.XMLSyntaxError:
                raise Exception("Invalid XML was passed to MultiLingualText")
            else:
                # Creating a dictionary of all the languages passed in the value XML
                # with the language code (i.e. 'en', 'de', 'fr') as the key
                language_text_as_dict = {}
                try:
                    for language in xml_as_python_object.language:
                        if language.text:
                            language_text = unicode(language.text)
                        else:
                            language_text = ''
                        language_text_as_dict[unicode(language.get('code'))] = language_text
                except AttributeError:
                    # Empty fields throw-off lxml and cause an AttributeError
                    pass
                for language_code, language_verbose in LANGUAGES:
                    if language_code in language_text_as_dict:
                        text = language_text_as_dict[language_code]
                    else:
                        text = ""

                    setattr(self, language_code, text)
        else:
            for language_code, language_verbose in LANGUAGES:
                setattr(self, language_code, "")

    def __repr__(self):
        return getattr(self, get_language())

class MultiLingualTextField(Field):
    """
    A django TextField for storing multiple manually-written
    translations of the same piece of text.
    """
    description = "Stores multiple manually-written translations of the same piece of text."

    __metaclass__ = SubfieldBase
    
    def __init__(self, *args, **kwargs):
        self.individual_widget_max_length = kwargs.get('max_length', None)
        if self.individual_widget_max_length:
            # Removing max_length so syncdb/south don't make a DB column
            # that's too small for future language additions
            del kwargs['max_length'] 
        super(MultiLingualTextField, self).__init__(*args, **kwargs)

    def db_type(self, connection):
        return 'text'

    def to_python(self, value):
        if isinstance(value, MultiLingualText):
            return value
        else:
            return MultiLingualText(value)

    def get_prep_value(self, value):
        """
        Compresses an instance of MultiLingualText into XML in the following format:
        <languages>
            <language code="en">
                Hello
            </language>
            <language code="es">
                Hola
            </language>
        </languages>
        """
        if isinstance(value, MultiLingualText):
            xml_to_return = etree.Element("languages")
            for language_code, language_verbose in LANGUAGES:
                language = etree.Element("language", code=language_code)
                language.text = getattr(value, language_code)
                xml_to_return.append(language)

            return etree.tostring(xml_to_return)
        else:
            try:
                xml_as_python_object = objectify.fromstring(value)
            except etree.XMLSyntaxError:
                raise Exception("""Multi Lingual field instances must be created with either an instance of `multilingualfield.fields.MultiLingualText` or a block of XML in the following format:
<languages>
    <language code="en">
        Hello
    </language>
    <language code="es">
        Hola
    </language>
</languages> 
""")
            else:
                return value

    def formfield(self, **kwargs):
        # This is a fairly standard way to set up some defaults
        # while letting the caller override them.
        defaults = {
            'form_class': MultiLingualTextFieldForm,
            'individual_widget_max_length':self.individual_widget_max_length
        }
        defaults.update(kwargs)
        return super(MultiLingualTextField, self).formfield(**defaults)

class MultiLingualCharField(MultiLingualTextField):
    """
    A django CharField for storing multiple manually-written
    translations of the same piece of text.
    """
    def formfield(self, **kwargs):
        # This is a fairly standard way to set up some defaults
        # while letting the caller override them.
        defaults = {
            'form_class': MultiLingualCharFieldForm,
            'individual_widget_max_length':self.individual_widget_max_length
        }
        defaults.update(kwargs)
        return super(MultiLingualCharField, self).formfield(**defaults)
{'fraction_non_alphanumeric': 0.07240393775801841, 'fraction_numerical': 0.0, 'mean_word_length': 2.0368852459016393, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, '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': '940026', 'n_tokens_mistral': 3345, 'n_tokens_neox': 3161, 'n_words': 948}