<commit_before>"""Create resources, get sheets/metadata, permission checks."""
from copy import copy

from pyramid.request import Request
from pyramid.util import DottedNameResolver
from pyramid.decorator import reify
from pyramid.traversal import resource_path
from substanced.content import ContentRegistry
from substanced.content import add_content_type
from substanced.content import add_service_type
from substanced.workflow import IWorkflow
from zope.interface.interfaces import IInterface

from adhocracy_core.exceptions import RuntimeConfigurationError
from adhocracy_core.interfaces import ISheet
from adhocracy_core.utils import get_iresource


resolver = DottedNameResolver()


class ResourceContentRegistry(ContentRegistry):

    """Extend substanced content registry to work with resources."""

    def __init__(self, registry):
        super().__init__(registry)
        self.resources_meta = {}
        """Resources meta mapping.

        Dictionary with key iresource (`resource type` interface) and value
        :class:`adhocracy_core.interfaces.ResourceMetadata`.
        """
        self.sheets_meta = {}
        """Sheets meta mapping.

        Dictionary with key isheet (`sheet type` interface) and value
        :class:`adhocracy_core.interfaces.SheetMetadata`.
        """
        self.workflows_meta = {}
        """Dictionary with key workflow name and value data.

        The value data structure is defined in
        :class:`adhocracy_core.workflows.schemas.Workflow`
        """
        self.workflows = {}
        """Dictionary with key workflow name and value
        :class:`substanced.workflow.IWorkflow`.
        """

    def get_resources_meta_addable(self, context: object,
                                   request: Request) -> list:
        """Get addable resource meta for context, mind permissions."""
        iresource = get_iresource(context)
        addables = self.resources_meta_addable[iresource]
        addables_allowed = []
        for resource_meta in addables:
            permission = resource_meta.permission_add
            if request.has_permission(permission, context):
                addables_allowed.append(resource_meta)
        return addables_allowed

    @reify
    def resources_meta_addable(self):
        """Addable resources metadata mapping.

        Dictionary with key iresource (`resource type` interface)` and value
        list of :class:`adhocracy_core.interfaces.ResourceMetadata`.
        The value includes only addables for a context with `resource type`.
        """
        resources_addables = {}
        for iresource, resource_meta in self.resources_meta.items():
            addables = resource_meta.element_types
            all_addables = []
            for iresource_, resource_meta_ in self.resources_meta.items():
                is_implicit = resource_meta_.is_implicit_addable
                for iresource_addable in addables:
                    is_subtype = is_implicit\
                        and iresource_.extends(iresource_addable)
                    is_is = iresource_ is iresource_addable
                    if is_subtype or is_is:
                        all_addables.append(resource_meta_)
            resources_addables[iresource] = all_addables
        return resources_addables

    @reify
    def permissions(self):
        """Set of all permissions defined in the system."""
        perms = {'hide_resource', 'edit_group'}  # built-in permissions
        for resource_meta in self.resources_meta.values():<commit_msg>Add type definition to the permission function<commit_after>"""Create resources, get sheets/metadata, permission checks."""
from copy import copy

from pyramid.request import Request
from pyramid.util import DottedNameResolver
from pyramid.decorator import reify
from pyramid.traversal import resource_path
from substanced.content import ContentRegistry
from substanced.content import add_content_type
from substanced.content import add_service_type
from substanced.workflow import IWorkflow
from zope.interface.interfaces import IInterface

from adhocracy_core.exceptions import RuntimeConfigurationError
from adhocracy_core.interfaces import ISheet
from adhocracy_core.utils import get_iresource


resolver = DottedNameResolver()


class ResourceContentRegistry(ContentRegistry):

    """Extend substanced content registry to work with resources."""

    def __init__(self, registry):
        super().__init__(registry)
        self.resources_meta = {}
        """Resources meta mapping.

        Dictionary with key iresource (`resource type` interface) and value
        :class:`adhocracy_core.interfaces.ResourceMetadata`.
        """
        self.sheets_meta = {}
        """Sheets meta mapping.

        Dictionary with key isheet (`sheet type` interface) and value
        :class:`adhocracy_core.interfaces.SheetMetadata`.
        """
        self.workflows_meta = {}
        """Dictionary with key workflow name and value data.

        The value data structure is defined in
        :class:`adhocracy_core.workflows.schemas.Workflow`
        """
        self.workflows = {}
        """Dictionary with key workflow name and value
        :class:`substanced.workflow.IWorkflow`.
        """

    def get_resources_meta_addable(self, context: object,
                                   request: Request) -> list:
        """Get addable resource meta for context, mind permissions."""
        iresource = get_iresource(context)
        addables = self.resources_meta_addable[iresource]
        addables_allowed = []
        for resource_meta in addables:
            permission = resource_meta.permission_add
            if request.has_permission(permission, context):
                addables_allowed.append(resource_meta)
        return addables_allowed

    @reify
    def resources_meta_addable(self):
        """Addable resources metadata mapping.

        Dictionary with key iresource (`resource type` interface)` and value
        list of :class:`adhocracy_core.interfaces.ResourceMetadata`.
        The value includes only addables for a context with `resource type`.
        """
        resources_addables = {}
        for iresource, resource_meta in self.resources_meta.items():
            addables = resource_meta.element_types
            all_addables = []
            for iresource_, resource_meta_ in self.resources_meta.items():
                is_implicit = resource_meta_.is_implicit_addable
                for iresource_addable in addables:
                    is_subtype = is_implicit\
                        and iresource_.extends(iresource_addable)
                    is_is = iresource_ is iresource_addable
                    if is_subtype or is_is:
                        all_addables.append(resource_meta_)
            resources_addables[iresource] = all_addables
        return resources_addables

    @reify
    def permissions(self) -> [str]:
        """Set of all permissions defined in the system."""
        perms = {'hide_resource', 'edit_group'}  # built-in permissions
        for resource_meta in self.resources_meta.values():
{'fraction_non_alphanumeric': 0.07179054054054054, 'fraction_numerical': 0.0, 'mean_word_length': 2.759259259259259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '1884582', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1746, 'n_words': 500}