def is_valid(isbn): """Check whether an ISBN 10 or 13 is valid.""" # isbnlib marks any ISBN10 with lowercase 'X' as invalid isbn = isbn.upper() try: stdnum.isbn except NameError: pass else: try: stdnum.isbn.validate(isbn) except (stdnum.isbn.InvalidFormat, stdnum.isbn.InvalidChecksum, stdnum.isbn.InvalidLength) as e: raise InvalidIsbnException(str(e)) return True try: isbnlib except NameError: pass else: if isbnlib.notisbn(isbn): raise InvalidIsbnException('Invalid ISBN found') return True raise NotImplementedError( 'ISBN functionality not available. Install stdnum package.')
def _hyphenateIsbnNumber(match): """Helper function to deal with a single ISBN.""" isbn = match.group('code') isbn = isbn.upper() try: is_valid(isbn) except InvalidIsbnException: return isbn try: stdnum.isbn except NameError: pass else: i = stdnum.isbn.format(isbn) return i try: isbn_hyphenate except NameError: pass else: try: i = isbn_hyphenate.hyphenate(isbn) except (isbn_hyphenate.IsbnMalformedError, isbn_hyphenate.IsbnUnableToHyphenateError): return isbn return i i = getIsbn(isbn) i.format() return i.code
def _isbn10toIsbn13(match): """Helper function to deal with a single ISBN.""" isbn = match.group('code') isbn = isbn.upper() try: is_valid(isbn) except InvalidIsbnException: # don't change return isbn try: stdnum.isbn except NameError: pass else: return stdnum.isbn.to_isbn13(isbn) try: isbnlib except NameError: pass else: # remove hyphenation, otherwise isbnlib.to_isbn13() returns None i = isbnlib.canonical(isbn) if i == isbn: i13 = isbnlib.to_isbn13(i) return i13 # add removed hyphenation i13 = isbnlib.to_isbn13(i) i13h = hyphenateIsbnNumbers('ISBN ' + i13) return i13h[5:]
def is_valid(isbn): """Check whether an ISBN 10 or 13 is valid.""" # isbnlib marks any ISBN10 with lowercase 'X' as invalid isbn = isbn.upper() try: stdnum.isbn except NameError: pass else: try: stdnum.isbn.validate(isbn) except stdnum.isbn.InvalidFormat as e: raise InvalidIsbnException(str(e)) except stdnum.isbn.InvalidChecksum as e: raise InvalidIsbnException(str(e)) except stdnum.isbn.InvalidLength as e: raise InvalidIsbnException(str(e)) return True try: isbnlib except NameError: pass else: if isbnlib.notisbn(isbn): raise InvalidIsbnException('Invalid ISBN found') return True getIsbn(isbn) return True
def _hyphenateIsbnNumber(match): """Helper function to deal with a single ISBN.""" isbn = match.group('code') isbn = isbn.upper() try: is_valid(isbn) except InvalidIsbnException: return isbn try: stdnum.isbn except NameError: pass else: return stdnum.isbn.format(isbn)
def _isbn10toIsbn13(match): """Helper function to deal with a single ISBN.""" isbn = match.group('code') isbn = isbn.upper() try: stdnum.isbn except NameError: pass else: try: is_valid(isbn) except InvalidIsbnException: return isbn i = stdnum.isbn.to_isbn13(isbn) return i try: isbnlib except NameError: pass else: try: is_valid(isbn) except InvalidIsbnException: return isbn # remove hyphenation, otherwise isbnlib.to_isbn13() returns None i = isbnlib.canonical(isbn) if i == isbn: i13 = isbnlib.to_isbn13(i) return i13 # add removed hyphenation i13 = isbnlib.to_isbn13(i) i13h = hyphenateIsbnNumbers('ISBN ' + i13) return i13h[5:] try: is_valid(isbn) except InvalidIsbnException: # don't change return isbn i1x = getIsbn(isbn) if not isinstance(i1x, ISBN13): i13 = i1x.toISBN13() else: i13 = i1x return i13.code