From 0b64bf086ed11d1b40e9454db76bb4417eee73a3 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Mon, 28 Apr 2014 20:53:49 +0200 Subject: [PATCH] Update for latest bibtexparser (>= 0.5.3). Breaks compatibility with previous versions. Also fixes the UnicodeWarning. --- pubs/endecoder.py | 24 +++++++++++++++--------- readme.md | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pubs/endecoder.py b/pubs/endecoder.py index c2f2616..64caabd 100644 --- a/pubs/endecoder.py +++ b/pubs/endecoder.py @@ -1,6 +1,5 @@ from __future__ import print_function, absolute_import, division, unicode_literals -import io import copy try: @@ -13,7 +12,6 @@ except ImportError: import yaml - """Important notice: All functions and methods in this file assume and produce unicode data. """ @@ -44,7 +42,9 @@ def customizations(record): return record -bibfield_order = ['author', 'title', 'journal', 'institution', 'publisher', 'year', 'month', 'number', 'pages', 'link', 'doi', 'id', 'note', 'abstract'] +bibfield_order = ['author', 'title', 'journal', 'institution', 'publisher', + 'year', 'month', 'number', 'pages', 'link', 'doi', 'id', + 'note', 'abstract'] class EnDecoder(object): @@ -60,7 +60,7 @@ class EnDecoder(object): def encode_metadata(self, metadata): return yaml.safe_dump(metadata, allow_unicode=True, - encoding=None, indent = 4) + encoding=None, indent=4) def decode_metadata(self, metadata_raw): return yaml.safe_load(metadata_raw) @@ -99,14 +99,20 @@ class EnDecoder(object): bibraw += '}\n' return bibraw - def decode_bibdata(self, bibdata_raw): + def decode_bibdata(self, bibdata_unicode): """""" - bibdata_stream = io.StringIO(bibdata_raw) - return self._decode_bibdata(bibdata_stream) + if isinstance(bibdata_unicode, str): + # Nothing to do for python3 + bibdata_string = bibdata_unicode + else: + # For python2 bibtexparser expects utf8 encoded string + bibdata_string = bibdata_unicode.encode('utf8') + return self._decode_bibdata(bibdata_string) - def _decode_bibdata(self, bibdata_stream): + def _decode_bibdata(self, bibdata_string): try: - entries = bp.bparser.BibTexParser(bibdata_stream, customization=customizations).get_entry_dict() + entries = bp.bparser.BibTexParser( + bibdata_string, customization=customizations).get_entry_dict() # Remove 'id' attribute which is stored as citekey for e in entries: entries[e].pop('id') diff --git a/readme.md b/readme.md index 922ff2b..4c7ab57 100644 --- a/readme.md +++ b/readme.md @@ -34,7 +34,7 @@ Requirements - python >= 2.6 - [dateutil](http://labix.org/python-dateutil) - [pyYaml](http://pyyaml.org) -- [bibtexparser](https://github.com/sciunto/python-bibtexparser) +- [bibtexparser](https://github.com/sciunto/python-bibtexparser) >= 0.5.3 Authors