<commit_before>"""
Mapping and mapping registry.

This file is part of the everest project.
See LICENSE.txt for licensing, CONTRIBUTORS.txt for contributor information.

Created on May 4, 2012.
"""
from collections import OrderedDict
from everest.constants import RESOURCE_ATTRIBUTE_KINDS
from everest.representers.attributes import MappedAttribute
from everest.representers.attributes import MappedAttributeKey
from everest.representers.config import RepresenterConfiguration
from everest.representers.dataelements import SimpleCollectionDataElement
from everest.representers.dataelements import SimpleLinkedDataElement
from everest.representers.dataelements import SimpleMemberDataElement
from everest.representers.interfaces import IDataElement
from everest.representers.interfaces import IMemberDataElement
from everest.representers.traversal import DataElementBuilderResourceTreeVisitor
from everest.representers.traversal import ResourceTreeTraverser
from everest.resources.attributes import get_resource_class_attributes
from everest.resources.interfaces import ICollectionResource
from everest.resources.interfaces import IMemberResource
from everest.resources.interfaces import IResourceLink
from everest.resources.link import Link
from everest.resources.staging import create_staging_collection
from everest.resources.utils import get_collection_class
from everest.resources.utils import provides_collection_resource
from everest.resources.utils import provides_member_resource
from pyramid.compat import iteritems_
from pyramid.compat import itervalues_
from zope.interface import providedBy as provided_by # pylint: disable=E0611,F0401

__docformat__ = 'reStructuredText en'
__all__ = ['Mapping',
           'MappingRegistry',
           'PruningMapping',
           'SimpleMappingRegistry',
           ]


class Mapping(object):
    """
    Performs configurable resource <-> data element tree <-> representation
    attribute mappings.

    :property mapped_class: The resource class mapped by this mapping.
    :property data_element_class: The data element class for this mapping.
    """
    #: Flag indicating if this mapping should prune the attribute tree
    #: according to the IGNORE settings.
    is_pruning = False

    def __init__(self, mapping_registry, mapped_class, data_element_class,
                 configuration):
        """
        :param configuration: Mapping configuration object.
        """
        self.__mp_reg = mapping_registry
        self.__mapped_cls = mapped_class
        self.__is_collection_mapping = \
                                provides_collection_resource(mapped_class)
        self.__de_cls = data_element_class
        # List of configurations; the last one added is the active one.
        self.__configurations = [configuration]
        #
        self.__mapped_attr_cache = {}

    def clone(self, options=None, attribute_options=None):
        """
        Returns a clone of this mapping that is configured with the given
        option and attribute option dictionaries.
        """
        copied_cfg = self.__configurations[-1].copy()
        upd_cfg = type(copied_cfg)(options=options,
                                   attribute_options=attribute_options)
        copied_cfg.update(upd_cfg)
        return self.__class__(self.__mp_reg, self.__mapped_cls,
                              self.__de_cls, copied_cfg)

    def update(self, options=None, attribute_options=None):
        """
        Updates this mapping with the given option and attribute option maps.
        """
        for attributes, opts in attribute_options.items():
            ignore = opts.get('ignore', True)
            for attr_name in attributes:
                try:
                    attr = self.get_attribute(attr_name)
                except KeyError:
                    if not ignore:
                        raise AttributeError('Trying to configure non-existing resource attribute "%s"' %(attr_name))


        cfg = RepresenterConfiguration(options=options,
                                       attribute_options=attribute_options)
        self.configuration.update(cfg)
<commit_msg>issue1: some cosmetic changes<commit_after>"""
Mapping and mapping registry.

This file is part of the everest project.
See LICENSE.txt for licensing, CONTRIBUTORS.txt for contributor information.

Created on May 4, 2012.
"""
from collections import OrderedDict
from everest.constants import RESOURCE_ATTRIBUTE_KINDS
from everest.representers.attributes import MappedAttribute
from everest.representers.attributes import MappedAttributeKey
from everest.representers.config import RepresenterConfiguration, IGNORE_OPTION
from everest.representers.dataelements import SimpleCollectionDataElement
from everest.representers.dataelements import SimpleLinkedDataElement
from everest.representers.dataelements import SimpleMemberDataElement
from everest.representers.interfaces import IDataElement
from everest.representers.interfaces import IMemberDataElement
from everest.representers.traversal import DataElementBuilderResourceTreeVisitor
from everest.representers.traversal import ResourceTreeTraverser
from everest.resources.attributes import get_resource_class_attributes
from everest.resources.interfaces import ICollectionResource
from everest.resources.interfaces import IMemberResource
from everest.resources.interfaces import IResourceLink
from everest.resources.link import Link
from everest.resources.staging import create_staging_collection
from everest.resources.utils import get_collection_class
from everest.resources.utils import provides_collection_resource
from everest.resources.utils import provides_member_resource
from pyramid.compat import iteritems_
from pyramid.compat import itervalues_
from zope.interface import providedBy as provided_by # pylint: disable=E0611,F0401

__docformat__ = 'reStructuredText en'
__all__ = ['Mapping',
           'MappingRegistry',
           'PruningMapping',
           'SimpleMappingRegistry',
           ]


class Mapping(object):
    """
    Performs configurable resource <-> data element tree <-> representation
    attribute mappings.

    :property mapped_class: The resource class mapped by this mapping.
    :property data_element_class: The data element class for this mapping.
    """
    #: Flag indicating if this mapping should prune the attribute tree
    #: according to the IGNORE settings.
    is_pruning = False

    def __init__(self, mapping_registry, mapped_class, data_element_class,
                 configuration):
        """
        :param configuration: Mapping configuration object.
        """
        self.__mp_reg = mapping_registry
        self.__mapped_cls = mapped_class
        self.__is_collection_mapping = \
                                provides_collection_resource(mapped_class)
        self.__de_cls = data_element_class
        # List of configurations; the last one added is the active one.
        self.__configurations = [configuration]
        #
        self.__mapped_attr_cache = {}

    def clone(self, options=None, attribute_options=None):
        """
        Returns a clone of this mapping that is configured with the given
        option and attribute option dictionaries.
        """
        copied_cfg = self.__configurations[-1].copy()
        upd_cfg = type(copied_cfg)(options=options,
                                   attribute_options=attribute_options)
        copied_cfg.update(upd_cfg)
        return self.__class__(self.__mp_reg, self.__mapped_cls,
                              self.__de_cls, copied_cfg)

    def update(self, options=None, attribute_options=None):
        """
        Updates this mapping with the given option and attribute option maps.
        """
        for attributes, opts in attribute_options.items():
            do_ignore = opts.get(IGNORE_OPTION, True)
            for attr_name in attributes:
                if not (attr_name in
                        self.__get_attribute_map(self.__mapped_cls, None)
                                                             and do_ignore):
                    raise AttributeError('Trying to configure non-existing '
                                         'resource attribute "%s"' %(attr_name))

        cfg = RepresenterConfiguration(options=options,
                                       attribute_options=attribute_options)
        self.configuration.update(cfg)

{'fraction_non_alphanumeric': 0.05866254164683484, 'fraction_numerical': 0.0034507377439314613, 'mean_word_length': 3.1342843089030987, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1661998', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1993, 'n_words': 590}