From 45fc2575757b4f9da33b3eacc784a33ffae6385a Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Tue, 28 Apr 2015 11:45:44 +0200 Subject: [PATCH] Fixes #32. Use key depending on bibtexparser version in endecoder. --- pubs/bibstruct.py | 2 ++ pubs/endecoder.py | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pubs/bibstruct.py b/pubs/bibstruct.py index fafa533..76c0854 100644 --- a/pubs/bibstruct.py +++ b/pubs/bibstruct.py @@ -7,6 +7,8 @@ from .p3 import ustr, uchr # citekey stuff +TYPE_KEY = 'type' + CONTROL_CHARS = ''.join(map(uchr, list(range(0, 32)) + list(range(127, 160)))) CITEKEY_FORBIDDEN_CHARS = '@\'\\,#}{~%/' # '/' is OK for bibtex but forbidden # here since we transform citekeys into filenames diff --git a/pubs/endecoder.py b/pubs/endecoder.py index 0f59eca..1c92cf4 100644 --- a/pubs/endecoder.py +++ b/pubs/endecoder.py @@ -12,14 +12,23 @@ except ImportError: import yaml +from .bibstruct import TYPE_KEY """Important notice: All functions and methods in this file assume and produce unicode data. """ +if bp.__version__ > "0.6.0": + BP_ID_KEY = 'ID' + BP_ENTRYTYPE_KEY = 'ENTRYTYPE' +else: + BP_ID_KEY = 'id' + BP_ENTRYTYPE_KEY = 'type' + + def sanitize_citekey(record): - record['id'] = record['id'].strip('\n') + record[BP_ID_KEY] = record[BP_ID_KEY].strip('\n') return record @@ -44,8 +53,8 @@ def customizations(record): return record bibfield_order = ['author', 'title', 'journal', 'institution', 'publisher', - 'year', 'month', 'number', 'pages', 'link', 'doi', 'id', - 'note', 'abstract'] + 'year', 'month', 'number', 'pages', 'link', 'doi', 'note', + 'abstract'] class EnDecoder(object): @@ -88,7 +97,7 @@ class EnDecoder(object): @staticmethod def _encode_bibentry(citekey, bibentry): - bibraw = '@{}{{{},\n'.format(bibentry['type'], citekey) + bibraw = '@{}{{{},\n'.format(bibentry[TYPE_KEY], citekey) bibentry = copy.copy(bibentry) for key in bibfield_order: if key in bibentry: @@ -96,7 +105,7 @@ class EnDecoder(object): bibraw += ' {} = {{{}}},\n'.format( key, EnDecoder._encode_field(key, value)) for key, value in bibentry.items(): - if key != 'type': + if key != TYPE_KEY: bibraw += ' {} = {{{}}},\n'.format( key, EnDecoder._encode_field(key, value)) bibraw += '}\n' @@ -107,9 +116,12 @@ class EnDecoder(object): try: entries = bp.bparser.BibTexParser( bibdata, customization=customizations).get_entry_dict() - # Remove 'id' attribute which is stored as citekey + # Remove id from bibtexparser attribute which is stored as citekey for e in entries: - entries[e].pop('id') + entries[e].pop(BP_ID_KEY) + # Convert bibtexparser entrytype key to internal 'type' + t = entries[e].pop(BP_ENTRYTYPE_KEY) + entries[e][TYPE_KEY] = t if len(entries) > 0: return entries except Exception: