<commit_before>import json
from operator import attrgetter

from core.models import PlCoreBase, PlCoreBaseManager, SingletonModel, XOS
from core.models.plcorebase import StrippedCharField
from django.db import models
from xos.exceptions import *
import urlparse

COARSE_KIND = "coarse"

def get_xos():
    xos = XOS.objects.all()

    if xos:
       return xos[0]
    else:
       return None


class AttributeMixin(object):
    # helper for extracting things from a json-encoded
    # service_specific_attribute

    def get_attribute(self, name, default=None):
        if self.service_specific_attribute:
            attributes = json.loads(self.service_specific_attribute)
        else:
            attributes = {}
        return attributes.get(name, default)

    def set_attribute(self, name, value):
        if self.service_specific_attribute:
            attributes = json.loads(self.service_specific_attribute)
        else:
            attributes = {}
        attributes[name] = value
        self.service_specific_attribute = json.dumps(attributes)

    def get_initial_attribute(self, name, default=None):
        if self._initial["service_specific_attribute"]:
            attributes = json.loads(
                self._initial["service_specific_attribute"])
        else:
            attributes = {}
        return attributes.get(name, default)

    @classmethod
    def get_default_attribute(cls, name):
        for (attrname, default) in cls.simple_attributes:
            if attrname == name:
                return default
        if hasattr(cls, "default_attributes"):
            if name in cls.default_attributes:
                return cls.default_attributes[name]

        return None

    @classmethod
    def setup_simple_attributes(cls):
        for (attrname, default) in cls.simple_attributes:
            setattr(cls, attrname, property(lambda self, attrname=attrname, default=default: self.get_attribute(attrname, default),
                                            lambda self, value, attrname=attrname: self.set_attribute(
                                                attrname, value),
                                            None,
                                            attrname))

class ServiceController(PlCoreBase):
    xos = models.ForeignKey(XOS, related_name='service_controllers', help_text="Pointer to XOS", default=get_xos)
    name = StrippedCharField(max_length=30, help_text="Service Name")
    base_url = StrippedCharField(max_length=1024, help_text="Base URL, allows use of relative URLs for resources", null=True, blank=True)

    def __unicode__(self): return u'%s' % (self.name)

class ServiceControllerResource(PlCoreBase):
    KIND_CHOICES = (('models', 'Models'),
                    ('admin', 'Admin'),
                    ('django_library', 'Django Library'),
                    ('synchronizer', 'Synchronizer'),
                    ('rest_service', 'REST API (service)'),
                    ('rest_tenant', 'REST API (tenant)'),
                    ('tosca_custom_types', 'Tosca Custom Types'),
                    ('tosca_resource', 'Tosca Resource'),
                    ('private_key', 'Private Key'),
                    ('public_key', 'Public Key'))

    FORMAT_CHOICES = (('python', 'Python'),<commit_msg>make XOS rebuild if a service controller is changed<commit_after>import json
from operator import attrgetter

from core.models import PlCoreBase, PlCoreBaseManager, SingletonModel, XOS
from core.models.plcorebase import StrippedCharField
from django.db import models
from xos.exceptions import *
import urlparse

COARSE_KIND = "coarse"

def get_xos():
    xos = XOS.objects.all()

    if xos:
       return xos[0]
    else:
       return None


class AttributeMixin(object):
    # helper for extracting things from a json-encoded
    # service_specific_attribute

    def get_attribute(self, name, default=None):
        if self.service_specific_attribute:
            attributes = json.loads(self.service_specific_attribute)
        else:
            attributes = {}
        return attributes.get(name, default)

    def set_attribute(self, name, value):
        if self.service_specific_attribute:
            attributes = json.loads(self.service_specific_attribute)
        else:
            attributes = {}
        attributes[name] = value
        self.service_specific_attribute = json.dumps(attributes)

    def get_initial_attribute(self, name, default=None):
        if self._initial["service_specific_attribute"]:
            attributes = json.loads(
                self._initial["service_specific_attribute"])
        else:
            attributes = {}
        return attributes.get(name, default)

    @classmethod
    def get_default_attribute(cls, name):
        for (attrname, default) in cls.simple_attributes:
            if attrname == name:
                return default
        if hasattr(cls, "default_attributes"):
            if name in cls.default_attributes:
                return cls.default_attributes[name]

        return None

    @classmethod
    def setup_simple_attributes(cls):
        for (attrname, default) in cls.simple_attributes:
            setattr(cls, attrname, property(lambda self, attrname=attrname, default=default: self.get_attribute(attrname, default),
                                            lambda self, value, attrname=attrname: self.set_attribute(
                                                attrname, value),
                                            None,
                                            attrname))

class ServiceController(PlCoreBase):
    xos = models.ForeignKey(XOS, related_name='service_controllers', help_text="Pointer to XOS", default=get_xos)
    name = StrippedCharField(max_length=30, help_text="Service Name")
    base_url = StrippedCharField(max_length=1024, help_text="Base URL, allows use of relative URLs for resources", null=True, blank=True)

    def __unicode__(self): return u'%s' % (self.name)

    def save(self, *args, **kwargs):
       super(ServiceController, self).save(*args, **kwargs)

       if self.xos:
           # force XOS to rebuild
           # XXX somewhat hackish XXX
           self.xos.save(update_fields=["updated"])

class ServiceControllerResource(PlCoreBase):
    KIND_CHOICES = (('models', 'Models'),
                    ('admin', 'Admin'),
                    ('django_library', 'Django Library'),
                    ('synchronizer', 'Synchronizer'),
                    ('rest_service', 'REST API (service)'),
                    ('rest_tenant', 'REST API (tenant)'),
                    ('tosca_custom_types', 'Tosca Custom Types'),
                    ('tosca_resource', 'Tosca Resource'),
                    ('private_key', 'Private Key'),
                    ('public_key', 'Public Key'))

    FORMAT_CHOICES = (('python', 'Python'),
{'fraction_non_alphanumeric': 0.09598246895544194, 'fraction_numerical': 0.0020452885317750183, 'mean_word_length': 2.3200775945683803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1432692', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1764, 'n_words': 439}