Esempio n. 1
0
def valid_isbn(value):
    try:
        isbn.validate(value)
        return True

    except InvalidFormat:
        return False
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 5
0
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
Esempio n. 6
0
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)
Esempio n. 7
0
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 _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
Esempio n. 9
0
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}")
Esempio n. 10
0
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}"
            )
Esempio n. 11
0
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
Esempio n. 12
0
 def transform(self, data):
     try:
         return isbn.validate(data, convert=True)
     except:
         pass