<commit_before># -*- coding: utf-8 -*-
'''
This module contains an opionated gateway for the crab webservice.

.. versionadded:: 0.3.0
'''

from __future__ import unicode_literals

from crabpy.client import crab_request

from suds import WebFault

from crabpy.gateway.exception import (
    GatewayRuntimeException
)

from dogpile.cache import make_region


def crab_gateway_request(client, method, *args):
    '''
    Utility function that helps making requests to the CRAB service.

    This is a specialised version of :func:`crabpy.client.crab_request` that
    allows adding extra functionality for the calls made by the gateway.

    :param client: A :class:`suds.client.Client` for the CRAB service.
    :param string action: Which method to call, eg. `ListGewesten`
    :returns: Result of the SOAP call.
    '''
    try:
        return crab_request(client, method, *args)
    except WebFault as wf:
        err = GatewayRuntimeException(
            'Could not execute request. Message from server:\n%s' % wf.fault['faultstring'],
            wf
        )
        raise err


class CrabGateway(object):
    '''
    A gateway to the CRAB webservice.
    '''
    caches = {}

    def __init__(self, client, **kwargs):
        self.client = client
        cache_regions = ['permanent', 'long', 'short']
        for cr in cache_regions:
            self.caches[cr] = make_region(key_mangler=str)
        if 'cache_config' in kwargs:
            for cr in cache_regions:
                if ('%s.backend' % cr) in kwargs['cache_config']:
                    self.caches[cr].configure_from_config(
                        kwargs['cache_config'],
                        '%s.' % cr
                    )

    def list_gewesten(self, sort=1):
        '''
        List all `gewesten` in Belgium.

        :param integer sort: What field to sort on.
        :rtype: A :class`list` of class: `Gewest`.
        '''
        def creator():
            res = crab_gateway_request(self.client, 'ListGewesten', sort)
            tmp = {}
            for r in res.GewestItem:
                if r.GewestId not in tmp:
                    tmp[r.GewestId] = {}
                tmp[r.GewestId][r.TaalCodeGewestNaam] = r.GewestNaam
            return[
                Gewest(
                    k,
                    v
                )for k, v in tmp.items()
            ]
        if self.caches['permanent'].is_configured:
            key = 'ListGewesten#%s' % sort
            gewesten = self.caches['permanent'].get_or_create(key, creator)
        else:
            gewesten = creator()
        for g in gewesten:
            g.set_gateway(self)
        return gewesten

    def get_gewest_by_id(self, id):
        '''
        Get a `gewest` by id.

        :param integer id: The id of a `gewest`.
        :rtype: A :class:`Gewest`.
        '''
        def creator():
            nl = crab_gateway_request(
                self.client, 'GetGewestByGewestIdAndTaalCode', id, 'nl'
            )
            fr = crab_gateway_request(
                self.client, 'GetGewestByGewestIdAndTaalCode', id, 'fr'
            )
            de = crab_gateway_request(
                self.client, 'GetGewestByGewestIdAndTaalCode', id, 'de'
            )
            return Gewest(
                nl.GewestId,
                {'nl': nl.GewestNaam, 'fr': fr.GewestNaam, 'de': de.GewestNaam},
                (nl.CenterX, nl.CenterY),
                (nl.MinimumX, nl.MinimumY, nl.MaximumX, nl.MaximumY),
            )
        if self.caches['permanent'].is_configured:
            key = 'GetGewestByGewestId#%s' % id
            gewest = self.caches['long'].get_or_create(key, creator)
        else:
            gewest = creator()
        gewest.set_gateway(self)
        return gewest

    def list_gemeenten(self, gewest=2, sort=1):
        '''
        List all `gemeenten` in a `gewest`.

        :param gewest: The :class:`Gewest` for which the \
            `gemeenten` are wanted.
        :param integer sort: What field to sort on.
        :rtype: A :class:`list` of :class:`Gemeente`.
        '''
        try:
            id = gewest.id
        except AttributeError:
            id = gewest

        def creator():
            res = crab_gateway_request(
                self.client, 'ListGemeentenByGewestId', id, sort
            )
            return[
                Gemeente(
                    r.GemeenteId,
                    r.GemeenteNaam,
                    r.NISGemeenteCode,
                    Gewest(id)
                )for r in res.GemeenteItem
            ]
        if self.caches['permanent'].is_configured:
            key = 'ListGemeentenByGewestId#%s%s' % (id, sort)
            gemeenten = self.caches['permanent'].get_or_create(key, creator)
        else:
            gemeenten = creator()
        for g in gemeenten:
            g.set_gateway(self)
            g.gewest.set_gateway(self)
        return gemeenten

    def get_gemeente_by_id(self, id):
        '''
        Retrieve a `gemeente` by the crab id.

        :param integer id: The CRAB id of the gemeente.
        :rtype: :class:`Gemeente`
        '''
        def creator():
            res = crab_gateway_request(
                self.client, 'GetGemeenteByGemeenteId', id
            )<commit_msg>only load dutch gemeenten in list_gemeenten refs #10<commit_after># -*- coding: utf-8 -*-
