def test_general(self): """Test things that apply both to ISBN10 and ISBN13.""" # getIsbn self.assertIsInstance(getIsbn('097522980x'), ISBN10) self.assertIsInstance(getIsbn('9783161484100'), ISBN13) self.assertRaisesRegex( IsbnExc, 'ISBN-13: The ISBN 097522 is not 13 digits ' 'long. / ISBN-10: The ISBN 097522 is not 10 ' 'digits long.', getIsbn, '097522') # hyphenateIsbnNumbers self.assertEqual(hyphenateIsbnNumbers('ISBN 097522980x'), 'ISBN 0-9752298-0-X') self.assertEqual(hyphenateIsbnNumbers('ISBN 0975229801'), 'ISBN 0975229801') # Invalid ISBN - no changes # convertIsbn10toIsbn13 self.assertEqual(convertIsbn10toIsbn13('ISBN 0-9752298-0-X'), 'ISBN 978-0-9752298-0-4') self.assertEqual(convertIsbn10toIsbn13('ISBN 0-9752298-0-1'), 'ISBN 0-9752298-0-1') # Invalid ISBN - no changes # Errors isbn = ISBN10('9492098059') self.assertRaisesRegex(IsbnExc, 'ISBN 9492098059: group number unknown.', isbn.format) isbn = ISBN10('9095012042') self.assertRaisesRegex(IsbnExc, 'ISBN 9095012042: publisher number unknown.', isbn.format)
def test_general(self): """Test things that apply both to ISBN10 and ISBN13.""" # getIsbn self.assertIsInstance(getIsbn('097522980x'), ISBN10) self.assertIsInstance(getIsbn('9783161484100'), ISBN13) self.assertRaisesRegex(IsbnExc, 'ISBN-13: The ISBN 097522 is not 13 digits ' 'long. / ISBN-10: The ISBN 097522 is not 10 ' 'digits long.', getIsbn, '097522') # hyphenateIsbnNumbers self.assertEqual(hyphenateIsbnNumbers('ISBN 097522980x'), 'ISBN 0-9752298-0-X') self.assertEqual(hyphenateIsbnNumbers('ISBN 0975229801'), 'ISBN 0975229801') # Invalid ISBN - no changes # convertIsbn10toIsbn13 self.assertEqual(convertIsbn10toIsbn13('ISBN 0-9752298-0-X'), 'ISBN 978-0-9752298-0-4') self.assertEqual(convertIsbn10toIsbn13('ISBN 0-9752298-0-1'), 'ISBN 0-9752298-0-1') # Invalid ISBN - no changes # Errors isbn = ISBN10('9492098059') self.assertRaisesRegex(IsbnExc, 'ISBN 9492098059: group number unknown.', isbn.format) isbn = ISBN10('9095012042') self.assertRaisesRegex(IsbnExc, 'ISBN 9095012042: publisher number unknown.', isbn.format)
def test_general(self): """Test things that apply both to ISBN10 and ISBN13.""" # getIsbn self.assertIsInstance(getIsbn("097522980x"), ISBN10) self.assertIsInstance(getIsbn("9783161484100"), ISBN13) self.assertRaisesRegex( IsbnExc, "ISBN-13: The ISBN 097522 is not 13 digits " "long. / ISBN-10: The ISBN 097522 is not 10 " "digits long.", getIsbn, "097522", ) # hyphenateIsbnNumbers self.assertEqual(hyphenateIsbnNumbers("ISBN 097522980x"), "ISBN 0-9752298-0-X") self.assertEqual(hyphenateIsbnNumbers("ISBN 0975229801"), "ISBN 0975229801") # Invalid ISBN - no changes # convertIsbn10toIsbn13 self.assertEqual(convertIsbn10toIsbn13("ISBN 0-9752298-0-X"), "ISBN 978-0-9752298-0-4") self.assertEqual(convertIsbn10toIsbn13("ISBN 0-9752298-0-1"), "ISBN 0-9752298-0-1") # Invalid ISBN - no changes # Errors isbn = ISBN10("9492098059") self.assertRaisesRegex(IsbnExc, "ISBN 9492098059: group number unknown.", isbn.format) isbn = ISBN10("9095012042") self.assertRaisesRegex(IsbnExc, "ISBN 9095012042: publisher number unknown.", isbn.format)
def _format_isbn_match(match, strict=True): """Helper function to validate and format a single matched ISBN.""" isbn = match.group('code') if stdnum_isbn: try: stdnum_isbn.validate(isbn) except stdnum_isbn.ValidationError as e: if strict: raise pywikibot.log('ISBN "%s" validation error: %s' % (isbn, e)) return isbn return stdnum_isbn.format(isbn) else: try: scripts_isbn.is_valid(isbn) except scripts_isbn.InvalidIsbnException as e: if strict: raise pywikibot.log('ISBN "%s" validation error: %s' % (isbn, e)) return isbn isbn = scripts_isbn.getIsbn(isbn) try: isbn.format() except scripts_isbn.InvalidIsbnException as e: if strict: raise pywikibot.log('ISBN "%s" validation error: %s' % (isbn, e)) return isbn.code
def _format_isbn_match(match, strict=True): """Helper function to validate and format a single matched ISBN.""" scripts_isbn = None if not stdnum_isbn: # For backwards compatibility, if stdnum.isbn is not available # attempt loading scripts.isbn as an alternative implementation. try: import scripts.isbn as scripts_isbn except ImportError: raise NotImplementedError( 'ISBN functionality not available. Install stdnum package.') warn('package stdnum.isbn not found; using scripts.isbn', ImportWarning) isbn = match.group('code') if stdnum_isbn: try: stdnum_isbn.validate(isbn) except stdnum_isbn.ValidationError as e: if strict: raise pywikibot.log('ISBN "%s" validation error: %s' % (isbn, e)) return isbn return stdnum_isbn.format(isbn) else: try: scripts_isbn.is_valid(isbn) except scripts_isbn.InvalidIsbnException as e: if strict: raise pywikibot.log('ISBN "%s" validation error: %s' % (isbn, e)) return isbn isbn = scripts_isbn.getIsbn(isbn) try: isbn.format() except scripts_isbn.InvalidIsbnException as e: if strict: raise pywikibot.log('ISBN "%s" validation error: %s' % (isbn, e)) return isbn.code