Skip to content

Commit

Permalink
Merge pull request #145 from yarikoptic/enh-citeprocver
Browse files Browse the repository at this point in the history
ENH: Provide more informative message whenever using older citeproc without encoding arg
  • Loading branch information
yarikoptic committed Mar 3, 2019
2 parents d30c3c9 + d1d5cf4 commit 32e6322
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions duecredit/io.py
Expand Up @@ -28,6 +28,7 @@
from .config import CACHE_DIR, DUECREDIT_FILE
from .entries import BibTeX, Doi, Text, Url
from .log import lgr
from .versions import external_versions

_PREFERRED_ENCODING = locale.getpreferredencoding()
platform_system = platform.system().lower()
Expand Down Expand Up @@ -310,8 +311,16 @@ def format_bibtex(bibtex_entry, style='harvard1'):
# we should be able to provide encoding argument
bib_source = cpBibTeX(fname, encoding='utf-8')
except Exception as e:
lgr.error("Failed to process BibTeX file %s: %s" % (fname, e))
return "ERRORED: %s" % str(e)
msg = "Failed to process BibTeX file %s: %s." % (fname, e)
citeproc_version = external_versions['citeproc']
if 'unexpected keyword argument' in str(e) and \
citeproc_version and citeproc_version < '0.4':
err = "need a newer citeproc-py >= 0.4.0"
msg += " You might just " + err
else:
err = str(e)
lgr.error(msg)
return "ERRORED: %s" % err
finally:
# return warnings back
warnings.filters = old_filters
Expand Down
15 changes: 15 additions & 0 deletions duecredit/versions.py
Expand Up @@ -9,11 +9,13 @@
"""Module to help maintain a registry of versions for external modules etc
"""
import sys
import pkg_resources
from os import linesep
from six import string_types

from distutils.version import StrictVersion, LooseVersion


# To depict an unknown version, which can't be compared by mistake etc
class UnknownVersion:
"""For internal use
Expand Down Expand Up @@ -54,6 +56,19 @@ def _deduce_version(klass, module):
# Generate string representation
version = ".".join(str(x) for x in version)

if not version:
# Try pkg_resources
# module name might be different, and I found no way to
# deduce it for citeproc which comes from "citeproc-py"
# distribution
modname = module.__name__
try:
version = pkg_resources.get_distribution(
{'citeproc': 'citeproc-py'}.get(modname, modname)
).version
except Exception:
pass # oh well - no luck either

if version:
try:
return StrictVersion(version)
Expand Down

0 comments on commit 32e6322

Please sign in to comment.