<commit_before>from django.conf.urls.defaults import *
from lingcod.common.utils import get_logger
from lingcod.common.utils import get_class
from django.template.defaultfilters import slugify
from django.template import loader, TemplateDoesNotExist
from lingcod.features.forms import FeatureForm
from django.core.urlresolvers import reverse


logger = get_logger()

class FeatureConfigurationError(Exception):
    pass

class FeatureConfig:
    """Represents properties of Feature Classes derived from both defaults and
    developer-specified options within the Config inner-class. These 
    properties drive the features of the spatial content managment system, 
    such as CRUD operations, copy, sharing, etc.
    """
    def __init__(self, model):
        
        # Import down here to avoid circular reference
        from lingcod.features.models import Feature        
        
        if not issubclass(model, Feature):
            raise FeatureConfigurationError('Is not a subclass of \
lingcod.features.models.Feature')
        
        self._model = model
        name = model.__name__
        
        if not getattr(model, 'Config', False):
            raise FeatureConfigurationError(
                'Have not defined Config inner-class on registered feature \
class %s' % (name, ))
        
        self._config = model.Config
    
        if not hasattr(self._config, 'form'):
            raise FeatureConfigurationError(
                "Feature class %s is not configured with a form class. \
To specify, add a `form` property to its Config inner-class." % (name,))
    
        if not isinstance(self._config.form, str):
            raise FeatureConfigurationError(
                "Feature class %s is configured with a form property that is \
not a string path." % (name,))
                
        self.form = self._config.form
        self.slug = slugify(name)
        self.verbose_name = getattr(self._config, 'verbose_name', name)
        self.form_template = getattr(self._config, 'form_template', 
            'rest/form.html')
        self.form_context = getattr(self._config, 'form_context', {})
        self.show_context = getattr(self._config, 'show_context', {})
    
    def get_show_template(self):
        """If the user has specified a show_template, grab that template.
        Otherwise use the convention '{{model_slug}}/show.html. If a template
        doesn't exist at either of those paths, returns the 'rest/show.html'
        template.
        """
        template = getattr(self._config, 'show_template', 
            '%s/show.html' % (self.slug, ))
        try:
            t = loader.get_template(template)
        except TemplateDoesNotExist:
            t = loader.get_template('rest/show.html')
        return t
    
    def get_form_class(self):
        """Return the form class specified in the model configuration."""
        try:
            klass = get_class(self.form)
        except:
            raise FeatureConfigurationError(
                "Feature class %s is not configured with a valid form class. \
Could not import %s." % (self._model.__name__, self.form))

        if not issubclass(klass, FeatureForm):
            raise FeatureConfigurationError(
                "Feature class %s's form is not a subclass of \
lingcod.features.forms.FeatureForm." % (self._model.__name__, ))
        return klass
    
    def json(self):
        """Returns a json representation of this feature class configuration
        that can be used to specify client behavior
        """
        pass
        
    def get_create_form(self):
        return reverse('%s_create_form' % (self.slug, ))
    
    def get_update_form(self, pk):
        return reverse('%s_update_form' % (self.slug, ), args=[pk])
    
    def get_resource(self, pk):
        return reverse('%s_resource' % (self.slug, ), args=[pk])

registered_models = []

def register(*args):
    for model in args:
        model.get_config()
        logger.debug('registering %s' % (model.__name__,) )
        if model not in registered_models:
            registered_models.append(model)<commit_msg>Added more documentation for FeatureConfig methods<commit_after>from django.conf.urls.defaults import *
from lingcod.common.utils import get_logger
from lingcod.common.utils import get_class
from django.template.defaultfilters import slugify
from django.template import loader, TemplateDoesNotExist
from lingcod.features.forms import FeatureForm
from django.core.urlresolvers import reverse


logger = get_logger()

class FeatureConfigurationError(Exception):
    pass

class FeatureConfig:
    """
    Represents properties of Feature Classes derived from both defaults and
    developer-specified options within the Config inner-class. These 
    properties drive the features of the spatial content managment system, 
    such as CRUD operations, copy, sharing, etc.
    """
    def __init__(self, model):
        
        # Import down here to avoid circular reference
        from lingcod.features.models import Feature        
        
        if not issubclass(model, Feature):
            raise FeatureConfigurationError('Is not a subclass of \
lingcod.features.models.Feature')
        
        self._model = model
        name = model.__name__
        
        if not getattr(model, 'Config', False):
            raise FeatureConfigurationError(
                'Have not defined Config inner-class on registered feature \
class %s' % (name, ))
        
        self._config = model.Config
    
        if not hasattr(self._config, 'form'):
            raise FeatureConfigurationError(
                "Feature class %s is not configured with a form class. \
To specify, add a `form` property to its Config inner-class." % (name,))
    
        if not isinstance(self._config.form, str):
            raise FeatureConfigurationError(
                "Feature class %s is configured with a form property that is \
not a string path." % (name,))
                
        self.form = self._config.form
        self.slug = slugify(name)
        self.verbose_name = getattr(self._config, 'verbose_name', name)
        self.form_template = getattr(self._config, 'form_template', 
            'rest/form.html')
        self.form_context = getattr(self._config, 'form_context', {})
        self.show_context = getattr(self._config, 'show_context', {})
    
    def get_show_template(self):
        """
        Returns the template used to render this Feature Class' attributes
        """
        # Grab a template specified in the Config object, or use the default
        template = getattr(self._config, 'show_template', 
            '%s/show.html' % (self.slug, ))
        try:
            t = loader.get_template(template)
        except TemplateDoesNotExist:
            # If a template has not been created, use a stub that displays
            # some documentation on how to override the default template
            t = loader.get_template('rest/show.html')
        return t
    
    def get_form_class(self):
        """
        Returns the form class for this Feature Class.
        """
        try:
            klass = get_class(self.form)
        except:
            raise FeatureConfigurationError(
                "Feature class %s is not configured with a valid form class. \
Could not import %s." % (self._model.__name__, self.form))

        if not issubclass(klass, FeatureForm):
            raise FeatureConfigurationError(
                "Feature class %s's form is not a subclass of \
lingcod.features.forms.FeatureForm." % (self._model.__name__, ))

        return klass
    
    def json(self):
        """
        Returns a json representation of this feature class configuration
        that can be used to specify client behavior
        """
        pass
        
    def get_create_form(self):
        """
        Returns the path to a form for creating new instances of this model
        """
        return reverse('%s_create_form' % (self.slug, ))
    
    def get_update_form(self, pk):
        """
        Given a primary key, returns the path to a form for updating a Feature
        Class
        """
        return reverse('%s_update_form' % (self.slug, ), args=[pk])
    
    def get_resource(self, pk):
        """
        Returns the primary url for a feature. This url supports GET, POST, 
        and DELETE operations.
        """
        return reverse('%s_resource' % (self.slug, ), args=[pk])

registered_models = []

def register(*args):
    for model in args:
        model.get_config()
        logger.debug('registering %s' % (model.__name__,) )
        if model not in registered_models:
            registered_models.append(model)
{'fraction_non_alphanumeric': 0.08673645461865868, 'fraction_numerical': 0.0, 'mean_word_length': 2.3606494008504058, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5602246', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2235, 'n_words': 738}