'''
This module contains an opionated gateway for the crab webservice.

.. versionadded:: 0.3.0
'''

from __future__ import unicode_literals

from crabpy.client import crab_request

from suds import WebFault

from crabpy.gateway.exception import (
    GatewayRuntimeException
)

from dogpile.cache import make_region


def crab_gateway_request(client, method, *args):
    '''
    Utility function that helps making requests to the CRAB service.

    This is a specialised version of :func:`crabpy.client.crab_request` that
    allows adding extra functionality for the calls made by the gateway.

    :param client: A :class:`suds.client.Client` for the CRAB service.
    :param string action: Which method to call, eg. `ListGewesten`
    :returns: Result of the SOAP call.
    '''
    try:
        return crab_request(client, method, *args)
    except WebFault as wf:
        err = GatewayRuntimeException(
            'Could not execute request. Message from server:\n%s' % wf.fault['faultstring'],
            wf
        )
        raise err


class CrabGateway(object):
    '''
    A gateway to the CRAB webservice.
    '''
    caches = {}

    def __init__(self, client, **kwargs):
        self.client = client
        cache_regions = ['permanent', 'long', 'short']
        for cr in cache_regions:
            self.caches[cr] = make_region(key_mangler=str)
        if 'cache_config' in kwargs:
            for cr in cache_regions:
                if ('%s.backend' % cr) in kwargs['cache_config']:
                    self.caches[cr].configure_from_config(
                        kwargs['cache_config'],
                        '%s.' % cr
                    )

    def list_gewesten(self, sort=1):
        '''
        List all `gewesten` in Belgium.

        :param integer sort: What field to sort on.
        :rtype: A :class`list` of class: `Gewest`.
        '''
        def creator():
            res = crab_gateway_request(self.client, 'ListGewesten', sort)
            tmp = {}
            for r in res.GewestItem:
                if r.GewestId not in tmp:
                    tmp[r.GewestId] = {}
                tmp[r.GewestId][r.TaalCodeGewestNaam] = r.GewestNaam
            return[
                Gewest(
                    k,
                    v
                )for k, v in tmp.items()
            ]
        if self.caches['permanent'].is_configured:
            key = 'ListGewesten#%s' % sort
            gewesten = self.caches['permanent'].get_or_create(key, creator)
        else:
            gewesten = creator()
        for g in gewesten:
            g.set_gateway(self)
        return gewesten

    def get_gewest_by_id(self, id):
        '''
        Get a `gewest` by id.

        :param integer id: The id of a `gewest`.
        :rtype: A :class:`Gewest`.
        '''
        def creator():
            nl = crab_gateway_request(
                self.client, 'GetGewestByGewestIdAndTaalCode', id, 'nl'
            )
            fr = crab_gateway_request(
                self.client, 'GetGewestByGewestIdAndTaalCode', id, 'fr'
            )
            de = crab_gateway_request(
                self.client, 'GetGewestByGewestIdAndTaalCode', id, 'de'
            )
            return Gewest(
                nl.GewestId,
                {'nl': nl.GewestNaam, 'fr': fr.GewestNaam, 'de': de.GewestNaam},
                (nl.CenterX, nl.CenterY),
                (nl.MinimumX, nl.MinimumY, nl.MaximumX, nl.MaximumY),
            )
        if self.caches['permanent'].is_configured:
            key = 'GetGewestByGewestId#%s' % id
            gewest = self.caches['long'].get_or_create(key, creator)
        else:
            gewest = creator()
        gewest.set_gateway(self)
        return gewest

    def list_gemeenten(self, gewest=2, sort=1):
        '''
        List all `gemeenten` in a `gewest`.

        :param gewest: The :class:`Gewest` for which the \
            `gemeenten` are wanted.
        :param integer sort: What field to sort on.
        :rtype: A :class:`list` of :class:`Gemeente`.
        '''
        try:
            id = gewest.id
        except AttributeError:
            id = gewest

        def creator():
            res = crab_gateway_request(
                self.client, 'ListGemeentenByGewestId', id, sort
            )
            return[
                Gemeente(
                    r.GemeenteId,
                    r.GemeenteNaam,
                    r.NISGemeenteCode,
                    Gewest(id)
                )for r in res.GemeenteItem if r.TaalCode==r.TaalCodeGemeenteNaam
            ]
        if self.caches['permanent'].is_configured:
            key = 'ListGemeentenByGewestId#%s%s' % (id, sort)
            gemeenten = self.caches['permanent'].get_or_create(key, creator)
        else:
            gemeenten = creator()
        for g in gemeenten:
            g.set_gateway(self)
            g.gewest.set_gateway(self)
        return gemeenten

    def get_gemeente_by_id(self, id):
        '''
        Retrieve a `gemeente` by the crab id.

        :param integer id: The CRAB id of the gemeente.
        :rtype: :class:`Gemeente`
        '''
        def creator():
            res = crab_gateway_request(
                self.client, 'GetGemeenteByGemeenteId', id
            )
{'fraction_non_alphanumeric': 0.10113839495719258, 'fraction_numerical': 0.0015053156458744942, 'mean_word_length': 1.691139240506329, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '622726', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3266, 'n_words': 803}