<commit_before>from .data import get_codevalue
from .status import (TestResultStatus, TestCycleStatus, TestRunStatus,
                     UserStatus, TestCaseStatus, TestSuiteStatus,
                     ApprovalStatus)
from ..core.fields import Field



STATUS_ENUMS_BY_KEY = {
    "APPROVALSTATUS": ApprovalStatus,
    "TESTCASESTATUS": TestCaseStatus,
    "TESTSUITESTATUS": TestSuiteStatus,
    "TESTCYCLESTATUS": TestCycleStatus,
    "TESTRUNSTATUS": TestRunStatus,
    "USERSTATUS": UserStatus,
    "TESTRUNRESULTSTATUS": TestResultStatus,
    }


class StatusValue(object):
    """
    A wrapper for Enum status values that provides nice syntactic sugar for
    checking the status. Accessing any attribute on a StatusValue with a name
    matching a valid possible state will return True if that is the actual
    state; false otherwise.

    """
    def __init__(self, status):
        self.status = status
        self.possible_states = set(s.enumname for s in status.enumclass)


    def __getattr__(self, attr):
        if attr in self.possible_states:
            return attr == self.status.enumname
        raise AttributeError("%r object has no attribute %r"
                             % (self.__class__.__name__, attr))


    def __str__(self):
        return self.status.enumname


    def __repr__(self):
        return "<StatusValue: %s>" % repr(self.status)



class StaticData(Field):
    """
    A Field whose value is a key to a staticData lookup table. Attribute access
    returns the full CodeValue instance (with id, description, and sortOrder
    attributes.)

    """
    def __init__(self, key, api_name=None, default=None, api_submit_name=None):
        self.key = key
        self.states = STATUS_ENUMS_BY_KEY.get(self.key)
        super(StaticData, self).__init__(api_name, default, api_submit_name)


    def install(self, attrname, cls):
        auto_api_name = (self.api_name is None)
        auto_submit_name = (self.api_submit_name is None)

        super(StaticData, self).install(attrname, cls)

        if auto_api_name and not self.api_name.endswith("Id"):
            self.api_name = "%sId" % self.api_name
        if self.api_filter_name and not self.api_filter_name.endswith("Id"):
            self.api_filter_name = "%sId" % self.api_filter_name
        if auto_submit_name and not self.api_submit_name.endswith("Id"):
            self.api_submit_name = "%sId" % self.api_submit_name


    def __get__(self, obj, cls):
        if obj is None:
            return self

        data = super(StaticData, self).__get__(obj, cls)

        code = get_codevalue(self.key, data)
        if code:
            if self.states is not None:
                return StatusValue(self.states[code.id])
            return code
        return data


    def __set__(self, obj, value):
        try:
            value = value.id
        except AttributeError:
            pass
        super(StaticData, self).__set__(obj, value)


    def encode(self, value):
        if isinstance(value, StatusValue):
            value = value.status
        try:
            return str(int(value))
        except ValueError:
            pass
        return value<commit_msg>Fixed bug in StaticData field.<commit_after>from .data import get_codevalue
from .models import CodeValue
from .status import (TestResultStatus, TestCycleStatus, TestRunStatus,
                     UserStatus, TestCaseStatus, TestSuiteStatus,
                     ApprovalStatus)
from ..core.fields import Field



STATUS_ENUMS_BY_KEY = {
    "APPROVALSTATUS": ApprovalStatus,
    "TESTCASESTATUS": TestCaseStatus,
    "TESTSUITESTATUS": TestSuiteStatus,
    "TESTCYCLESTATUS": TestCycleStatus,
    "TESTRUNSTATUS": TestRunStatus,
    "USERSTATUS": UserStatus,
    "TESTRUNRESULTSTATUS": TestResultStatus,
    }


class StatusValue(object):
    """
    A wrapper for Enum status values that provides nice syntactic sugar for
    checking the status. Accessing any attribute on a StatusValue with a name
    matching a valid possible state will return True if that is the actual
    state; false otherwise.

    """
    def __init__(self, status):
        self.status = status
        self.possible_states = set(s.enumname for s in status.enumclass)


    def __getattr__(self, attr):
        if attr in self.possible_states:
            return attr == self.status.enumname
        raise AttributeError("%r object has no attribute %r"
                             % (self.__class__.__name__, attr))


    def __str__(self):
        return self.status.enumname


    def __repr__(self):
        return "<StatusValue: %s>" % repr(self.status)



class StaticData(Field):
    """
    A Field whose value is a key to a staticData lookup table. Attribute access
    returns the full CodeValue instance (with id, description, and sortOrder
    attributes.)

    """
    def __init__(self, key, api_name=None, default=None, api_submit_name=None):
        self.key = key
        self.states = STATUS_ENUMS_BY_KEY.get(self.key)
        super(StaticData, self).__init__(api_name, default, api_submit_name)


    def install(self, attrname, cls):
        auto_api_name = (self.api_name is None)
        auto_submit_name = (self.api_submit_name is None)

        super(StaticData, self).install(attrname, cls)

        if auto_api_name and not self.api_name.endswith("Id"):
            self.api_name = "%sId" % self.api_name
        if self.api_filter_name and not self.api_filter_name.endswith("Id"):
            self.api_filter_name = "%sId" % self.api_filter_name
        if auto_submit_name and not self.api_submit_name.endswith("Id"):
            self.api_submit_name = "%sId" % self.api_submit_name


    def __get__(self, obj, cls):
        if obj is None:
            return self

        data = super(StaticData, self).__get__(obj, cls)

        code = get_codevalue(self.key, data)
        if code:
            if self.states is not None:
                return StatusValue(self.states[code.id])
            return code
        return data


    def __set__(self, obj, value):
        try:
            value = value.id
        except AttributeError:
            pass
        super(StaticData, self).__set__(obj, value)


    def encode(self, value):
        if isinstance(value, StatusValue):
            value = value.status
        elif isinstance(value, CodeValue):
            value = value.id
        try:
            return str(int(value))
        except (TypeError, ValueError):
            pass
        return value
{'fraction_non_alphanumeric': 0.08620159803318991, 'fraction_numerical': 0.0, 'mean_word_length': 2.478888295029396, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, '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': '4007549', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1837, 'n_words': 521}