<commit_before>from magpie.api.management.resource.resource_utils import create_resource, delete_resource
from magpie.api.management.service.service_formats import *
from magpie.api.management.service.service_utils import *
from magpie.api.api_requests import *
from magpie.api.api_rest_schemas import *
from magpie.definitions.pyramid_definitions import view_config
from magpie.common import str2bool
from magpie.register import sync_services_phoenix
from magpie.models import resource_tree_service
from magpie.services import service_type_dict


@ServiceTypesAPI.get(tags=[ServicesTag], response_schemas=ServiceTypes_GET_responses)
@view_config(route_name=ServiceTypesAPI.name, request_method='GET')
def get_services_by_type_view(request):
    """List all registered services from a specific type."""
    return get_services_runner(request)


@ServicesAPI.get(tags=[ServicesTag], response_schemas=Services_GET_responses)
@view_config(route_name=ServicesAPI.name, request_method='GET')
def get_services_view(request):
    """List all registered services."""
    return get_services_runner(request)


def get_services_runner(request):
    service_type_filter = request.matchdict.get('service_type')  # no check because None/empty is for 'all services'
    json_response = {}
    if not service_type_filter:
        service_types = service_type_dict.keys()
    else:
        verify_param(service_type_filter, paramCompare=service_type_dict.keys(), isIn=True, httpError=HTTPNotAcceptable,
                     msgOnFail=Services_GET_NotAcceptableResponseSchema.description,
                     content={u'service_type': str(service_type_filter)}, contentType='application/json')
        service_types = [service_type_filter]

    for service_type in service_types:
        services = get_services_by_type(service_type, db_session=request.db)
        json_response[service_type] = {}
        for service in services:
            json_response[service_type][service.resource_name] = format_service(service, show_private_url=True)

    return valid_http(httpSuccess=HTTPOk, detail=Services_GET_OkResponseSchema.description,
                      content={u'services': json_response})


@ServicesAPI.post(schema=Services_POST_RequestBodySchema(), tags=[ServicesTag],
                  response_schemas=Services_POST_responses)
@view_config(route_name=ServicesAPI.name, request_method='POST')
def register_service(request):
    """Registers a new service."""
    service_name = get_value_multiformat_post_checked(request, 'service_name')
    service_url = get_value_multiformat_post_checked(request, 'service_url')
    service_type = get_value_multiformat_post_checked(request, 'service_type')
    service_push = str2bool(get_multiformat_post(request, 'service_push'))
    verify_param(service_type, isIn=True, paramCompare=service_type_dict.keys(), httpError=HTTPBadRequest,
                 msgOnFail=Services_POST_BadRequestResponseSchema.description)

    if models.Service.by_service_name(service_name, db_session=request.db):
        verify_param(service_name, notIn=True, httpError=HTTPConflict,
                     paramCompare=[models.Service.by_service_name(service_name, db_session=request.db).resource_name],
                     msgOnFail=Services_POST_ConflictResponseSchema.description,
                     content={u'service_name': str(service_name)})

    service = evaluate_call(lambda: models.Service(resource_name=str(service_name), resource_type=u'service',
                                                   url=str(service_url), type=str(service_type)),
                            fallback=lambda: request.db.rollback(), httpError=HTTPForbidden,
                            msgOnFail="Service creation for registration failed.",
                            content={u'service_name': str(service_name), u'resource_type': u'service',
                                     u'service_url': str(service_url), u'service_type': str(service_type)})

    def add_service_magpie_and_phoenix(svc, svc_push, db):<commit_msg>remove redundant call of get service for post<commit_after>from magpie.api.management.resource.resource_utils import create_resource, delete_resource
from magpie.api.management.service.service_formats import *
from magpie.api.management.service.service_utils import *
from magpie.api.api_requests import *
from magpie.api.api_rest_schemas import *
from magpie.definitions.pyramid_definitions import view_config
from magpie.common import str2bool
from magpie.register import sync_services_phoenix
from magpie.models import resource_tree_service
from magpie.services import service_type_dict


@ServiceTypesAPI.get(tags=[ServicesTag], response_schemas=ServiceTypes_GET_responses)
@view_config(route_name=ServiceTypesAPI.name, request_method='GET')
def get_services_by_type_view(request):
    """List all registered services from a specific type."""
    return get_services_runner(request)


@ServicesAPI.get(tags=[ServicesTag], response_schemas=Services_GET_responses)
@view_config(route_name=ServicesAPI.name, request_method='GET')
def get_services_view(request):
    """List all registered services."""
    return get_services_runner(request)


def get_services_runner(request):
    service_type_filter = request.matchdict.get('service_type')  # no check because None/empty is for 'all services'
    json_response = {}
    if not service_type_filter:
        service_types = service_type_dict.keys()
    else:
        verify_param(service_type_filter, paramCompare=service_type_dict.keys(), isIn=True, httpError=HTTPNotAcceptable,
                     msgOnFail=Services_GET_NotAcceptableResponseSchema.description,
                     content={u'service_type': str(service_type_filter)}, contentType='application/json')
        service_types = [service_type_filter]

    for service_type in service_types:
        services = get_services_by_type(service_type, db_session=request.db)
        json_response[service_type] = {}
        for service in services:
            json_response[service_type][service.resource_name] = format_service(service, show_private_url=True)

    return valid_http(httpSuccess=HTTPOk, detail=Services_GET_OkResponseSchema.description,
                      content={u'services': json_response})


@ServicesAPI.post(schema=Services_POST_RequestBodySchema(), tags=[ServicesTag],
                  response_schemas=Services_POST_responses)
@view_config(route_name=ServicesAPI.name, request_method='POST')
def register_service(request):
    """Registers a new service."""
    service_name = get_value_multiformat_post_checked(request, 'service_name')
    service_url = get_value_multiformat_post_checked(request, 'service_url')
    service_type = get_value_multiformat_post_checked(request, 'service_type')
    service_push = str2bool(get_multiformat_post(request, 'service_push'))
    verify_param(service_type, isIn=True, paramCompare=service_type_dict.keys(),
                 httpError=HTTPBadRequest, msgOnFail=Services_POST_BadRequestResponseSchema.description)
    verify_param(models.Service.by_service_name(service_name, db_session=request.db), isNone=True,
                 httpError=HTTPConflict, msgOnFail=Services_POST_ConflictResponseSchema.description,
                 content={u'service_name': str(service_name)}, paramName=u'service_name')

    service = evaluate_call(lambda: models.Service(resource_name=str(service_name), resource_type=u'service',
                                                   url=str(service_url), type=str(service_type)),
                            fallback=lambda: request.db.rollback(), httpError=HTTPForbidden,
                            msgOnFail="Service creation for registration failed.",
                            content={u'service_name': str(service_name), u'resource_type': u'service',
                                     u'service_url': str(service_url), u'service_type': str(service_type)})

    def add_service_magpie_and_phoenix(svc, svc_push, db):
{'fraction_non_alphanumeric': 0.08871878529301042, 'fraction_numerical': 0.0005019450370184465, 'mean_word_length': 4.49276361130255, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '520393', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2192, 'n_words': 361}