Esempio n. 1
0
def parse_config(config_path):
    """
    Parses the config present at config_path.

    Parameters
    ----------
    config_path : string
        The path pointing to the location where the config file is stored.

    Returns
    -------
    configparser.SectionProxy
        Returns the DEFAULT section from the config file.

    """
    cfg = configparser.ConfigParser()
    if not cfg.read(config_path):
        # cfg.read returns empty list of the config file does not exist
        logger.warning("Could not find the config file at %s" % config_path)
        cfg['DEFAULT'] = {'round_robin': 'false',
                          'ignore_slow_apis': 'false',
                          'enable_periodic_check': 'false',
                          'slow_multiplied': '0.5',
                          'PERCENTILE': '90',
                          'MAX_HISTORY_RTIME': '5000',
                          'MAX_WAIT_TIME': '0',
                          'exploration_coefficient': '0'}
    return cfg['DEFAULT']
Esempio n. 2
0
        def register_result(future):
            # Appends the result from the future to the final list that will
            # be returned

            # The future is ignored if it was cancelled.
            if not future.cancelled():
                nonlocal failed_futures
                nonlocal results

                future_exception = future.exception()
                if future_exception:
                    with failed_futures_lock:
                        failed_futures += 1
                    logger.warning(
                        "API %s raised exception %s" %
                        (future_to_api[future]['name'], future_exception))
                elif future.result() is not None:
                    results.append(
                        (future_to_api[future]['name'], future.result()))
                else:
                    # The API returned an invalid result, mark the future
                    # as failed and continue fetching from the next one.
                    with failed_futures_lock:
                        failed_futures += 1
            # Remove the future from the map
            future_to_api.pop(future, None)
Esempio n. 3
0
    def _get_result(self, api, data):
        """
        Gets the result from an API.

        Notes
        -----
            Returns None if the api raises an exception.

        Parameters
        ----------
        api : BaseThirdPartyAPIService
            The API object which implements the class BaseThirdPartyAPIService.
        data : object
            The object which will be sent to the method get_result of the
            api parameter.

        Returns
        -------
        object
            The result of calling get_result of the api object.

        """
        try:
            result, response_time = self._time_function(api.get_result, data)
            self._process_response_time_api(response_time, api)
            return result
        except Exception as e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            logger.warning(
                "API raised the exception: %s" %
                traceback.format_exception(exc_type, exc_value, exc_traceback))
        return None
Esempio n. 4
0
)
from python_translators.translation_response import merge_translations
from python_translators.factories.google_translator_factory import (
    GoogleTranslatorFactory, )
from python_translators.factories.microsoft_translator_factory import (
    MicrosoftTranslatorFactory, )
from python_translators.translators.wordnik_translator import WordnikTranslator

import logging
import python_translators

logging.getLogger("python_translators").setLevel(logging.CRITICAL)

MULTI_LANG_TRANSLATOR_AB_TESTING = False
if os.environ.get("MULTI_LANG_TRANSLATOR_AB_TESTING", None) is not None:
    logger.warning("A/B testing enabled! - MULTI_LANG_TRANSLATOR_AB_TESTING")
    MULTI_LANG_TRANSLATOR_AB_TESTING = True


class WordnikTranslate(BaseThirdPartyAPIService):
    def __init__(self, KEY_ENVVAR_NAME):
        super(WordnikTranslate,
              self).__init__(name=("Wordnik - %s" % KEY_ENVVAR_NAME))
        self._key_envvar_name = KEY_ENVVAR_NAME

    def get_result(self, data):
        lang_config = dict(
            source_language=data["source_language"],
            target_language=data["target_language"],
        )
        lang_config["key"] = get_key_from_config(self._key_envvar_name)
Esempio n. 5
0
from python_translators.config import get_key_from_config
from python_translators.query_processors.remove_unnecessary_sentences import (
    RemoveUnnecessarySentences)
from python_translators.translation_query import TranslationQuery
from python_translators.translation_response import (
    TranslationResponse, order_by_quality, filter_empty_translations)
from python_translators.translation_response import merge_translations
from python_translators.factories.google_translator_factory import (
    GoogleTranslatorFactory)
from python_translators.factories.microsoft_translator_factory import (
    MicrosoftTranslatorFactory)
from python_translators.translators.wordnik_translator import WordnikTranslator

MULTI_LANG_TRANSLATOR_AB_TESTING = False
if os.environ.get("MULTI_LANG_TRANSLATOR_AB_TESTING", None) is not None:
    logger.warning("A/B testing enabled! - MULTI_LANG_TRANSLATOR_AB_TESTING")
    MULTI_LANG_TRANSLATOR_AB_TESTING = True


class WordnikTranslate(BaseThirdPartyAPIService):
    def __init__(self, KEY_ENVVAR_NAME):
        super(WordnikTranslate, self).__init__(
            name=('Wordnik - %s' % KEY_ENVVAR_NAME))
        self._key_envvar_name = KEY_ENVVAR_NAME

    def get_result(self, data):
        lang_config = dict(
            source_language=data["source_language"],
            target_language=data["target_language"]
        )
        lang_config['key'] = get_key_from_config(self._key_envvar_name)