<commit_before>import os
import functools
from copy import copy
from bisect import bisect
from contextlib import contextmanager
from importlib import import_module

from gitmodel import exceptions
from gitmodel import fields
from gitmodel import utils


class GitModelOptions(object):
    """
    An options class for ``GitModel``.
    """
    # attributes that can be overridden in a model's options ("Meta" class)
    meta_opts = ('abstract', 'id_attr', 'get_data_path')
    reserved = ('oid',)

    def __init__(self, meta, workspace):
        self.meta = meta
        self.abstract = False
        self.local_fields = []
        self.local_many_to_many = []
        self.model_name = None
        self.parents = []
        self.id_attr = None
        self.workspace = workspace
        self._serializer = None

    @property
    def serializer(self):
        if self._serializer is None:
            from gitmodel.conf import defaults
            default_serializer = defaults.DEFAULT_SERIALIZER
            if self.workspace is not None:
                default_serializer = self.workspace.config.DEFAULT_SERIALIZER
            self._serializer = import_module(default_serializer)
        return self._serializer

    def contribute_to_class(self, cls, name):
        cls._meta = self

        # Default values for these options
        self.model_name = cls.__name__

        # Apply overrides from Meta
        if self.meta:
            meta_attrs = self.meta.__dict__.copy()
            # Ignore private attributes
            for name in self.meta.__dict__:
                if name.startswith('_'):
                    del meta_attrs[name]

            override_attrs = [a for a in self.meta_opts if a in meta_attrs]
            for attr_name in override_attrs:
                value = meta_attrs.pop(attr_name)
                # if attr is a function, bind it to this instance
                if not isinstance(value, type) and hasattr(value, '__call__'):
                    value = value.__get__(self, self.__class__)
                setattr(self, attr_name, value)
        self._declared_meta = self.meta
        del self.meta

    def get_data_path(self, object_id):
        """
        Default method for building the path name for a given id.

        This is used by a model instance's get_data_path() method, which simply
        passes the instance id.
        """
        model_name = self.model_name.lower()
        return os.path.join(model_name, unicode(object_id), 'data.json')

    def add_field(self, field):
        """ Insert a field into the fields list in correct order """
        if field.name in self.reserved:
            raise exceptions.FieldError("{} is a reserved name and cannot be"
                                        "used as a field name.")
        # bisect calls field.__cmp__ which uses field.creation_counter to
        # maintain the correct order
        position = bisect(self.local_fields, field)
        self.local_fields.insert(position, field)

        # invalidate the field cache
        if hasattr(self, '_field_cache'):
            del self._field_cache

    @property
    def fields(self):
        """
        Returns a list of field objects available for this model (including
        through parent models).<commit_msg>Added data_filename to overridable opts<commit_after>import os
import functools
from copy import copy
from bisect import bisect
from contextlib import contextmanager
from importlib import import_module

from gitmodel import exceptions
from gitmodel import fields
from gitmodel import utils


class GitModelOptions(object):
    """
    An options class for ``GitModel``.
    """
    # attributes that can be overridden in a model's options ("Meta" class)
    meta_opts = ('abstract', 'id_attr', 'get_data_path', 'data_filename')
    reserved = ('oid',)

    def __init__(self, meta, workspace):
        self.meta = meta
        self.abstract = False
        self.local_fields = []
        self.local_many_to_many = []
        self.model_name = None
        self.parents = []
        self.id_attr = None
        self.workspace = workspace
        self.data_filename = 'data.json'
        self._serializer = None

    @property
    def serializer(self):
        if self._serializer is None:
            from gitmodel.conf import defaults
            default_serializer = defaults.DEFAULT_SERIALIZER
            if self.workspace is not None:
                default_serializer = self.workspace.config.DEFAULT_SERIALIZER
            self._serializer = import_module(default_serializer)
        return self._serializer

    def contribute_to_class(self, cls, name):
        cls._meta = self

        # Default values for these options
        self.model_name = cls.__name__

        # Apply overrides from Meta
        if self.meta:
            meta_attrs = self.meta.__dict__.copy()
            # Ignore private attributes
            for name in self.meta.__dict__:
                if name.startswith('_'):
                    del meta_attrs[name]

            override_attrs = [a for a in self.meta_opts if a in meta_attrs]
            for attr_name in override_attrs:
                value = meta_attrs.pop(attr_name)
                # if attr is a function, bind it to this instance
                if not isinstance(value, type) and hasattr(value, '__call__'):
                    value = value.__get__(self, self.__class__)
                setattr(self, attr_name, value)
        self._declared_meta = self.meta
        del self.meta

    def get_data_path(self, object_id):
        """
        Default method for building the path name for a given id.

        This is used by a model instance's get_data_path() method, which simply
        passes the instance id.
        """
        model_name = self.model_name.lower()
        return os.path.join(model_name, unicode(object_id), self.data_filename)

    def add_field(self, field):
        """ Insert a field into the fields list in correct order """
        if field.name in self.reserved:
            raise exceptions.FieldError("{} is a reserved name and cannot be"
                                        "used as a field name.")
        # bisect calls field.__cmp__ which uses field.creation_counter to
        # maintain the correct order
        position = bisect(self.local_fields, field)
        self.local_fields.insert(position, field)

        # invalidate the field cache
        if hasattr(self, '_field_cache'):
            del self._field_cache

    @property
    def fields(self):
        """
        Returns a list of field objects available for this model (including
        through parent models).
{'fraction_non_alphanumeric': 0.07362885048835462, 'fraction_numerical': 0.0, 'mean_word_length': 2.1650023775558727, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3903421', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1715, 'n_words': 572}