<commit_before># -*- coding: utf-8 -*-
from aiohttp import web
from concurrent.futures import ThreadPoolExecutor
from pkg_resources import iter_entry_points
from plone.server.async import IAsyncUtility
from plone.server.transactions import RequestAwareDB
from plone.server.transactions import RequestAwareTransactionManager
from plone.server.traversal import TraversalRouter
from zope.component import getAllUtilitiesRegisteredFor
from zope.configuration.config import ConfigurationMachine
from zope.configuration.xmlconfig import include
from zope.configuration.xmlconfig import registerCommonDirectives
from plone.server.interfaces import IApplication
from plone.server.interfaces import IDataBase
from plone.server.content import StaticFile
from plone.server.content import IStaticDirectory
from plone.server.content import IStaticFile
from plone.server.auth.participation import RootParticipation
from zope.interface import implementer
from zope.securitypolicy.principalpermission import PrincipalPermissionManager
from zope.component import provideUtility
from zope.component import getUtility
from zope.component import getGlobalSiteManager
from plone.server import DICT_RENDERS
from plone.server import DICT_LANGUAGES
from zope.security.interfaces import IPermission
from plone.server.contentnegotiation import ContentNegotiatorUtility
from plone.server.interfaces import IContentNegotiation
from plone.server.utils import import_class
from ZEO.ClientStorage import ClientStorage
from ZODB.DemoStorage import DemoStorage
from ZODB import DB
from zope.security.interfaces import IInteraction
from zope.configuration.config import ConfigurationConflictError

import asyncio
import json
import hashlib
import logging
import sys
import base64

logger = logging.getLogger(__name__)


@implementer(IApplication)
class ApplicationRoot(object):

    def __init__(self, config_file):
        self._dbs = {}
        self._config_file = config_file
        self._async_utilities = {}

    def add_async_utility(self, config):
        interface = import_class(config['provides'])
        factory = import_class(config['factory'])
        utility_object = factory(config['settings'])
        provideUtility(utility_object, interface)
        task = asyncio.ensure_future(utility_object.initialize(app=self))
        self.add_async_task(config['provides'], task, config)

    def add_async_task(self, ident, task, config):
        if ident in self._async_utilities:
            raise KeyError("Already exist an async utility with this id")
        self._async_utilities[ident] = {
            'task': task,
            'config': config
        }

    def cancel_async_utility(self, ident):
        if ident in self._async_utilities:
            self._async_utilities[ident]['task'].cancel()
        else:
            raise KeyError("Ident does not exist as utility")

    def del_async_utility(self, config):
        self.cancel_async_utility(config['provides'])
        interface = import_class(config['provides'])
        utility = getUtility(interface)
        gsm = getGlobalSiteManager()
        gsm.unregisterUtility(utility, provided=interface)

    def set_creator_password(self, password):
        self._creator_password = <PASSWORD>(password)

    def check_token(self, password):
        if self._creator_password == <PASSWORD>(password):
            return True
        else:
            return False

    def root_participation(self, request):
        header_auth = request.headers.get('AUTHORIZATION')
        if header_auth is not None:
            schema, _, encoded_token = header_auth.partition(' ')
            if schema.lower() == 'basic':
                token = encoded_token.encode('ascii')
                if self.check_token(token):
                    return RootParticipation(request)
        return None

    def __contains__(self, key):
        return True if key in self._dbs else False

    def __len__(self):
        return len(self._dbs)

    def __getitem__(self, key):
        return self._dbs[key]

    def __delitem__(self, key):
        """ This operation can only be done throw HTTP request

        We can check if there is permission to delete a site
        XXX TODO
        """

        del self._dbs[key]

    def __iter__(self):
        return iter(self._dbs.items())
<commit_msg>adding also root auth with bearer header<commit_after># -*- coding: utf-8 -*-
from aiohttp import web
from concurrent.futures import ThreadPoolExecutor
from pkg_resources import iter_entry_points
from plone.server.async import IAsyncUtility
from plone.server.transactions import RequestAwareDB
from plone.server.transactions import RequestAwareTransactionManager
from plone.server.traversal import TraversalRouter
from zope.component import getAllUtilitiesRegisteredFor
from zope.configuration.config import ConfigurationMachine
from zope.configuration.xmlconfig import include
from zope.configuration.xmlconfig import registerCommonDirectives
from plone.server.interfaces import IApplication
from plone.server.interfaces import IDataBase
from plone.server.content import StaticFile
from plone.server.content import IStaticDirectory
from plone.server.content import IStaticFile
from plone.server.auth.participation import RootParticipation
from zope.interface import implementer
from zope.securitypolicy.principalpermission import PrincipalPermissionManager
from zope.component import provideUtility
from zope.component import getUtility
from zope.component import getGlobalSiteManager
from plone.server import DICT_RENDERS
from plone.server import DICT_LANGUAGES
from zope.security.interfaces import IPermission
from plone.server.contentnegotiation import ContentNegotiatorUtility
from plone.server.interfaces import IContentNegotiation
from plone.server.utils import import_class
from ZEO.ClientStorage import ClientStorage
from ZODB.DemoStorage import DemoStorage
from ZODB import DB
from zope.security.interfaces import IInteraction
from zope.configuration.config import ConfigurationConflictError

import asyncio
import json
import hashlib
import logging
import sys
import base64

logger = logging.getLogger(__name__)


@implementer(IApplication)
class ApplicationRoot(object):

    def __init__(self, config_file):
        self._dbs = {}
        self._config_file = config_file
        self._async_utilities = {}

    def add_async_utility(self, config):
        interface = import_class(config['provides'])
        factory = import_class(config['factory'])
        utility_object = factory(config['settings'])
        provideUtility(utility_object, interface)
        task = asyncio.ensure_future(utility_object.initialize(app=self))
        self.add_async_task(config['provides'], task, config)

    def add_async_task(self, ident, task, config):
        if ident in self._async_utilities:
            raise KeyError("Already exist an async utility with this id")
        self._async_utilities[ident] = {
            'task': task,
            'config': config
        }

    def cancel_async_utility(self, ident):
        if ident in self._async_utilities:
            self._async_utilities[ident]['task'].cancel()
        else:
            raise KeyError("Ident does not exist as utility")

    def del_async_utility(self, config):
        self.cancel_async_utility(config['provides'])
        interface = import_class(config['provides'])
        utility = getUtility(interface)
        gsm = getGlobalSiteManager()
        gsm.unregisterUtility(utility, provided=interface)

    def set_creator_password(self, password):
        self._creator_password = <PASSWORD>(password)

    def check_token(self, password):
        if self._creator_password == <PASSWORD>(password):
            return True
        else:
            return False

    def root_participation(self, request):
        header_auth = request.headers.get('AUTHORIZATION')
        if header_auth is not None:
            schema, _, encoded_token = header_auth.partition(' ')
            if schema.lower() == 'basic' or schema.lower() == 'bearer':
                token = encoded_token.encode('ascii')
                if self.check_token(token):
                    return RootParticipation(request)
        return None

    def __contains__(self, key):
        return True if key in self._dbs else False

    def __len__(self):
        return len(self._dbs)

    def __getitem__(self, key):
        return self._dbs[key]

    def __delitem__(self, key):
        """ This operation can only be done throw HTTP request

        We can check if there is permission to delete a site
        XXX TODO
        """

        del self._dbs[key]

    def __iter__(self):
        return iter(self._dbs.items())

{'fraction_non_alphanumeric': 0.07212699873461406, 'fraction_numerical': 0.0006902105142068331, 'mean_word_length': 3.6343283582089554, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '513953', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2283, 'n_words': 643}