<commit_before>""" SQL Mappers
"""
import json
from zope.interface import implements

from tokenserver.assignment import INodeAssignment
from tokenserver.util import get_logger

from mozsvc.exceptions import BackendError
from mozsvc.http_helpers import get_url
from wimms.sql import SQLMetadata
from wimms.shardedsql import ShardedSQLMetadata


class SQLNodeAssignment(SQLMetadata):
    """Just a placeholder to mark with a zope interface.

    Silly, isn't it ?
    """
    implements(INodeAssignment)

    def get_patterns(self):
        res = super(SQLNodeAssignment, self).get_patterns()
        return dict([(pattern.service, pattern.pattern) for pattern in res])


class ShardedSQLNodeAssignment(ShardedSQLMetadata):
    """Like the SQL backend, but with one DB per service
    """
    implements(INodeAssignment)


class SecuredShardedSQLNodeAssignment(ShardedSQLMetadata):
    """Like the sharded backend, but proxies all writes to stoken
    """
    implements(INodeAssignment)

    def __init__(self, proxy_uri, databases, create_tables, **kw):
        base = super(SecuredShardedSQLNodeAssignment, self)
        base.__init__(databases, create_tables, **kw)
        self.proxy_uri = proxy_uri
        self.logger = get_logger()

    def _proxy(self, method, url, data=None, headers=None):
        if data is not None:
            data = json.dumps(data)
        status, headers, body = get_url(url, method, data, headers)
        if body:
            try:
                body = json.loads(body)
            except ValueError:
                self.logger.error("bad json body from sreg (%s): %s" %
                                                        (url, body))
                raise BackendError('Bad answer from proxy')
        return status, body

    def allocate_node(self, email, service):
        """Calls the proxy to get an allocation"""
        url = '%s/1.0/%s' % (self.proxy_uri, service)
        data = {'email': email}
        status, body = self._proxy('POST', url, data)
        if status != 200:
            raise BackendError('Could not get an allocation')
<commit_msg>lazy loading of the logger to avoid an error at startup<commit_after>""" SQL Mappers
"""
import json
from zope.interface import implements

from tokenserver.assignment import INodeAssignment
from tokenserver.util import get_logger

from mozsvc.exceptions import BackendError
from mozsvc.http_helpers import get_url
from wimms.sql import SQLMetadata
from wimms.shardedsql import ShardedSQLMetadata


class SQLNodeAssignment(SQLMetadata):
    """Just a placeholder to mark with a zope interface.

    Silly, isn't it ?
    """
    implements(INodeAssignment)

    def get_patterns(self):
        res = super(SQLNodeAssignment, self).get_patterns()
        return dict([(pattern.service, pattern.pattern) for pattern in res])


class ShardedSQLNodeAssignment(ShardedSQLMetadata):
    """Like the SQL backend, but with one DB per service
    """
    implements(INodeAssignment)


class SecuredShardedSQLNodeAssignment(ShardedSQLMetadata):
    """Like the sharded backend, but proxies all writes to stoken
    """
    implements(INodeAssignment)

    def __init__(self, proxy_uri, databases, create_tables, **kw):
        base = super(SecuredShardedSQLNodeAssignment, self)
        base.__init__(databases, create_tables, **kw)
        self.proxy_uri = proxy_uri
        self.logger = None

    def get_logger(self):
        if self.logger is None:
            self.logger = get_logger()
        return self.logger

    def _proxy(self, method, url, data=None, headers=None):
        if data is not None:
            data = json.dumps(data)
        status, headers, body = get_url(url, method, data, headers)
        if body:
            try:
                body = json.loads(body)
            except ValueError:
                self.logger.error("bad json body from sreg (%s): %s" %
                                                        (url, body))
                raise BackendError('Bad answer from proxy')
        return status, body

    def allocate_node(self, email, service):
        """Calls the proxy to get an allocation"""
        url = '%s/1.0/%s' % (self.proxy_uri, service)
        data = {'email': email}
        status, body = self._proxy('POST', url, data)
        if status != 200:
            raise BackendError('Could not get an allocation')

{'fraction_non_alphanumeric': 0.09117983963344788, 'fraction_numerical': 0.002290950744558992, 'mean_word_length': 2.706281833616299, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5196392', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1212, 'n_words': 376}