def valid_isbn(value): try: isbn.validate(value) return True except InvalidFormat: return False
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 validate_identifier(identifier): errors = [] if identifier: if identifier["id_type"] == "issn": try: issn.validate(identifier["value"]) except: errors.append("Invalid ISSN: {}".format(identifier["value"])) elif identifier["id_type"] == "isbn": try: isbn.validate(identifier["value"]) except: errors.append("Invalid ISBN: {}".format(identifier["value"])) elif identifier["id_type"] == "isan": try: isan.validate(identifier["value"]) except: errors.append("Invalid ISAN: {}".format(identifier["value"])) elif identifier["id_type"] == "ismn": try: ismn.validate(identifier["value"]) except: errors.append("Invalid ISMN: {}".format(identifier["value"])) elif identifier["id_type"] == "ean": try: ean.validate(identifier["value"]) except: errors.append("Invalid EAN: {}".format(identifier["value"])) return errors
def _format_isbn_match(match, strict=True): """Helper function to validate and format a single matched ISBN.""" if not stdnum_isbn: raise NotImplementedError( 'ISBN functionality not available. Install stdnum package.') isbn = match.group('code') 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)
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
def validate_isbn(item, errors): """ Checks ISBN for validity. Will accept both ISBN-10 and ISBN-13 formats """ isbn_list = item.get("isbn") if isbn_list is None: return for idx, single_isbn in enumerate(isbn_list): try: isbn.validate(single_isbn) except isbn.ValidationError as ex: errors.add(f"ISBN #{idx} ({single_isbn}) is not valid: {ex}") continue formatted = isbn.format(single_isbn) if (formatted != single_isbn): errors.add(f"ISBN #{idx} ({single_isbn}) should be reformatted to {formatted}") if (isbn.isbn_type(single_isbn) != 'ISBN13'): errors.add(f"ISBN-10 #{idx} ({single_isbn}) should be reformatted to ISBN-13 {formatted}")
def validate_isbn(item, errors): """ Checks ISBN for validity. Will accept both ISBN-10 and ISBN-13 formats """ isbn_list = item.get("isbn") if isbn_list is None: return for idx, single_isbn in enumerate(isbn_list): try: isbn.validate(single_isbn) except isbn.ValidationError as ex: errors.add(f"ISBN #{idx} ({single_isbn}) is not valid: {ex}") continue formatted = isbn.format(single_isbn) if (formatted != single_isbn): errors.add( f"ISBN #{idx} ({single_isbn}) should be reformatted to {formatted}" ) if (isbn.isbn_type(single_isbn) != 'ISBN13'): errors.add( f"ISBN-10 #{idx} ({single_isbn}) should be reformatted to ISBN-13 {formatted}" )
def validate_gtin(number, accepted_length=DEFAULT_ACCEPT_LENGTH): """ It's the same as stdnum.ean.validate() but also accept ISBN-10 """ number = compact(number) # Convert to the minimal representation if not isdigits(number): raise InvalidFormat() if len(number) not in accepted_length: raise InvalidLength() if len(number) == 10: # Maybe a ISBN-10 number: return isbn.validate(number) if calc_check_digit(number[:-1]) != number[-1]: raise InvalidChecksum() return number
def transform(self, data): try: return isbn.validate(data, convert=True) except: pass