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