Пример #1
0
def test_find_references(literature_records):
    reference_names = [
        ("1979PhLB...80..360E", 1),
        ("Beacom:2004yd", 1),
        ("hep-th/0501240", 3),
        ("CERN-W5013", 6),
        ("Garcia:2020ay", 8),
        ("JHEP.0412.015", 9),
    ]

    expected_references_bibtex = [
        '@article{1979PhLB...80..360E,\n    author = "Ellis, John R.",\n    title = "{Baryon Number Generation in Grand Unified Theories}"\n}\n',
        '@article{Beacom:2004yd,\n    author = "Beacom, John F.",\n    title = "{Neutrinoless universe}"\n}\n',
        '@article{hep-th/0501240,\n    author = "Bern, Zvi",\n    title = "{On-shell recurrence relations for one-loop QCD amplitudes}",\n    eprint = "hep-th/0501240",\n    archivePrefix = "arXiv"\n}\n',
        '@article{JHEP.0412.015,\n    author = "Dixon, Lance J.",\n    title = "{MHV rules for Higgs plus multi-gluon amplitudes}",\n    journal = "JHEP",\n    volume = "12",\n    pages = "015",\n    year = "2004"\n}\n',
    ]

    expected_references_latex_us = [
        "%\\cite{1979PhLB...80..360E}\n\\bibitem{1979PhLB...80..360E}\nJ.~R.~Ellis,\n%``Baryon Number Generation in Grand Unified Theories,''\n%0 citations counted in INSPIRE as of 12 Jun 2020",
        "%\\cite{Beacom:2004yd}\n\\bibitem{Beacom:2004yd}\nJ.~F.~Beacom,\n%``Neutrinoless universe,''\n%0 citations counted in INSPIRE as of 12 Jun 2020",
        "%\\cite{hep-th/0501240}\n\\bibitem{hep-th/0501240}\nZ.~Bern,\n%``On-shell recurrence relations for one-loop QCD amplitudes,''\n[arXiv:hep-th/0501240 [hep-th]].\n%0 citations counted in INSPIRE as of 12 Jun 2020",
        "%\\cite{JHEP.0412.015}\n\\bibitem{JHEP.0412.015}\nL.~J.~Dixon,\n%``MHV rules for Higgs plus multi-gluon amplitudes,''\nJHEP \\textbf{12}, 015 (2004)\n%0 citations counted in INSPIRE as of 12 Jun 2020",
    ]

    expected_references_latex_eu = [
        "%\\cite{1979PhLB...80..360E}\n\\bibitem{1979PhLB...80..360E}\nJ.~R.~Ellis,\n%``Baryon Number Generation in Grand Unified Theories,''\n%0 citations counted in INSPIRE as of 12 Jun 2020",
        "%\\cite{Beacom:2004yd}\n\\bibitem{Beacom:2004yd}\nJ.~F.~Beacom,\n%``Neutrinoless universe,''\n%0 citations counted in INSPIRE as of 12 Jun 2020",
        "%\\cite{hep-th/0501240}\n\\bibitem{hep-th/0501240}\nZ.~Bern,\n%``On-shell recurrence relations for one-loop QCD amplitudes,''\n[arXiv:hep-th/0501240 [hep-th]].\n%0 citations counted in INSPIRE as of 12 Jun 2020",
        "%\\cite{JHEP.0412.015}\n\\bibitem{JHEP.0412.015}\nL.~J.~Dixon,\n%``MHV rules for Higgs plus multi-gluon amplitudes,''\nJHEP \\textbf{12} (2004), 015\n%0 citations counted in INSPIRE as of 12 Jun 2020",
    ]

    expected_errors = [
        {
            "ref": "CERN-W5013",
            "line": 6,
            "type": "ambiguous"
        },
        {
            "ref": "Garcia:2020ay",
            "line": 8,
            "type": "not found"
        },
    ]

    references, errors = find_references(reference_names, "bibtex")
    assert references == expected_references_bibtex

    references, errors = find_references(reference_names, "latex_eu")
    assert references == expected_references_latex_eu

    references, errors = find_references(reference_names, "latex_us")
    assert references == expected_references_latex_us

    assert errors == expected_errors
Пример #2
0
def generate_bibliography():
    content_length = request.content_length
    if content_length is not None and content_length > 10 * 1024 * 1024:
        raise FileTooBigError

    requested_format = request.args.get("format")
    bytes_file = request.files["file"]

    if not allowed_file(bytes_file.filename):
        raise FileFormatNotSupportedError

    text_file = TextIOWrapper(bytes_file, errors="ignore")

    reference_names = get_references(text_file)
    references, errors = find_references(reference_names, requested_format)

    if not references:
        raise NoReferencesFoundError

    file_data = "\n\n".join(references).encode("utf-8")
    key = hash_data(file_data)
    file_data = BytesIO(file_data)
    filename = get_filename(bytes_file.filename, requested_format)
    mime_type = get_mimetype(requested_format)
    bucket = current_app.config.get("S3_BIBLIOGRAPHY_GENERATOR_BUCKET")

    current_s3_instance.upload_file(file_data, key, filename, mime_type,
                                    current_app.config["S3_FILE_ACL"], bucket)
    download_url = current_s3_instance.get_s3_url(key, bucket)

    formatted_errors = []
    for error in errors:
        if error["type"] == "ambiguous":
            formatted_errors.append({
                "message":
                f"Ambiguous reference to {error['ref']} on line {error['line']}"
            })
        else:
            formatted_errors.append({
                "message":
                f"Reference to {error['ref']} not found on line {error['line']}"
            })

    response = {
        "data": {
            "download_url": download_url
        },
        "errors": formatted_errors
    }

    return jsonify(response)