Exemple #1
0
def test_processing_invariant(test_string):
    entry = bibpy.read_string(test_string,
                              postprocess=True,
                              remove_braces=True).entries[0]

    assert entry.author == ['James Conway', 'Archer Sterling']
    assert entry.xdata == ['key1', 'key2', 'key3', 'key4', 'key5']
    assert entry.urldate == bibpy.date.DateRange.fromstring('2017-01-14')
    assert entry.keywords == ['parsing', 'computer science', 'databases']
    assert entry.year == 1957
    assert entry.month == 'November'
    assert entry.pages == (11, 20)
    assert entry.msg == '"Part of " # var # " string"'
    assert entry.foreword == ['Jan Leo and the Editors']

    entry = bibpy.read_string(bibpy.write_string([entry]),
                              postprocess=True,
                              remove_braces=True).entries[0]

    assert entry.author == ['James Conway', 'Archer Sterling']
    assert entry.xdata == ['key1', 'key2', 'key3', 'key4', 'key5']
    assert entry.urldate == bibpy.date.DateRange.fromstring('2017-01-14')
    assert entry.keywords == ['parsing', 'computer science', 'databases']
    assert entry.year == 1957
    assert entry.month == 'November'
    assert entry.pages == (11, 20)
    assert entry.msg == '"Part of " # var # " string"'
    assert entry.foreword == ['Jan Leo and the Editors']
Exemple #2
0
def test_reading_encodings():
    bibpy.read_string('@article{key,author={James Grönroos}}', format='bibtex')

    bibpy.read_file(
        'tests/data/simple_1.bib',
        format='relaxed',
        encoding='ascii'
    )

    bibpy.read_file(
        'tests/data/simple_1.bib',
        format='relaxed',
        encoding='utf-8'
    )

    bibpy.read_file(
        'tests/data/simple_1.bib',
        format='relaxed',
        encoding='iso-8859-1'
    )

    with pytest.raises(UnicodeDecodeError):
        bibpy.read_file(
            'tests/data/iso-8859-1.bib',
            format='bibtex',
            encoding='utf-8'
        )
Exemple #3
0
def test_reading_errors():
    bibpy.read_string(
        'tests/data/bibtex_missing_requirements.bib',
        format='bibtex'
    )

    bibpy.read_string(
        'tests/data/bibtex_missing_requirements.bib',
        format='bibtex'
    )
Exemple #4
0
def test_single_capitalized_preamble_entry():
    contents = '$1$ LaTeX code $\\sqrt{2}'
    s = '@Preamble( ' + contents + ' )'
    preambles = bibpy.read_string(s, 'bibtex').preambles

    assert type(preambles[0]) is bibpy.entry.Preamble
    assert preambles[0].value == contents
Exemple #5
0
def test_single_comment_entry():
    contents = ' I can write whatever I want here '
    s = '@comment{ I can write whatever I want here }'
    comment_entries = bibpy.read_string(s, 'bibtex').comment_entries

    assert type(comment_entries[0]) is bibpy.entry.Comment
    assert comment_entries[0].value == contents
def test_single_preamble_entry():
    contents = "$1$ LaTeX code $\\sqrt{2}"
    s = "@preamble( " + contents + " )"
    preambles = bibpy.read_string(s, 'bibtex').preambles

    assert type(preambles[0]) is bibpy.entry.Preamble
    assert preambles[0].value == contents
Exemple #7
0
def retrieve(doi, source='https://doi.org/{0}', raw=False, **options):
    """Download a bibtex file specified by a digital object identifier.

    The source is a URL containing a single positional format specifier which
    is where the requested doi should appear.

    By default, the data from the doi is parsed by bibpy. If raw is True, the
    raw string is returned instead.

    The options kwargs correspond to the arguments normally passed to
    :py:func:`bibpy.read_string`.

    """
    req = Request(source.format(doi))
    req.add_header('accept', 'application/x-bibtex')
    handle = None

    try:
        handle = urlopen(req)
        contents = handle.read()

        if raw:
            return contents
        else:
            return bibpy.read_string(
                contents.decode('utf-8'),
                **options
            ).entries[0]
    finally:
        if handle:
            handle.close()
Exemple #8
0
def test_single_string_entry():
    variable = 'var'
    value = 'March'
    s = '@string{ ' + variable + ' = ' + value + ' }'
    strings = bibpy.read_string(s, 'bibtex').strings

    assert type(strings[0]) is bibpy.entry.String
    assert strings[0].variable == variable
    assert strings[0].value == value
def test_single_string_entry():
    variable = "var"
    value = "March"
    s = "@string{ " + variable + " = " + value + " }"
    strings = bibpy.read_string(s, 'bibtex').strings

    assert type(strings[0]) is bibpy.entry.String
    assert strings[0].variable == variable
    assert strings[0].value == value
Exemple #10
0
def test_single_entry():
    s = "@article{example_key,author={McLovin'}," +\
        'title={Hawaiian Organ Donation}}'

    entries = bibpy.read_string(s, 'bibtex').entries

    assert type(entries[0]) is bibpy.entry.Entry
    assert entries[0].bibtype == 'article'
    assert entries[0].bibkey == 'example_key'
    assert entries[0].author == 'McLovin\''
    assert entries[0].title == 'Hawaiian Organ Donation'
Exemple #11
0
def test_reading_empty():
    bibpy.read_string('', 'bibtex')
    bibpy.read_string('', 'biblatex')
    bibpy.read_string('', 'mixed')
    bibpy.read_string('', 'relaxed')

    with pytest.raises(IOError):
        bibpy.read_file('', 'bibtex')

    with pytest.raises(IOError):
        bibpy.read_file('', 'biblatex')

    with pytest.raises(IOError):
        bibpy.read_file('', 'mixed')

    with pytest.raises(IOError):
        bibpy.read_file('', 'relaxed')
Exemple #12
0
def test_single_comment():
    s = 'This is a comment'
    comments = bibpy.read_string(s, 'bibtex', ignore_comments=False).comments

    assert comments[0] == s
def test_incorrect_format():
    with pytest.raises(KeyError):
        bibpy.read_string('', 'gibberish')
Exemple #14
0
def test_unicode_string():
    bibpy.read_string('@article{keåy, author = {荡 襡}, title = "â"}')