Ejemplo n.º 1
0
def register_search_provider(name, fetch_endpoint=None, provider_class=None):
    """Register a Search Provider with the given name and fetch_endpoint.

    Both have to be unique and if not raises AlreadyExistsError.
    The fetch_endpoint is used by clients to fetch the article from the Search Provider.

    :param name: Search Provider Name
    :type name: str
    :param fetch_endpoint: relative url to /api
    :type fetch_endpoint: str
    :param provider_class: provider implementation
    :type provider: superdesk.SearchProvider
    :raises: AlreadyExistsError - if a search has been registered with either name or fetch_endpoint.
    """
    if name in registered_search_providers:
        raise AlreadyExistsError(
            "A Search Provider with name: {} already exists".format(name))

    if fetch_endpoint and fetch_endpoint in registered_search_providers.values(
    ):
        raise AlreadyExistsError(
            "A Search Provider for the fetch endpoint: {} exists with name: {}"
            .format(fetch_endpoint, registered_search_providers[name]))

    registered_search_providers[
        name] = provider_class if provider_class else fetch_endpoint

    if not registered_search_providers[name]:
        raise ValueError(
            'You have to specify fetch_endpoint or provider_class.')

    allowed_search_providers.append(name)
Ejemplo n.º 2
0
def register_search_provider(name,
                             fetch_endpoint=None,
                             provider_class=None,
                             label=None):
    """Register a Search Provider with the given name and fetch_endpoint.

    Both have to be unique and if not raises AlreadyExistsError.
    The fetch_endpoint is used by clients to fetch the article from the Search Provider.

    :param name: Search Provider Name
    :type name: str
    :param fetch_endpoint: relative url to /api
    :type fetch_endpoint: str
    :param provider_class: provider implementation
    :type provider: superdesk.SearchProvider
    :param label: label to use (None to use provider_class.label or name in this order)
    :type label: str
    :raises: AlreadyExistsError - if a search has been registered with either name or fetch_endpoint.
    """
    if fetch_endpoint is not None and not isinstance(fetch_endpoint, str):
        raise ValueError(_("fetch_enpoint must be a string"))
    if name in registered_search_providers:
        raise AlreadyExistsError(
            "A Search Provider with name: {} already exists".format(name))

    if not ((fetch_endpoint and not provider_class) or
            (not fetch_endpoint and provider_class)):
        raise ValueError(
            _('You have to specify either fetch_endpoint or provider_class.'))

    provider_data = {}

    if fetch_endpoint:
        existing_endpoints = {
            d['endpoint']
            for d in registered_search_providers.values() if 'endpoint' in d
        }
        if fetch_endpoint in existing_endpoints:
            raise AlreadyExistsError(
                _("A Search Provider for the fetch endpoint: {endpoint} exists with name: {name}"
                  ).format(endpoint=fetch_endpoint,
                           name=registered_search_providers[name]))
        provider_data['endpoint'] = fetch_endpoint
    else:
        provider_data['class'] = provider_class

    if label is not None:
        provider_data['label'] = label
    elif provider_class is not None and hasattr(
            provider_class, 'label') and provider_class.label:
        provider_data['label'] = provider_class.label
    else:
        provider_data['label'] = name

    provider_data = registered_search_providers[name] = provider_data

    allowed_search_providers.append(name)
Ejemplo n.º 3
0
def register_search_provider(name, fetch_endpoint):
    """
    Registers a Search Provider with the given name and fetch_endpoint. Both have to be unique and if not raises
    AlreadyExistsError. The fetch_endpoint is used by clients to fetch the article from the Search Provider.

    :param name: Search Provider Name
    :type name: str
    :param fetch_endpoint: relative url to /api
    :type fetch_endpoint: str
    :raises: AlreadyExistsError - if a search has been registered with either name or fetch_endpoint.
    """

    if name in registered_search_providers:
        raise AlreadyExistsError(
            "A Search Provider with name: {} already exists".format(name))

    if fetch_endpoint in registered_search_providers.values():
        raise AlreadyExistsError(
            "A Search Provider for the fetch endpoint: {} exists with name: {}"
            .format(fetch_endpoint, registered_search_providers[name]))

    registered_search_providers[name] = fetch_endpoint
    allowed_search_providers.append(name)
Ejemplo n.º 4
0
def register_feed_parser(parser_name, parser_class):
    """
    Registers the Feed Parser with the application.
    :class: `superdesk.io.feed_parsers.RegisterFeedParser` uses this function to register the feed parser.

    :param parser_name: unique name to identify the Feed Parser class
    :param parser_class: Feed Parser class
    :raises: AlreadyExistsError if a feed parser with same name already been registered
    """

    if parser_name in registered_feed_parsers:
        raise AlreadyExistsError('Feed Parser: {} already registered by {}'
                                 .format(parser_name, type(registered_feed_parsers[parser_name])))

    registered_feed_parsers[parser_name] = parser_class
    allowed_feed_parsers.append(parser_name)
Ejemplo n.º 5
0
def register_feeding_service(service_class):
    """
    Registers the Feeding Service with the application.

    :class: `superdesk.io.feeding_services.RegisterFeedingService` uses this function to register the feeding service.

    :param service_class: Feeding Service class
    :raises: AlreadyExistsError if a feeding service with same name already been registered
    """

    if service_class.NAME in registered_feeding_services:
        raise AlreadyExistsError('Feeding Service: {} already registered by {}'
                                 .format(service_class.NAME,
                                         type(registered_feeding_services[service_class.NAME])))

    registered_feeding_services[service_class.NAME] = service_class()
    allowed_feeding_services.append(service_class.NAME)

    service_class.ERRORS.append(SuperdeskIngestError.parserNotFoundError().get_error_description())
    feeding_service_errors[service_class.NAME] = dict(service_class.ERRORS)
Ejemplo n.º 6
0
def register_feeding_service(service_name, service_class, errors):
    """
    Registers the Feeding Service with the application.
    :class: `superdesk.io.feeding_services.RegisterFeedingService` uses this function to register the feeding service.

    :param service_name: unique name to identify the Feeding Service class
    :param service_class: Feeding Service class
    :param errors: list of tuples, where each tuple represents an error that can be raised by a Feeding Service class.
                   Tuple syntax: (error_code, error_message)
    :raises: AlreadyExistsError if a feeding service with same name already been registered
    """

    if service_name in registered_feed_parsers:
        raise AlreadyExistsError('Feeding Service: {} already registered by {}'
                                 .format(service_name, type(registered_feeding_services[service_name])))

    registered_feeding_services[service_name] = service_class
    allowed_feeding_services.append(service_name)

    errors.append(SuperdeskIngestError.parserNotFoundError().get_error_description())
    feeding_service_errors[service_name] = dict(errors)