Skip to content

Commit

Permalink
Merge c4ecc50 into f4d87f0
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikoptic committed May 28, 2016
2 parents f4d87f0 + c4ecc50 commit d2ce406
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
16 changes: 10 additions & 6 deletions duecredit/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

_PREFERRED_ENCODING = locale.getpreferredencoding()


def get_doi_cache_file(doi):
return os.path.join(CACHE_DIR, doi)

Expand All @@ -44,7 +45,6 @@ def import_doi(doi, sleep=0.5, retries=10):
return doi

# else -- fetch it
#headers = {'Accept': 'text/bibliography; style=bibtex'}
headers = {'Accept': 'application/x-bibtex; charset=utf-8'}
url = 'http://dx.doi.org/' + doi
while retries > 0:
Expand Down Expand Up @@ -91,11 +91,14 @@ def __init__(self, fd, collector):
self.fd = fd
self.collector = collector

def _filter_citations(self, tags=None):
def _get_collated_citations(self, tags=None, all_=None):
"""Given all the citations, filter only those that the user wants and
those that were actually used"""
if not tags:
tags = os.environ.get('DUECREDIT_REPORT_TAGS', 'reference-implementation,implementation').split(',')
if all_ is None:
# consult env var
all_ = os.environ.get('DUECREDIT_REPORT_ALL', '').lower() in {'1', 'true', 'yes', 'on'}
tags = set(tags)

citations = self.collector.citations
Expand Down Expand Up @@ -125,8 +128,9 @@ def _filter_citations(self, tags=None):
cited_modobj = list(modules) + list(objects)
for package in cited_packages:
package_citations = packages[package]
if list(filter(lambda x: x.cite_module, package_citations)) or \
list(filter(lambda x: _is_contained(package, x), cited_modobj)):
if all_ or \
list(filter(lambda x: x.cite_module, package_citations)) or \
list(filter(lambda x: _is_contained(package, x), cited_modobj)):
continue
else:
# we don't need it
Expand Down Expand Up @@ -161,7 +165,7 @@ def _format_citations(citations, citation_nr):

def dump(self, tags=None):
# get 'model' of citations
packages, modules, objects = self._filter_citations(tags)
packages, modules, objects = self._get_collated_citations(tags)
# put everything into a single dict
pmo = {}
pmo.update(packages)
Expand Down Expand Up @@ -307,7 +311,7 @@ def __init__(self, fd, collector):
super(BibTeXOutput, self).__init__(fd, collector)

def dump(self, tags=None):
packages, modules, objects = self._filter_citations(tags)
packages, modules, objects = self._get_collated_citations(tags)
# get all the citations in order
pmo = {}
pmo.update(packages)
Expand Down
34 changes: 29 additions & 5 deletions duecredit/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_output():

output = Output(None, collector)

packages, modules, objects = output._filter_citations(tags=['*'])
packages, modules, objects = output._get_collated_citations(tags=['*'])

assert_equal(len(packages), 1)
assert_equal(len(modules), 1)
Expand All @@ -115,7 +115,7 @@ def test_output():

output = Output(None, collector)

packages, modules, objects = output._filter_citations(tags=['*'])
packages, modules, objects = output._get_collated_citations(tags=['*'])

assert_equal(len(packages), 0)
assert_equal(len(modules), 1)
Expand All @@ -132,7 +132,7 @@ def test_output():

output = Output(None, collector)

packages, modules, objects = output._filter_citations(tags=['*'])
packages, modules, objects = output._get_collated_citations(tags=['*'])

assert_equal(len(packages), 1)
assert_equal(len(modules), 1)
Expand All @@ -152,7 +152,7 @@ def test_output():

output = Output(None, collector)

packages, modules, objects = output._filter_citations(tags=['*'])
packages, modules, objects = output._get_collated_citations(tags=['*'])

assert_equal(len(packages), 1)
assert_equal(len(packages['package']), 2)
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_output():

output = Output(None, collector)

packages, modules, objects = output._filter_citations(tags=['edu'])
packages, modules, objects = output._get_collated_citations(tags=['edu'])

assert_equal(len(packages), 1)
assert_equal(len(packages['package']), 1)
Expand All @@ -192,6 +192,30 @@ def test_output():
assert_equal(modules['package.module'][0],
collector.citations[('package.module', entry.get_key())])


def test_output_return_all():
entry = BibTeX(_sample_bibtex)
entry2 = BibTeX(_sample_bibtex2)

# normal use
collector = DueCreditCollector()
collector.cite(entry, path='package')
collector.cite(entry2, path='package2')

output = Output(None, collector)

packages, modules, objects = output._get_collated_citations(tags=['*'])
assert_false(packages)
assert_false(modules)
assert_false(objects)

with patch.dict(os.environ, {'DUECREDIT_REPORT_ALL': '1'}):
packages, modules, objects = output._get_collated_citations(tags=['*'])
assert_equal(len(packages), 2)
assert_false(modules)
assert_false(objects)


def test_text_output():
entry = BibTeX(_sample_bibtex)
entry2 = BibTeX(_sample_bibtex2)
Expand Down

0 comments on commit d2ce406

Please sign in to comment.