Skip to content

Commit

Permalink
Merge pull request #13 from pitthsls/issue1
Browse files Browse the repository at this point in the history
pull in fix for issue 1 (closes issue #1 )
  • Loading branch information
Wooble committed Apr 28, 2015
2 parents cf06942 + 0d66be7 commit 93d7b8a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
41 changes: 30 additions & 11 deletions pycounter/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class CounterEresource(six.Iterator):
:param metric: metric tracked by this report. Should be a value
from pycounter.report.METRICS dict.
:param month_data: a list containing usage data for this
resource, as (datetime.date, usage) tuples
:ivar title: title of the resource
:ivar publisher: name of the resource's publisher
Expand All @@ -97,11 +100,16 @@ class CounterEresource(six.Iterator):
"""

def __init__(self, line=None, period=None, metric=None):
def __init__(self, line=None, period=None, metric=None, month_data=None):
self.period = period
if metric not in METRICS.values():
warnings.warn("metric %s not known" % metric)
self.metric = metric
self._monthdata = []
self._full_data = []
if month_data is not None:
for item in month_data:
self._full_data.append(item)
if line is not None:
self.title = line[0]
self.publisher = line[1]
Expand All @@ -112,12 +120,16 @@ def __init__(self, line=None, period=None, metric=None):
logging.debug("monthdata: %s", self._monthdata)

def __iter__(self):
currmonth = self.period[0]
mondat = iter(self._monthdata)
while currmonth < self.period[1]:
currusage = next(mondat)
yield (currmonth, self.metric, currusage)
currmonth = _next_month(currmonth)
if self._full_data:
for item in self._full_data:
yield (item[0], self.metric, item[1])
else:
currmonth = self.period[0]
mondat = iter(self._monthdata)
while currmonth < self.period[1]:
currusage = next(mondat)
yield (currmonth, self.metric, currusage)
currmonth = _next_month(currmonth)


class CounterJournal(CounterEresource):
Expand All @@ -135,14 +147,18 @@ class CounterJournal(CounterEresource):
(Should probably always be "FT Article Requests" for
CounterJournal objects, as long as only JR1 is supported.)
:param month_data: a list containing usage data for this
journal, as (datetime.date, usage) tuples
:ivar issn: eJournal's print ISSN
:ivar eissn: eJournal's eISSN
"""

def __init__(self, line=None, period=None, metric=METRICS[u"JR1"]):
super(CounterJournal, self).__init__(line, period, metric)
def __init__(self, line=None, period=None, metric=METRICS[u"JR1"],
issn=None, eissn=None, month_data=None):
super(CounterJournal, self).__init__(line, period, metric, month_data)
if line is not None:
self.issn = line[3].strip()
self.eissn = line[4].strip()
Expand All @@ -165,10 +181,13 @@ class CounterBook(CounterEresource):
:ivar issn: eBook's ISSN (if any)
:param month_data: a list containing usage data for this
book, as (datetime.date, usage) tuples
"""

def __init__(self, line=None, period=None, metric=None):
super(CounterBook, self).__init__(line, period, metric)
def __init__(self, line=None, period=None, metric=None, month_data=None):
super(CounterBook, self).__init__(line, period, metric, month_data)
if line is not None:
self.isbn = line[3].strip().replace('-', '')
if len(self.isbn) == 10:
Expand Down
15 changes: 12 additions & 3 deletions pycounter/sushi.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,31 @@ def _raw_to_full(raw_report):
eissn = ""
itemline.append(issn)
itemline.append(eissn)
month_data = []

for perfitem in item.ItemPerformance:
logger.debug("Perfitem date: %r", pycounter.report._convert_date_run(perfitem.Period.Begin.text))
item_date = pycounter.report._convert_date_run(
perfitem.Period.Begin.text)
usage = None
for inst in perfitem.Instance:
if inst.MetricType == "ft_total":
usage = str(inst.Count)
break
if usage is not None:
month_data.append((item_date, usage))
itemline.append(usage)

if report.report_type:
if report.report_type.startswith('JR'):
report.pubs.append(pycounter.report.CounterJournal(
itemline,
report.period,
report.metric))
line=itemline,
period=report.period,
metric=report.metric,
issn=issn,
eissn=eissn,
month_data=month_data
))
elif report.report_type.startswith('BR'):
report.pubs.append(
pycounter.report.CounterBook(itemline,
Expand Down
17 changes: 17 additions & 0 deletions pycounter/test/data/sushi_missing_jan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<!--suppress CheckTagEmptyBody -->
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns3:ReportResponse xmlns="http://www.niso.org/schemas/counter" xmlns:ns2="http://www.niso.org/schemas/sushi" xmlns:ns3="http://www.niso.org/schemas/sushi/counter">
<!--suppress CheckTagEmptyBody -->
<ns2:Requestor><ns2:ID>exampleRequestor</ns2:ID><ns2:Name></ns2:Name><ns2:Email></ns2:Email></ns2:Requestor><ns2:CustomerReference><ns2:ID>exampleReference</ns2:ID><ns2:Name></ns2:Name></ns2:CustomerReference>
<ns2:ReportDefinition Release="4" Name="JR1"><ns2:Filters><ns2:UsageDateRange><ns2:Begin>2013-01-01</ns2:Begin><ns2:End>2013-02-28</ns2:End></ns2:UsageDateRange></ns2:Filters></ns2:ReportDefinition>
<ns3:Report><Report Title="Journal Report 1" Name="JR1" Version="4" ID="exampleuser:JR1" Created="2015-01-23T14:44:16.801-05:00">
<Vendor><Name>Example Vendor</Name><ID>example</ID><Contact><Contact>Vendor Contact</Contact><E-mail>vendor@example.com</E-mail></Contact></Vendor>
<Customer><Name>Example Library</Name><ID>exampleLibrary</ID>
<ReportItems><ItemIdentifier><Type>Print_ISSN</Type><Value>0737-1764</Value></ItemIdentifier>
<ItemIdentifier><Type>Online_ISSN</Type><Value>1234-5678</Value></ItemIdentifier>
<ItemIdentifier><Type>Proprietary</Type><Value>JFD</Value></ItemIdentifier>
<ItemPlatform>ExamplePlatform</ItemPlatform><ItemPublisher>Example Publisher</ItemPublisher>
<ItemName>Journal of fake data</ItemName><ItemDataType>Journal</ItemDataType>
<ItemPerformance><Period><Begin>2013-02-01</Begin><End>2013-02-28</End></Period><Category>Requests</Category><Instance><MetricType>ft_html</MetricType><Count>0</Count></Instance><Instance><MetricType>ft_pdf</MetricType><Count>17</Count></Instance><Instance><MetricType>ft_total</MetricType><Count>17</Count></Instance>
</ItemPerformance>
</ReportItems></Customer></Report></ns3:Report></ns3:ReportResponse></S:Body></S:Envelope>
2 changes: 1 addition & 1 deletion pycounter/test/data/sushi_simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns3:ReportResponse xmlns="http://www.niso.org/schemas/counter" xmlns:ns2="http://www.niso.org/schemas/sushi" xmlns:ns3="http://www.niso.org/schemas/sushi/counter">
<!--suppress CheckTagEmptyBody -->
<ns2:Requestor><ns2:ID>exampleRequestor</ns2:ID><ns2:Name></ns2:Name><ns2:Email></ns2:Email></ns2:Requestor><ns2:CustomerReference><ns2:ID>exampleReference</ns2:ID><ns2:Name></ns2:Name></ns2:CustomerReference>
<ns2:ReportDefinition Release="4" Name="JR1"><ns2:Filters><ns2:UsageDateRange><ns2:Begin>2013-01-01</ns2:Begin><ns2:End>2013-12-31</ns2:End></ns2:UsageDateRange></ns2:Filters></ns2:ReportDefinition>
<ns2:ReportDefinition Release="4" Name="JR1"><ns2:Filters><ns2:UsageDateRange><ns2:Begin>2013-01-01</ns2:Begin><ns2:End>2013-01-31</ns2:End></ns2:UsageDateRange></ns2:Filters></ns2:ReportDefinition>
<ns3:Report><Report Title="Journal Report 1" Name="JR1" Version="4" ID="exampleuser:JR1" Created="2015-01-23T14:44:16.801-05:00">
<Vendor><Name>Example Vendor</Name><ID>example</ID><Contact><Contact>Vendor Contact</Contact><E-mail>vendor@example.com</E-mail></Contact></Vendor>
<Customer><Name>Example Library</Name><ID>exampleLibrary</ID>
Expand Down
21 changes: 21 additions & 0 deletions pycounter/test/test_sushi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def sushi_mock(url, request):
def bogus_mock(url, request):
return "Bogus response with no XML"


class TestHelpers(unittest.TestCase):
def test_ns(self):
self.assertEqual(
Expand All @@ -39,6 +40,25 @@ def test_report(self):
self.assertEqual(self.report.report_type, u'JR1')
self.assertEqual(self.report.report_version, 4)

def test_title(self):
publication = next(iter(self.report))
self.assertEqual(publication.title, u'Journal of fake data')


class TestMissingMonth(unittest.TestCase):
def setUp(self):
path = os.path.join(os.path.dirname(__file__),
'data', 'sushi_missing_jan.xml')
with open(path, 'rb') as datafile:
self.report = sushi._raw_to_full(datafile.read())
self.publication = next(iter(self.report))
def test_february(self):
first_month_data = next(iter(self.publication))
self.assertEqual(first_month_data[0],
datetime.date(2013,2,1))
def test_title(self):
self.assertEqual(self.publication.title, u'Journal of fake data')


class TestSushiRequest(unittest.TestCase):
def setUp(self):
Expand All @@ -52,6 +72,7 @@ def test_report(self):
self.assertEqual(self.report.report_type, u'JR1')
self.assertEqual(self.report.report_version, 4)


class TestBogusXML(unittest.TestCase):
def test_request(self):
with HTTMock(bogus_mock):
Expand Down

0 comments on commit 93d7b8a

Please sign in to comment.