<commit_before># Copyright (c) 2008 gocept gmbh & co. kg
# See also LICENSE.txt
"""Connector which integrates into Zope CA and transaction machinery."""

import ZConfig
import gocept.cache.property
import logging
import os
import threading
import transaction
import transaction.interfaces
import zeit.connector.connector
import zeit.connector.interfaces
import zope.app.appsetup.product
import zope.component
import zope.event
import zope.interface
import zope.publisher.interfaces
import zope.security.interfaces
import zope.security.management


log = logging.getLogger(__name__)


class ZopeConnector(zeit.connector.connector.Connector):

    def create_connection(self, root):
        connection = super(self.__class__, self).create_connection(root)
        dm = connection._connector_datamanager = DataManager(self)
        transaction.get().join(dm)
        url = self._get_calling_url()
        if url is not None:
            connection.additional_headers['Referer'] = url
        return connection

    def _get_calling_url(self):
        try:
            interaction = zope.security.management.getInteraction()
        except zope.security.interfaces.NoInteraction:
            return None
        if not interaction.participations:
            return None
        request = interaction.participations[0]
        if not (zope.publisher.interfaces.http.IHTTPApplicationRequest
                .providedBy(request)):
            return None
        return request.getURL()

    def get_datamanager(self):
        conn = self.get_connection()
        return conn._connector_datamanager

    def lock(self, id, principal, until):
        locktoken = super(self.__class__, self).lock(id, principal, until)
        datamanager = self.get_datamanager()
        datamanager.add_cleanup(self.unlock, id, locktoken, False)
        return locktoken

    def unlock(self, id, locktoken=None, invalidate=True):
        locktoken = super(self.__class__, self).unlock(id, locktoken,
                                                      invalidate)
        self.get_datamanager().remove_cleanup(self.unlock, id, locktoken,
                                              False)
        return locktoken

    @property
    def body_cache(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.IResourceCache)

    @property
    def property_cache(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.IPropertyCache)

    @property
    def child_name_cache(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.IChildNameCache)

    @property
    def locktokens(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.ILockInfoStorage)

    def _invalidate_cache(self, id, added=False, deleted=False):
        zope.event.notify(
            zeit.connector.interfaces.ResourceInvaliatedEvent(id))


def connectorFactory():
    """Factory for creating the connector with data from zope.conf."""
    config = zope.app.appsetup.product.getProductConfiguration(
        'zeit.connector')
    if config:
        root = config.get('document-store')
        if not root:
            raise ZConfig.ConfigurationError(
                "WebDAV server not configured properly.")
        search_root = config.get('document-store-search')
    else:
        root = os.environ.get('connector-url')
        search_root = os.environ.get('search-connector-url')

    if not root:
        raise ZConfig.ConfigurationError(
            "zope.conf has no product config for zeit.connector.")
<commit_msg>remove arguments which are no longer in use<commit_after># Copyright (c) 2008 gocept gmbh & co. kg
# See also LICENSE.txt
"""Connector which integrates into Zope CA and transaction machinery."""

import ZConfig
import gocept.cache.property
import logging
import os
import threading
import transaction
import transaction.interfaces
import zeit.connector.connector
import zeit.connector.interfaces
import zope.app.appsetup.product
import zope.component
import zope.event
import zope.interface
import zope.publisher.interfaces
import zope.security.interfaces
import zope.security.management


log = logging.getLogger(__name__)


class ZopeConnector(zeit.connector.connector.Connector):

    def create_connection(self, root):
        connection = super(self.__class__, self).create_connection(root)
        dm = connection._connector_datamanager = DataManager(self)
        transaction.get().join(dm)
        url = self._get_calling_url()
        if url is not None:
            connection.additional_headers['Referer'] = url
        return connection

    def _get_calling_url(self):
        try:
            interaction = zope.security.management.getInteraction()
        except zope.security.interfaces.NoInteraction:
            return None
        if not interaction.participations:
            return None
        request = interaction.participations[0]
        if not (zope.publisher.interfaces.http.IHTTPApplicationRequest
                .providedBy(request)):
            return None
        return request.getURL()

    def get_datamanager(self):
        conn = self.get_connection()
        return conn._connector_datamanager

    def lock(self, id, principal, until):
        locktoken = super(self.__class__, self).lock(id, principal, until)
        datamanager = self.get_datamanager()
        datamanager.add_cleanup(self.unlock, id, locktoken, False)
        return locktoken

    def unlock(self, id, locktoken=None, invalidate=True):
        locktoken = super(self.__class__, self).unlock(id, locktoken,
                                                      invalidate)
        self.get_datamanager().remove_cleanup(self.unlock, id, locktoken,
                                              False)
        return locktoken

    @property
    def body_cache(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.IResourceCache)

    @property
    def property_cache(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.IPropertyCache)

    @property
    def child_name_cache(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.IChildNameCache)

    @property
    def locktokens(self):
        return zope.component.getUtility(
            zeit.connector.interfaces.ILockInfoStorage)

    def _invalidate_cache(self, id):
        zope.event.notify(
            zeit.connector.interfaces.ResourceInvaliatedEvent(id))


def connectorFactory():
    """Factory for creating the connector with data from zope.conf."""
    config = zope.app.appsetup.product.getProductConfiguration(
        'zeit.connector')
    if config:
        root = config.get('document-store')
        if not root:
            raise ZConfig.ConfigurationError(
                "WebDAV server not configured properly.")
        search_root = config.get('document-store-search')
    else:
        root = os.environ.get('connector-url')
        search_root = os.environ.get('search-connector-url')

    if not root:
        raise ZConfig.ConfigurationError(
            "zope.conf has no product config for zeit.connector.")

{'fraction_non_alphanumeric': 0.08490566037735849, 'fraction_numerical': 0.0013873473917869034, 'mean_word_length': 2.663109756097561, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5813540', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1891, 'n_words': 443}