Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write of manually created duplicate blocks fails when no raw bibtex was specified #400

Open
2 tasks
claell opened this issue Sep 20, 2023 · 2 comments
Open
2 tasks

Comments

@claell
Copy link
Contributor

claell commented Sep 20, 2023

Describe the bug
Writing a library with duplicate blocks leads to:

WARNING:root:Unknown block type <class 'bibtexparser.model.DuplicateBlockKeyBlock'>
Traceback (most recent call last):
  File "n:\Gruppenplatte\Veröffentlichungen\2023\Systems Engineering Journal (INCOSE)\Scopus retrieval\test.py", line 24, in <module>
    bibtexparser.write_file("my_new_file.bib", bib_library)
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 136, in write_file
    bibtex_str = write_string(
                 ^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 168, in write_string
    return write(library, bibtex_format=bibtex_format)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 106, in write
    string_block_pieces = _treat_block(bibtex_format, block)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 127, in _treat_block
    string_block_pieces = _treat_failed_block(block, bibtex_format)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 71, in _treat_failed_block
    lines = len(block.raw.splitlines())
                ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'
PS N:\Gruppenplatte\Veröffentlichungen\2023\Systems Engineering Journal (INCOSE)> & C:/Users/Ellsel/AppData/Local/Programs/Python/Python311/python.exe "n:/Gruppenplatte/Veröffentlichungen/2023/Systems Engineering Journal (INCOSE)/Scopus retrieval/test.py"
{'test': Entry(entry_type=`article`, key=`test`, fields=`[Field(key=`author`, value=`test`, start_line=None)]`, start_line=None)}
WARNING:root:Unknown block type <class 'bibtexparser.model.DuplicateBlockKeyBlock'>
Traceback (most recent call last):
  File "n:\Gruppenplatte\Veröffentlichungen\2023\Systems Engineering Journal (INCOSE)\Scopus retrieval\test.py", line 24, in <module>
    bibtexparser.write_file("my_new_file.bib", bib_library)
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 136, in write_file
    bibtex_str = write_string(
                 ^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\entrypoint.py", line 168, in write_string
    return write(library, bibtex_format=bibtex_format)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 106, in write
    string_block_pieces = _treat_block(bibtex_format, block)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 127, in _treat_block
    string_block_pieces = _treat_failed_block(block, bibtex_format)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ellsel\AppData\Local\Programs\Python\Python311\Lib\site-packages\bibtexparser\writer.py", line 71, in _treat_failed_block
    lines = len(block.raw.splitlines())
                ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'

Reproducing

Version: Latest from PyPI

import bibtexparser
from bibtexparser import *

bib_library = bibtexparser.Library()

fields = []

fields.append(bibtexparser.model.Field("author", "test"))

entry = bibtexparser.model.Entry("article", "test", fields)

entry1 = bibtexparser.model.Entry("article", "test", fields)

bib_library.add(entry)

bib_library.add(entry1)

print(bib_library.entries_dict)

bibtexparser.write_file("my_new_file.bib", bib_library)

Workaround
Checking for duplicate entries beforehand.

Remaining Questions (Optional)
Please tick all that apply:

  • I would be willing to to contribute a PR to fix this issue.
  • This issue is a blocker, I'd be greatful for an early fix.
@MiWeiss
Copy link
Collaborator

MiWeiss commented Sep 20, 2023

When writing any ParsingFailedBlock, including duplicate key blocks, the writer defaults to writing the raw bibtex back which was observed when parsing. In your case, as you manually created the entries, you did not specify such raw bibtex (which is an optional constructor parameter). In that sense, the library is working as intended.

In principle, changing this would be rather easy, as we could have the writer write the block.ignore_error_block (which is the block with the duplicate key) whenever a DuplicateBlockKeyBlock is faced. However, I'd be reluctant to merge such a change as the current rule "failed block => print raw" is nice and easy to understand and deviating from this will make things harder to grasp for most users.

I need to think about this for a bit and will get back asap.

@MiWeiss MiWeiss changed the title Write of files with duplicate blocks fails Write of files with manually created duplicate blocks fails when no raw bibtex was specified Sep 20, 2023
@MiWeiss MiWeiss changed the title Write of files with manually created duplicate blocks fails when no raw bibtex was specified Write of manually created duplicate blocks fails when no raw bibtex was specified Sep 20, 2023
@claell
Copy link
Contributor Author

claell commented Sep 21, 2023

How about only writing the block in case there is no raw content? One could also add some sanity check for the block, before writing it.

That's what I can currently think of (apart from changing the failed blocks logic).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants