Murfel has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/184225

Change subject: External isbn packages support
......................................................................

External isbn packages support

Script isbn.py tries to import stdnum.isbn xor (isbnlib or isbn_hyphenate)
libraries. If impossible, it uses custom outdated code.

Bug: T85240
Change-Id: I963ab7b781390564df4f3c2a169b69d22c7f86b1
---
M scripts/isbn.py
1 file changed, 126 insertions(+), 24 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/25/184225/1

diff --git a/scripts/isbn.py b/scripts/isbn.py
index 412214e..a1591df 100755
--- a/scripts/isbn.py
+++ b/scripts/isbn.py
@@ -36,7 +36,7 @@
 
 """
 #
-# (C) Pywikibot team, 2009-2014
+# (C) Pywikibot team, 2009-2015
 #
 # Distributed under the terms of the MIT license.
 #
@@ -46,6 +46,18 @@
 import re
 import pywikibot
 from pywikibot import i18n, pagegenerators, Bot, WikidataBot
+
+try:
+    import stdnum.isbn
+except ImportError:
+    try:
+        import isbnlib
+    except ImportError:
+        pass
+    try:
+        import isbn_hyphenate
+    except ImportError:
+        pass
 
 docuReplacements = {
     '&params;': pagegenerators.parameterHelp,
@@ -1329,9 +1341,76 @@
     return i
 
 
+def unvalidate(code):
+    """Return either first encountered exception or None."""
+    # isbnlib marks any ISBN10 with lowercase 'X' as invalide
+    code = code.upper()
+    library_choosed = False
+    if not library_choosed:
+        try:
+            stdnum.isbn
+        except NameError:
+            pass
+        else:
+            library_choosed = True
+            try:
+                stdnum.isbn.validate(code)
+            except stdnum.isbn.InvalidFormat as e:
+                return e
+            except stdnum.isbn.InvalidChecksum as e:
+                return e
+            except stdnum.isbn.InvalidLength as e:
+                return e
+
+    if not library_choosed:
+        try:
+            isbnlib
+        except NameError:
+            pass
+        else:
+            library_choosed = True
+            if isbnlib.notisbn(code):
+                e = 'Invalid ISBN found'
+                return e
+
+    if not library_choosed:
+        try:
+            getIsbn(code)
+        except InvalidIsbnException as e:
+            return e
+
+    return None
+
+
 def _hyphenateIsbnNumber(match):
     """Helper function to deal with a single ISBN."""
     code = match.group('code')
+    code = code.upper()
+    try:
+        stdnum.isbn
+    except NameError:
+        pass
+    else:
+        try:
+            stdnum.isbn.validate(code)
+            i = stdnum.isbn.format(code)
+        except (stdnum.isbn.InvalidFormat, stdnum.isbn.InvalidChecksum,
+                stdnum.isbn.InvalidLength):
+            return code
+        return i
+
+    try:
+        isbn_hyphenate
+    except NameError:
+        pass
+    else:
+        try:
+            i = isbn_hyphenate.hyphenate(code)
+        except (isbn_hyphenate.IsbnMalformedError,
+                isbn_hyphenate.IsbnUnableToHyphenateError):
+            return code
+        return i
+
     try:
         i = getIsbn(code)
         i.format()
@@ -1351,13 +1430,37 @@
 def _isbn10toIsbn13(match):
     """Helper function to deal with a single ISBN."""
     code = match.group('code')
+    code = code.upper()
+    try:
+        stdnum.isbn
+    except NameError:
+        pass
+    else:
+        try:
+            stdnum.isbn.validate(code)
+            i = stdnum.isbn.to_isbn13(code)
+        except (stdnum.isbn.InvalidFormat, stdnum.isbn.InvalidChecksum,
+                stdnum.isbn.InvalidLength):
+            return code
+        return i
+
+    try:
+        isbnlib
+    except NameError:
+        pass
+    else:
+        if isbnlib.notisbn(code):
+            return code
+        i = isbnlib.to_isbn13(code)
+        return i
+
     try:
         i = getIsbn(code)
+        i.toISBN13()
     except InvalidIsbnException:
         # don't change
         return code
-    i13 = i.toISBN13()
-    return i13.code
+    return i.code
 
 
 def convertIsbn10toIsbn13(text):
@@ -1387,9 +1490,8 @@
             oldText = page.get()
             for match in self.isbnR.finditer(oldText):
                 code = match.group('code')
-                try:
-                    getIsbn(code)
-                except InvalidIsbnException as e:
+                e = unvalidate(code)
+                if e:
                     pywikibot.output(e)
 
             newText = oldText
@@ -1448,56 +1550,56 @@
 
         if self.isbn_10_prop_id in item.claims:
             for claim in item.claims[self.isbn_10_prop_id]:
-                try:
-                    isbn = getIsbn(claim.getTarget())
-                except InvalidIsbnException as e:
+                e = unvalidate(claim.getTarget())
+                if e:
                     pywikibot.output(e)
                     continue
 
-                old_code = claim.getTarget()
+                old_code = "ISBN " + claim.getTarget()
 
                 if self.getOption('format'):
-                    isbn.format()
+                    new_code = hyphenateIsbnNumbers(old_code)
 
                 if self.getOption('to13'):
-                    isbn = isbn.toISBN13()
+                    new_code = convertIsbn10toIsbn13(old_code)
 
                     item.claims[claim.getID()].remove(claim)
                     claim = pywikibot.Claim(self.repo, self.isbn_13_prop_id)
-                    claim.setTarget(isbn.code)
+                    claim.setTarget(new_code)
                     if self.isbn_13_prop_id in item.claims:
                         item.claims[self.isbn_13_prop_id].append(claim)
                     else:
                         item.claims[self.isbn_13_prop_id] = [claim]
                     change_messages.append('Changing %s (%s) to %s (%s)' %
                                            (self.isbn_10_prop_id, old_code,
-                                            self.isbn_13_prop_id, isbn.code))
+                                            self.isbn_13_prop_id, new_code))
                     continue
 
-                if old_code == isbn.code:
+                if old_code == new_code:
                     continue
-                claim.setTarget(isbn.code)
+                # remove 'ISBN ' prefix
+                new_code = new_code[5:]
+                claim.setTarget(new_code)
                 change_messages.append('Changing %s (%s --> %s)' %
                                        (self.isbn_10_prop_id, old_code,
-                                        isbn.code))
+                                        new_code))
 
         # -format is the only option that has any effect on ISBN13
         if self.getOption('format') and self.isbn_13_prop_id in item.claims:
             for claim in item.claims[self.isbn_13_prop_id]:
-                try:
-                    isbn = getIsbn(claim.getTarget())
-                except InvalidIsbnException as e:
+                e = unvalidate(claim.getTarget())
+                if e:
                     pywikibot.output(e)
                     continue
 
                 old_code = claim.getTarget()
-                isbn.format()
-                if old_code == isbn.code:
+                new_code = hyphenateIsbnNumbers(old_code)
+                if old_code == new_code:
                     continue
                 change_messages.append(
                     'Changing %s (%s --> %s)' % (self.isbn_13_prop_id,
-                                                 claim.getTarget(), isbn.code))
-                claim.setTarget(isbn.code)
+                                                 claim.getTarget(), new_code))
+                claim.setTarget(new_code)
 
         if change_messages:
             self.current_page = item

-- 
To view, visit https://gerrit.wikimedia.org/r/184225
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I963ab7b781390564df4f3c2a169b69d22c7f86b1
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Murfel <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to