Skip to content

Commit

Permalink
Merge pull request #91 from pitthsls/turnaways
Browse files Browse the repository at this point in the history
Turnaways support
  • Loading branch information
Wooble committed Sep 17, 2019
2 parents dd87167 + c334b65 commit 01112e7
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## 2.1.0 (unreleased)
* add support for COUNTER 4 title-level turnaways reports (JR2 and BR3).

## 2.0.0 (2019-09-13)

* COUNTER 5 support (still a work in progress; see README).
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Usage
-----

Parsing COUNTER reports (currently supports COUNTER 3 and 4, in .csv, .tsv,
or .xlsx files, reports JR1, DB1, DB2, BR1, and BR2)::
or .xlsx files, reports JR1, JR2, DB1, DB2, PR1, BR1, BR2 and BR3)::

>>> import pycounter.report
>>> report = pycounter.report.parse("COUNTER4_2015.tsv") # filename or path to file
Expand Down
20 changes: 17 additions & 3 deletions pycounter/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ def parse_generic(report_reader):
# these reports do not have line with totals
six.next(report_reader)

if report.report_type == "DB2":
if report.report_type in ("DB2", "BR3", "JR3"):
# this report has two lines of totals
six.next(report_reader)

Expand All @@ -792,6 +792,7 @@ def _parse_line(line, report, last_col):
:param last_col: last column number containing data
:return: an appropriate CounterResource subclass instance
"""
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
issn = None
eissn = None
isbn = None
Expand All @@ -800,6 +801,7 @@ def _parse_line(line, report, last_col):
doi = ""
prop_id = ""

metric = report.metric
if report.report_version >= 4:
if report.report_type.startswith("JR1") or report.report_type == "TR_J1":
old_line = line
Expand All @@ -816,6 +818,16 @@ def _parse_line(line, report, last_col):
isbn = line[3].strip()
issn = line[4].strip()

elif report.report_type in ("BR3", "JR2"):
metric = line[7]
doi = line[3]
prop_id = line[4]
line = line[0:3] + line[5:7] + line[9:last_col]
eissn = line[4].strip()
if report.report_type == "BR3":
isbn = line[3].strip()
else:
issn = line[3].strip()
# For DB1 and DB2, nothing additional to do here

else:
Expand All @@ -841,7 +853,7 @@ def _parse_line(line, report, last_col):
curr_month = next_month(curr_month)
if report.report_type.startswith("JR") or report.report_type == "TR_J1":
return CounterJournal(
metric=report.metric,
metric=metric,
month_data=month_data,
doi=doi,
issn=issn,
Expand All @@ -853,7 +865,7 @@ def _parse_line(line, report, last_col):
)
elif report.report_type.startswith("BR"):
return CounterBook(
metric=report.metric,
metric=metric,
month_data=month_data,
doi=doi,
issn=issn,
Expand Down Expand Up @@ -923,6 +935,8 @@ def _year_from_header(header, report):
first_date_col = 5
elif report.report_type == "PR1" and report.report_version == 4:
first_date_col = 4
elif report.report_type == "JR2":
first_date_col = 11
year = int(header[first_date_col].split("-")[1])
if year < 100:
year += 2000
Expand Down
12 changes: 12 additions & 0 deletions pycounter/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,15 @@ def report_file_output(request):
def db_report(request):
"""All C4 database reports."""
return parsedata(request.param)


@pytest.fixture
def br3_report():
"""Book report 3 (turnaways)."""
return parsedata("C4BR3.csv")


@pytest.fixture
def jr2_report():
"""Journal report 2 (turnaways)."""
return parsedata("C4JR2.csv")
14 changes: 14 additions & 0 deletions pycounter/test/data/C4BR3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Book Report 3 (R4),"Access Denied to Content Items by Month, Title and Category",,,,,,,,,,,,,
University of Maximegalon,,,,,,,,,,,,,,
BlahBlah,,,,,,,,,,,,,,
Period covered by Report:,,,,,,,,,,,,,,
2012-01-01 to 2012-06-30,,,,,,,,,,,,,,
Date run:,,,,,,,,,,,,,,
2012-07-09,,,,,,,,,,,,,,
,Publisher,Platform,Book DOI,Propriatary Identifier,ISBN,ISSN,Access Denied Category,Reporting Period Total,Jan-2012,Feb-2012,Mar-2012,Apr-2012,May-2012,Jun-2012
Total for all titles,Megadodo Publications,HHGTTG Online,,,,,Access denied: concurrent/simultaneous user license limit exceeded,14,4,0,2,2,3,3
Total for all titles,Megadodo Publications,HHGTTG Online,,,,,Access denied: content item not licensed,30,10,3,9,1,4,3
Hitchhikers Guide to the Galaxy,Megadodo Publications,HHGTTG Online,,,9787490833809,1900-1000,Access denied: concurrent/simultaneous user license limit exceeded,14,4,0,2,2,3,3
Hitchhikers Guide to the Galaxy,Megadodo Publications,HHGTTG Online,,,9787490833809,1900-1000,Access denied: content item not licensed,0,0,0,0,0,0,0
Öfake Book,Megadodo Publications,HHGTTG Online,,,9783572324031,,Access denied: concurrent/simultaneous user license limit exceeded,0,0,0,0,0,0,0
Öfake Book,Megadodo Publications,HHGTTG Online,,,9783572324031,,Access denied: content item not licensed,30,10,3,9,1,4,3
11 changes: 11 additions & 0 deletions pycounter/test/data/C4JR2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"Journal Report 2 (R4)","Access denied to Full-text Articles by Month, Journal and Category"
"University of Maximegalon"
""
"Period covered by Report:"
"2011-01-01 to 2011-12-31"
"Date run:"
2012-02-21
Journal,Publisher,Platform,"Journal DOI","Proprietary Identifier","Print ISSN","Online ISSN","Access Denied Category","Reporting Period Total","Reporting Period HTML","Reporting Period PDF",Jan-2011,Feb-2011,Mar-2011,Apr-2011,May-2011,Jun-2011,Jul-2011,Aug-2011,Sep-2011,Oct-2011,Nov-2011,Dec-2011
"Total for all journals","Maximegalon University Press",MJO,,,,,16,0,16,2,1,0,0,0,5,1,1,0,5,1,0
"Abstracts of Working Papers in Economics","Maximegalon University Press",MJO,,,0951-0079,0951-0079,Access denied: concurrent/simultaneous user license limit exceeded,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0
"Acta Numerica","Maximegalon University Press",MJO,,,0962-4929,1474-0508,Access denied: concurrent/simultaneous user license limit exceeded,16,0,16,2,1,0,0,0,5,1,1,0,5,1,0
22 changes: 22 additions & 0 deletions pycounter/test/test_br3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Test parsing of COUNTER 4 BR3 report (turnaways)"""

import datetime


def test_metric(br3_report):
assert br3_report.metric is None # Multiple metrics per report


def test_type(br3_report):
assert br3_report.report_type == u"BR3"


def test_data(br3_report):
i = iter(br3_report)
row = next(i)
item = next(iter(row))
assert item == (
datetime.date(2012, 1, 1),
u"Access denied: concurrent/simultaneous user license limit exceeded",
4,
)
22 changes: 22 additions & 0 deletions pycounter/test/test_jr2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Test parsing of COUNTER 4 JR2 report (turnaways)"""

import datetime


def test_metric(jr2_report):
assert jr2_report.metric is None # Multiple metrics per report


def test_type(jr2_report):
assert jr2_report.report_type == u"JR2"


def test_data(jr2_report):
i = iter(jr2_report)
row = next(i)
item = next(iter(row))
assert item == (
datetime.date(2011, 1, 1),
u"Access denied: concurrent/simultaneous user license limit exceeded",
3,
)

0 comments on commit 01112e7

Please sign in to comment.