def check_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): if not isbn.is_valid(single_isbn): errors.add("ISBN #{idx} isn't valid".format( idx=idx )) continue formatted = isbn.format(single_isbn) if (formatted != single_isbn): errors.add("ISBN #{idx} ({single_isbn}) should be reformatted to {formatted}".format( idx=idx, single_isbn=single_isbn, formatted=formatted )) if (isbn.isbn_type(single_isbn) != 'ISBN13'): errors.add("ISBN-10 #{idx} ({single_isbn}) should be reformatted to ISBN-13 {formatted}".format( idx=idx, single_isbn=single_isbn, formatted=isbn.to_isbn13(single_isbn) ))
def create_isbn(self, key, var_list=None): tmp_list = [] for var in var_list: # print(self.csvRow[var].strip()) tmp_elem = re.split('; |, ', self.csvRow[var].strip()) for i in range(len(tmp_elem)): """ Filter ISSN and ISBN only """ tmp_var_isbn = self.remove_non_isbn_chars(tmp_elem[i].upper()) if isbn.is_valid(tmp_var_isbn): # print("Valid ISBN: " + tmp_var_isbn) tmp_elem[i] = isbn.format(tmp_var_isbn) else: tmp_elem[i] = None # Filter all elements, remove text chars, parenthesis etc # print(tmp_elem) tmp_list += tmp_elem # print(tmp_list) tmp_list = list(filter(None, tmp_list)) # remove empty list elements # print(tmp_list) # No semi-colon, so do a join here. Cannot use the function to split semi-colons return '||'.join(filter(None, tmp_list)).strip()
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_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): if not isbn.is_valid(single_isbn): errors.add("ISBN #{idx} isn't valid".format( idx=idx )) continue formatted = isbn.format(single_isbn) if (formatted != single_isbn): errors.add("ISBN #{idx} ({single_isbn}) should be reformatted to {formatted}".format( idx=idx, single_isbn=single_isbn, formatted=formatted )) if (isbn.isbn_type(single_isbn) != 'ISBN13'): errors.add("ISBN-10 #{idx} ({single_isbn}) should be reformatted to ISBN-13 {formatted}".format( idx=idx, single_isbn=single_isbn, formatted=isbn.to_isbn13(single_isbn) ))
def show_isbn(_isbn): isbn = '' for code in _isbn.split(';'): if stdisbn.is_valid(code): isbn += stdisbn.format(code) + '; ' else: isbn += code + '; ' if isbn: # chop trailing '; ' return isbn[:-2]
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 main(raw_url, convert=True): url = get_page_url(raw_url) wikitext, times = get_wikitext(url) code = mwparserfromhell.parse(wikitext) count = 0 for template, raw_isbn, para in find_isbns(code): if not check_isbn(raw_isbn): continue new_isbn = isbn.format(raw_isbn, convert=convert) if raw_isbn != new_isbn: count += 1 template.add(para, new_isbn) return code, times, count, url
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 formatIdentifier(stringToFormat, type): """ This is used to format/check ISBN and ISSN numbers """ if type.lower() == "isbn": if isbn.is_valid(stringToFormat): log.debug("OK ISBN: {0}".format(stringToFormat)) return isbn.format(stringToFormat) else: log.warning("Malformed ISBN: {0}".format(stringToFormat)) return stringToFormat elif type.lower() == "issn": if issn.is_valid(stringToFormat): log.debug("OK ISSN: {0}".format(stringToFormat)) return issn.format(stringToFormat) else: log.warning("Malformed ISSN: {0}".format(stringToFormat)) return stringToFormat
def set_isbn(self): """Add ISBN's, both 10 and 13 char long.""" self.isbn_13 = None self.isbn_10 = None raw_ids = self.raw_data[1].get("identifiedBy") if not raw_ids: return for r_id in raw_ids: if r_id.get("@type").lower() == "isbn": raw_isbn = r_id.get("value") isbn_type = isbn_tool.isbn_type(raw_isbn) if isbn_type: formatted = isbn_tool.format(raw_isbn) if isbn_type == "ISBN13": prop = "isbn_13" self.isbn_13 = isbn_tool.compact(formatted) elif isbn_type == "ISBN10": prop = "isbn_10" self.isbn_10 = isbn_tool.compact(formatted) self.add_statement(prop, formatted, ref=self.source)
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 formatted_isbn(self): """ Returns ISBN in proper format woth dashes. """ return isbn.format(self.isbn)