Skip to content

Commit

Permalink
Merge pull request #57 from yarikoptic/enh-moreinjections
Browse files Browse the repository at this point in the history
ENH: versions -- provide dumps, keys, __contains__
  • Loading branch information
yarikoptic committed Sep 21, 2015
2 parents 622b040 + 5bd236b commit 4282bc2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
31 changes: 26 additions & 5 deletions duecredit/io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the duecredit package for the
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

from citeproc.source.bibtex import BibTeX as cpBibTeX
import citeproc as cp

import time
from collections import defaultdict, Iterator
import copy
import os
Expand Down Expand Up @@ -32,11 +41,23 @@ def import_doi(doi):
# else -- fetch it
headers = {'Accept': 'text/bibliography; style=bibtex'}
url = 'http://dx.doi.org/' + doi
r = requests.get(url, headers=headers)
r.encoding = 'UTF-8'
bibtex = r.text.strip()
retries = 10
while retries > 0:
r = requests.get(url, headers=headers)
r.encoding = 'UTF-8'
bibtex = r.text.strip()
if bibtex.startswith('@'):
# no more retries necessary
break
lgr.warning("Failed to obtain bibtex from doi.org, retrying...")
time.sleep(0.5) # give some time to the server
retries -= 1
status_code = r.status_code
if not bibtex.startswith('@'):
raise ValueError('wrong doi specified')
raise ValueError('Query %(url)s for BibTeX for a DOI %(doi)s (wrong doi?) has failed. '
'Response code %(status_code)d. '
#'BibTeX response was: %(bibtex)s'
% locals())
if not exists(cached):
cache_dir = dirname(cached)
if not exists(cache_dir):
Expand Down
9 changes: 9 additions & 0 deletions duecredit/tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the duecredit package for the
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

from ..collector import DueCreditCollector, Citation
from .test_collector import _sample_bibtex, _sample_doi
from ..entries import BibTeX, DueCreditEntry, Doi
Expand Down
22 changes: 21 additions & 1 deletion duecredit/tests/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

from os import linesep

from ..version import __version__
from ..versions import ExternalVersions

from nose.tools import assert_true, assert_false
from nose.tools import assert_equal, assert_greater_equal, assert_greater
from nose.tools import assert_raises
from nose import SkipTest
Expand All @@ -27,6 +30,11 @@ def test_external_versions_basic():
# and it could be compared
assert_greater_equal(ev['duecredit'], __version__)
assert_greater(ev['duecredit'], '0.1')
assert_equal(list(ev.keys()), ['duecredit'])
assert_true('duecredit' in ev)
assert_false('unknown' in ev)

assert_equal(ev.dumps(), "Versions: duecredit=%s" % __version__)

# For non-existing one we get None
assert_equal(ev['duecreditnonexisting'], None)
Expand All @@ -47,11 +55,19 @@ def test_external_versions_basic():
from duecredit.tests import mod
assert_equal(ev[mod], mod.__version__)

# Check that we can get a copy of the verions
versions_dict = ev.versions
versions_dict['duecredit'] = "0.0.1"
assert_equal(versions_dict['duecredit'], "0.0.1")
assert_equal(ev['duecredit'], __version__)


def test_external_versions_unknown():
assert_equal(str(ExternalVersions.UNKNOWN), 'UNKNOWN')

def test_external_versions_popular_packages():
ev = ExternalVersions()

def _test_external(modname):
try:
exec("import %s" % modname, locals(), globals())
Expand All @@ -65,4 +81,8 @@ def _test_external(modname):

for modname in ('scipy', 'numpy', 'mvpa2', 'sklearn', 'statsmodels', 'pandas',
'matplotlib', 'psychopy'):
yield _test_external, modname
yield _test_external, modname

# more of a smoke test
assert_false(linesep in ev.dumps())
assert_true(ev.dumps(indent=True).endswith(linesep))
38 changes: 36 additions & 2 deletions duecredit/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the duecredit package for the
# copyright and license terms. Originates from datalad package distributed
# under MIT license
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Module to help maintain a registry of versions for external modules etc
"""
import sys
from os import linesep
from six import string_types

from distutils.version import StrictVersion, LooseVersion
Expand Down Expand Up @@ -84,4 +84,38 @@ def __getitem__(self, module):

return self._versions.get(modname, self.UNKNOWN)

def keys(self):
"""Return names of the known modules"""
return self._versions.keys()

def __contains__(self, item):
return item in self._versions

@property
def versions(self):
"""Return dictionary (copy) of versions"""
return self._versions.copy()

def dumps(self, indent=False, preamble="Versions:"):
"""Return listing of versions as a string
Parameters
----------
indent: bool or str, optional
If set would instruct on how to indent entries (if just True, ' '
is used). Otherwise returned in a single line
preamble: str, optional
What preamble to the listing to use
"""
if indent and (indent is True):
indent = ' '
items = ["%s=%s" % (k, self._versions[k]) for k in sorted(self._versions)]
out = "%s" % preamble
if indent:
out += (linesep + indent).join([''] + items) + linesep
else:
out += " " + ' '.join(items)
return out


external_versions = ExternalVersions()

0 comments on commit 4282bc2

Please sign in to comment.