<commit_before>#
# Copyright 2012, Couchbase, Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from functools import wraps

import requests


def verbose_error(function):
    """Ensure more verbose error message in case of connection error"""
    @wraps(function)
    def wrapper(self, *args, **kargs):
        try:
            return function(self, *args, **kargs)
        except requests.exceptions.ConnectionError:
            raise ConnectionError(self.base_url)
    return wrapper


def only_existing(function):
    """Allow operations only on existing databases"""
    @wraps(function)
    def wrapper(self, dbname):
        if dbname not in self.list_dbs():
            raise NotExistingDatabase(dbname)
        else:
            return function(self, dbname)
    return wrapper


def only_not_existing(function):
    """Allow operations only on not existing databases."""
    @wraps(function)
    def wrapper(self, dbname):
        if dbname in self.list_dbs():
            raise ExistingDatabase(dbname)
        else:
            return function(self, dbname)
    return wrapper


def correct_data(function):
    """Check data type"""
    @wraps(function)
    def wrapper(self, data, *args, **kargs):
        if not isinstance(data, dict) or not data:
            raise TypeError('Non-empty dictionary is expected')
        else:
            return function(self, data, *args, **kargs)
    return wrapper


def correct_params(function):
    """Check type of container and names of parameters"""
    @wraps(function)
    def wrapper(self, params, *args, **kargs):
        if not isinstance(params, dict) or not params:
            raise TypeError('Non-empty dictionary is expected')
        for param in params:
            if param not in ('to', 'from', 'group', 'ptr', 'reducer'):
                raise TypeError('Unexpected parameter "{0}"'.format(param))
        return function(self, params, *args, **kargs)
    return wrapper


class ConnectionError(Exception):

    def __init__(self, base_url):
        self.base_url = base_url

    def __str__(self):
        return 'Connection refused to "{0}"'.format(self.base_url)


class NotExistingDatabase(Exception):

    def __init__(self, dbname):
        self.dbname = dbname

    def __str__(self):
        return 'Database "{0}" does not exist'.format(self.dbname)


class ExistingDatabase(Exception):

    def __init__(self, dbname):
        self.dbname = dbname

    def __str__(self):
        return 'Database "{0}" already exists'.format(self.dbname)
<commit_msg>fix list of args and kargs in wrappers<commit_after>#
# Copyright 2012, Couchbase, Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from functools import wraps

import requests


def verbose_error(function):
    """Ensure more verbose error message in case of connection error"""
    @wraps(function)
    def wrapper(self, url, *args, **kargs):
        try:
            return function(self, url, *args, **kargs)
        except requests.exceptions.ConnectionError:
            raise ConnectionError(self.base_url)
    return wrapper


def only_existing(function):
    """Allow operations only on existing databases"""
    @wraps(function)
    def wrapper(self, dbname):
        if dbname not in self.list_dbs():
            raise NotExistingDatabase(dbname)
        else:
            return function(self, dbname)
    return wrapper


def only_not_existing(function):
    """Allow operations only on not existing databases."""
    @wraps(function)
    def wrapper(self, dbname):
        if dbname in self.list_dbs():
            raise ExistingDatabase(dbname)
        else:
            return function(self, dbname)
    return wrapper


def correct_data(function):
    """Check data type"""
    @wraps(function)
    def wrapper(self, data, *args, **kargs):
        if not isinstance(data, dict) or not data:
            raise TypeError('Non-empty dictionary is expected')
        else:
            return function(self, data, *args, **kargs)
    return wrapper


def correct_params(function):
    """Check type of container and names of parameters"""
    @wraps(function)
    def wrapper(self, params):
        if not isinstance(params, dict) or not params:
            raise TypeError('Non-empty dictionary is expected')
        for param in params:
            if param not in ('to', 'from', 'group', 'ptr', 'reducer'):
                raise TypeError('Unexpected parameter "{0}"'.format(param))
        return function(self, params)
    return wrapper


class ConnectionError(Exception):

    def __init__(self, base_url):
        self.base_url = base_url

    def __str__(self):
        return 'Connection refused to "{0}"'.format(self.base_url)


class NotExistingDatabase(Exception):

    def __init__(self, dbname):
        self.dbname = dbname

    def __str__(self):
        return 'Database "{0}" does not exist'.format(self.dbname)


class ExistingDatabase(Exception):

    def __init__(self, dbname):
        self.dbname = dbname

    def __str__(self):
        return 'Database "{0}" already exists'.format(self.dbname)

{'fraction_non_alphanumeric': 0.09815148045149681, 'fraction_numerical': 0.003926059218059872, 'mean_word_length': 3.070572569906791, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '476951', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1651, 'n_words': 597}