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

Replace pybtex or make it optional? #4285

Open
DanielYang59 opened this issue Feb 9, 2025 · 1 comment
Open

Replace pybtex or make it optional? #4285

DanielYang59 opened this issue Feb 9, 2025 · 1 comment

Comments

@DanielYang59
Copy link
Contributor

DanielYang59 commented Feb 9, 2025

Looks like development on pybtex might have stalled (no release since Jan 2021), and the following would error out on MacOS Python 3.13:

from pybtex.database import BibliographyData, Entry

Gives:

Traceback (most recent call last):
  File "/Users/yang/developer/pymatgen/debug/test_pybtex_install.py", line 1, in <module>
    from pybtex.database import BibliographyData, Entry
  File "/Users/yang/developer/pymatgen/venv313/lib/python3.13/site-packages/pybtex/database/__init__.py", line 44, in <module>
    from pybtex.plugin import find_plugin
  File "/Users/yang/developer/pymatgen/venv313/lib/python3.13/site-packages/pybtex/plugin/__init__.py", line 26, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

This is an already fixed issue from pybtex's side but they haven't got a release with this patch yet.

Should we consider replace it (python-bibtexparser for example) or make it optional as it's only used in two places across the code base:

cif.CifParser.get_bibtex_string

def get_bibtex_string(self) -> str:
"""Get BibTeX reference from CIF file.
Args:
data:
Returns:
BibTeX string.
"""
try:
from pybtex.database import BibliographyData, Entry
except ImportError:
raise RuntimeError("Bibliographic data extraction requires pybtex.")

Helper function needed by util.provenance.StructureNL

def is_valid_bibtex(reference: str) -> bool:
"""Use pybtex to validate that a reference is in proper BibTeX format.
Args:
reference: A String reference in BibTeX format.
Returns:
bool: True if reference is valid bibtex.
"""
# str is necessary since pybtex seems to have an issue with unicode. The
# filter expression removes all non-ASCII characters.
str_io = StringIO(reference.encode("ascii", "ignore").decode("ascii"))
parser = bibtex.Parser()
errors.set_strict_mode(enable=False)
bib_data = parser.parse_stream(str_io)
return len(bib_data.entries) > 0


Related:

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented Feb 9, 2025

I would give it a try and make it optional for now (0b25745)

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

No branches or pull requests

1 participant