Пример #1
0
def text_resolve(reference, returned_format):
    """

    :param reference:
    :param returned_format:
    :return:
    """
    try:
        resolved = cache_resolved_get(reference)
        if resolved:
            return format_resolved_reference(returned_format,
                                             resolved=resolved,
                                             reference=reference)

        if bool(RE_NUMERIC_VALUE.search(reference)):
            parsed_ref = text_parser(reference)
            if parsed_ref:
                return format_resolved_reference(
                    returned_format,
                    resolved=str(solve_reference(Hypotheses(parsed_ref))),
                    reference=reference)
            raise NoSolution("NotParsed")
        else:
            raise ValueError(
                'Reference with no year and volume cannot be resolved.')
    except (NoSolution, Incomplete, ValueError) as e:
        current_app.logger.error('Exception: {error}'.format(error=str(e)))
        return format_resolved_reference(returned_format,
                                         resolved='0.0 %s' % (19 * '.'),
                                         reference=reference)
    except Exception as e:
        current_app.logger.error('Exception: {error}'.format(error=str(e)))
        raise
Пример #2
0
def text_resolve(reference, returned_format):
    """

    :param reference:
    :param returned_format:
    :return:
    """
    not_resolved = '0.0 %s' % (19 * '.')
    try:
        resolved = cache_resolved_get(reference)
        if resolved:
            return format_resolved_reference(returned_format,
                                             resolved=resolved,
                                             reference=reference)

        if bool(RE_NUMERIC_VALUE.search(reference)):
            parsed_ref = text_parser(reference)
            if parsed_ref:
                return format_resolved_reference(returned_format,
                                                 resolved=str(solve_reference(Hypotheses(parsed_ref))),
                                                 reference=reference)
            error_comment = 'NoSolution: unable to parse'
            current_app.logger.error('Exception: {error}'.format(error=error_comment))
            return format_resolved_reference(returned_format,
                                             resolved=not_resolved,
                                             reference=reference,
                                             comment=error_comment)
        else:
            error_comment = 'ValueError: reference with no year and volume cannot be resolved.'
            current_app.logger.error('Exception: {error}'.format(error=error_comment))
            return format_resolved_reference(returned_format,
                                             resolved=not_resolved,
                                             reference=reference,
                                             comment=error_comment)
    except (NoSolution, Incomplete, ValueError) as e:
        error_comment = 'Exception: {error}'.format(error=str(e))
        current_app.logger.error(error_comment)
        return format_resolved_reference(returned_format,
                                         resolved=not_resolved,
                                         reference=reference,
                                         comment=error_comment)
    except Exception as e:
        error_comment = 'Exception: {error}'.format(error=str(e))
        current_app.logger.error(error_comment)
        return format_resolved_reference(returned_format,
                                         resolved=not_resolved,
                                         reference=reference,
                                         comment=error_comment)
Пример #3
0
def text_resolve(reference, returned_format):
    """

    :param reference:
    :param returned_format:
    :return:
    """
    try:
        if bool(RE_NUMERIC_VALUE.search(reference)):
            parsed_ref = text_parser(reference)
            result = format_resolved_reference(
                returned_format,
                resolved=str(solve_reference(Hypotheses(parsed_ref))),
                reference=reference)
        else:
            raise ValueError(
                'Reference with no year and volume cannot be resolved.')
    except Exception as e:
        current_app.logger.error('Exception: %s', str(e))
        result = format_resolved_reference(returned_format,
                                           resolved='0.0 %s' % (19 * '.'),
                                           reference=reference)

    return result
Пример #4
0
def xml_post():
    """

    :return:
    """
    try:
        payload = request.get_json(force=True)  # post data in json
    except:
        payload = dict(request.form)  # post data in form encoding

    if not payload:
        return {'error': 'no information received'}, 400
    if 'parsed_reference' not in payload:
        return {
            'error':
            'no reference found in payload (parameter name is `reference`)'
        }, 400

    parsed_references = payload['parsed_reference']

    current_app.logger.debug(
        'received POST request with {count} references to resolve in xml mode.'
        .format(count=len(parsed_references)))

    returned_format = request.headers.get('Accept', 'text/plain')

    results = []
    for parsed_reference in parsed_references:
        try:
            resolved = str(solve_reference(Hypotheses(parsed_reference)))
            if resolved.startswith('0.0'):
                raise "Not Resolved"
            result = format_resolved_reference(
                returned_format,
                resolved=resolved,
                reference=parsed_reference['refstr'])
        except Exception as e:
            current_app.logger.error('Exception: {error}'.format(error=str(e)))
            # lets attempt to resolve using the text model
            if 'refplaintext' in parsed_reference:
                reference = urllib.parse.unquote(
                    parsed_reference['refplaintext'])
                if bool(RE_NUMERIC_VALUE.search(reference)):
                    current_app.logger.info(
                        'attempting to resolve the reference=`{reference}` in text mode now'
                        .format(reference=reference))
                    try:
                        parsed_ref = text_parser(reference)
                        if parsed_ref:
                            result = format_resolved_reference(
                                returned_format,
                                resolved=str(
                                    solve_reference(Hypotheses(parsed_ref))),
                                reference=reference,
                                cache=True)
                    except Exception as e:
                        current_app.logger.error(
                            'Exception: {error}'.format(error=str(e)))
                        result = format_resolved_reference(returned_format,
                                                           resolved='0.0 %s' %
                                                           (19 * '.'),
                                                           reference=reference)
                        continue
                else:
                    result = format_resolved_reference(
                        returned_format,
                        resolved='0.0 %s' % (19 * '.'),
                        reference=parsed_reference['refstr'])
                    continue
            else:
                result = format_resolved_reference(
                    returned_format,
                    resolved='0.0 %s' % (19 * '.'),
                    reference=parsed_reference['refstr'])
                continue
        finally:
            results.append(result)

    if len(results) == len(parsed_reference):
        if returned_format == 'application/json':
            return return_response({'resolved': results}, 200,
                                   'application/json; charset=UTF8')
        return return_response({'resolved': '\n'.join(results)}, 200,
                               'application/json; charset=UTF8')
    return return_response({'error': 'unable to resolve any references'}, 400,
                           'text/plain; charset=UTF8')