# Path: worldcat_api.py
# def get_by_oclc(oclc):
#     o = fetch_worldcat('oclc', oclc)
#     #sys.stderr.write('o = ' + repr(o) + '\n')
#     if o is None or 'stat' not in o or o['stat'] != 'ok':
#         return
# 
#     assert len(o['list']) == 1
#     for i in o['list']:
#         #print i
#         if 'isbn' not in i:
#             return {'identifiers': set([('oclc', str(oclc))])}
#         for isbn in i['isbn']:
#             #print isbn
#             output = get_by_isbn(isbn)
#             # Remove other OCLC
#             output['identifiers'] = set(p for p in output['identifiers'] if p[0] != 'oclc')
#             # The known oclc, in case it is not returned from WorldCat
#             output['identifiers'].add(('oclc', str(oclc)))
#             return output
# 
# def get_by_isbn(isbn):
#     o = fetch_worldcat('isbn', isbn)
#     #sys.stderr.write('o = ' + repr(o) + '\n')
#     if o is None or 'stat' not in o or o['stat'] != 'ok':
#         return
#     
#     assert len(o['list']) == 1
#     data_dict = o['list'][0]
#     #print "DICT:" + repr(data_dict) + '\n'
# 
#     output = {}
# 
#     fields = ['title']
#     for field in fields:
#         if field in data_dict:
#             output[field] = data_dict[field].encode("utf-8")
#             
#     if 'author' in data_dict:
#         author = data_dict['author']
#         author = re.sub(r'ed\. by ', '', author)
#         author = re.split(r'(?<!\s\w)\.', author)[0]
#         # Remove brackets and anything outside them
#         author = re.sub(r'^.*?\[(.*?)\].*', r'\1', author)
#         #sys.stderr.write('author = ' + repr(author) + '\n')
#         authors = author.split(',')
#         output['authors'] = [a.strip().encode("utf-8") for a in authors]
#     
#     if 'year' in data_dict:
#         output['date'] = data_dict['year']
#             
#     if 'publisher' in data_dict:
#         output['publishers'] = [data_dict['publisher'].encode("utf-8")]
#             
#     identifiers = set()
#     # First add the known isbn
#     identifiers.add(('isbn', str(isbn)))
#     if 'isbn' in data_dict:
#         for otherisbn in data_dict['isbn']:
#             identifiers.add(('isbn', otherisbn))
#     if 'oclcnum' in data_dict:
#         for otheroclc in data_dict['oclcnum']:
#             identifiers.add(('oclc', otheroclc))
#     output['identifiers'] = identifiers
# 
#     return output


import unittest
from worldcat_api import get_by_oclc, get_by_isbn

class TestOclc(unittest.TestCase):
    def test_oclc(self):
        oclc = '550538756'
        info = get_by_oclc(oclc)
        self.assertEquals(info['title'], "On hobos and homelessness")
        self.assertIn('Nels Anderson', info['authors'][0])
        self.assertIn('1998', info['date'])
        self.assertIn('Chicago Press', info['publishers'][0])

        identifiers = info['identifiers']
        self.assertIn(('oclc', oclc), identifiers)
        self.assertIn(('isbn', '9780226019666'), identifiers)

    def test_igma(self):
        oclc = '608542024'
        info = get_by_oclc(oclc)
        self.assertEquals(info['title'], "It probably won't kill you ... : twisted humor for your kitchen")
        self.assertEquals('Iyan Igma', info['authors'][0])
        self.assertIn('2009', info['date'])
        self.assertIn('CreateSpace', info['publishers'][0])

        identifiers = info['identifiers']
        self.assertIn(('oclc', oclc), identifiers)
        self.assertIn(('isbn', '9781441476562'), identifiers)

    def test_isbn(self):
        isbn = '1858286999'

# Path: worldcat_api.py
# def get_by_oclc(oclc):
#     o = fetch_worldcat('oclc', oclc)
#     #sys.stderr.write('o = ' + repr(o) + '\n')
#     if o is None or 'stat' not in o or o['stat'] != 'ok':
#         return
# 
#     assert len(o['list']) == 1
#     for i in o['list']:
#         #print i
#         if 'isbn' not in i:
#             return {'identifiers': set([('oclc', str(oclc))])}
#         for isbn in i['isbn']:
#             #print isbn
#             output = get_by_isbn(isbn)
#             # Remove other OCLC
#             output['identifiers'] = set(p for p in output['identifiers'] if p[0] != 'oclc')
#             # The known oclc, in case it is not returned from WorldCat
#             output['identifiers'].add(('oclc', str(oclc)))
#             return output
# 
# def get_by_isbn(isbn):
#     o = fetch_worldcat('isbn', isbn)
#     #sys.stderr.write('o = ' + repr(o) + '\n')
#     if o is None or 'stat' not in o or o['stat'] != 'ok':
#         return
#     
#     assert len(o['list']) == 1
#     data_dict = o['list'][0]
#     #print "DICT:" + repr(data_dict) + '\n'
# 
#     output = {}
# 
#     fields = ['title']
#     for field in fields:
#         if field in data_dict:
#             output[field] = data_dict[field].encode("utf-8")
#             
#     if 'author' in data_dict:
#         author = data_dict['author']
#         author = re.sub(r'ed\. by ', '', author)
#         author = re.split(r'(?<!\s\w)\.', author)[0]
#         # Remove brackets and anything outside them
#         author = re.sub(r'^.*?\[(.*?)\].*', r'\1', author)
#         #sys.stderr.write('author = ' + repr(author) + '\n')
#         authors = author.split(',')
#         output['authors'] = [a.strip().encode("utf-8") for a in authors]
#     
#     if 'year' in data_dict:
#         output['date'] = data_dict['year']
#             
#     if 'publisher' in data_dict:
#         output['publishers'] = [data_dict['publisher'].encode("utf-8")]
#             
#     identifiers = set()
#     # First add the known isbn
#     identifiers.add(('isbn', str(isbn)))
#     if 'isbn' in data_dict:
#         for otherisbn in data_dict['isbn']:
#             identifiers.add(('isbn', otherisbn))
#     if 'oclcnum' in data_dict:
#         for otheroclc in data_dict['oclcnum']:
#             identifiers.add(('oclc', otheroclc))
#     output['identifiers'] = identifiers
# 
#     return output


# Path: test/test_oclc.py
import unittest
from worldcat_api import get_by_oclc, get_by_isbn


class TestOclc(unittest.TestCase):
    def test_oclc(self):
        oclc = '550538756'
        info = get_by_oclc(oclc)
        self.assertEquals(info['title'], "On hobos and homelessness")
        self.assertIn('Nels Anderson', info['authors'][0])
        self.assertIn('1998', info['date'])
        self.assertIn('Chicago Press', info['publishers'][0])

        identifiers = info['identifiers']
        self.assertIn(('oclc', oclc), identifiers)
        self.assertIn(('isbn', '9780226019666'), identifiers)

    def test_igma(self):
        oclc = '608542024'
        info = get_by_oclc(oclc)
        self.assertEquals(info['title'], "It probably won't kill you ... : twisted humor for your kitchen")
        self.assertEquals('Iyan Igma', info['authors'][0])
        self.assertIn('2009', info['date'])
        self.assertIn('CreateSpace', info['publishers'][0])

        identifiers = info['identifiers']
        self.assertIn(('oclc', oclc), identifiers)
        self.assertIn(('isbn', '9781441476562'), identifiers)

    def test_isbn(self):
        isbn = '1858286999'

        info = get_by_isbn(isbn)