Skip to content

Commit

Permalink
refact wcat and goob providers (issue #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlcnd committed Apr 21, 2015
1 parent e50a70c commit 4f32f90
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
25 changes: 10 additions & 15 deletions isbnlib/_goob.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

from .dev import stdmeta
from .dev._exceptions import (DataWrongShapeError, ISBNNotConsistentError,
from .dev._exceptions import (ISBNNotConsistentError,
NoDataForSelectorError, RecordMappingError)
from .dev.bouth23 import u
from .dev.webquery import query as wquery
Expand Down Expand Up @@ -40,25 +40,20 @@ def _mapper(isbn, records):

def _records(isbn, data):
"""Classify (canonically) the parsed data."""
# put the selected data in records
try:
# put the selected data in records
records = data['items'][0]['volumeInfo']
# consistency check (isbn request = isbn response)
ids = records.get('industryIdentifiers', '')
recs = data['items'][0]['volumeInfo']
except: # pragma: no cover
LOGGER.debug('NoDataForSelectorError for %s', isbn)
raise NoDataForSelectorError(isbn)
# consistency check (isbn request = isbn response)
if recs:
ids = recs.get('industryIdentifiers', '')
if isbn not in repr(ids): # pragma: no cover
LOGGER.debug('Not consistent data for %s (%s)', isbn, repr(ids))
raise ISBNNotConsistentError(isbn)
except: # pragma: no cover
try:
extra = data['stat']
LOGGER.debug('DataWrongShapeError for %s with data %s',
isbn, extra)
except:
raise DataWrongShapeError(isbn)
raise NoDataForSelectorError(isbn)

# map canonical <- records
return _mapper(isbn, records)
return _mapper(isbn, recs)


def query(isbn):
Expand Down
29 changes: 19 additions & 10 deletions isbnlib/_wcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import logging

from .dev import stdmeta
from .dev._exceptions import (DataWrongShapeError, NoDataForSelectorError,
RecordMappingError)
from .dev._exceptions import (DataWrongShapeError, ISBNNotConsistentError,
NoDataForSelectorError, RecordMappingError)
from .dev.bouth23 import u
from .dev.webquery import query as wquery

Expand Down Expand Up @@ -36,18 +36,27 @@ def _mapper(isbn, records):

def _records(isbn, data):
"""Classify (canonically) the parsed data."""
# check status
try:
status = data['stat']
if status != 'ok':
raise
except:
LOGGER.debug('DataWrongShapeError for %s with status %s',
isbn, status)
raise DataWrongShapeError(isbn)
# put the selected data in records
try:
# put the selected data in records
recs = data['list'][0]
except: # pragma: no cover
try:
extra = data['stat']
LOGGER.debug('DataWrongShapeError for %s with data %s',
isbn, extra)
except:
raise DataWrongShapeError(isbn)
LOGGER.debug('NoDataForSelectorError for %s', isbn)
raise NoDataForSelectorError(isbn)

# consistency check (isbn request = isbn response)
if recs:
ids = recs.get('isbn', '')
if isbn not in repr(ids): # pragma: no cover
LOGGER.debug('Not consistent data for %s (%s)', isbn, repr(ids))
raise ISBNNotConsistentError(isbn)
# map canonical <- records
return _mapper(isbn, recs)

Expand Down

0 comments on commit 4f32f90

Please sign in to comment.