Yujwey has uploaded a new change for review.

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

Change subject: Replaced casechecker.SetColor with new colour tools in 
pywikibot/tools/formatter.py
......................................................................

Replaced casechecker.SetColor with new colour tools in 
pywikibot/tools/formatter.py

Change-Id: I5a14298f0c1cb70e4911aed751226e9e666851bc
---
M scripts/casechecker.py
1 file changed, 31 insertions(+), 78 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/97/261097/1

diff --git a/scripts/casechecker.py b/scripts/casechecker.py
index d1858de..de26961 100755
--- a/scripts/casechecker.py
+++ b/scripts/casechecker.py
@@ -19,7 +19,7 @@
 from pywikibot import i18n
 
 from pywikibot.data import api
-from pywikibot.tools import first_lower, first_upper
+from pywikibot.tools import first_lower, first_upper, formatter
 
 from scripts.category import CategoryMoveRobot as CategoryMoveBot
 
@@ -40,51 +40,6 @@
                 yield [items[i]] + cc
 # End of permutation code
 #
-
-#
-# Windows Concole colors
-# This code makes this script Windows ONLY!!!
-# Feel free to adapt it to another platform
-#
-# Adapted from https://code.activestate.com/recipes/496901/
-#
-STD_OUTPUT_HANDLE = -11
-
-FOREGROUND_BLUE = 0x01  # text color contains blue.
-FOREGROUND_GREEN = 0x02  # text color contains green.
-FOREGROUND_RED = 0x04  # text color contains red.
-FOREGROUND_INTENSITY = 0x08  # text color is intensified.
-BACKGROUND_BLUE = 0x10  # background color contains blue.
-BACKGROUND_GREEN = 0x20  # background color contains green.
-BACKGROUND_RED = 0x40  # background color contains red.
-BACKGROUND_INTENSITY = 0x80  # background color is intensified.
-
-FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
-
-try:
-    import ctypes
-    std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
-except:
-    std_out_handle = None
-
-
-def SetColor(color):
-    if std_out_handle:
-        try:
-            return ctypes.windll.kernel32.SetConsoleTextAttribute(
-                std_out_handle, color)
-        except:
-            pass
-
-    if color == FOREGROUND_BLUE:
-        print('(b:', end=' ')
-    if color == FOREGROUND_GREEN:
-        print('(g:', end=' ')
-    if color == FOREGROUND_RED:
-        print('(r:', end=' ')
-
-# end of console code
-
 
 class CaseChecker(object):
 
@@ -122,6 +77,10 @@
     lclClrFnt = u'<font color=green>'
     latClrFnt = u'<font color=brown>'
     suffixClr = u'</font>'
+
+    screenLclClrFnt = u'{green}'
+    screenLatClrFnt = u'{red}'
+    screenSuffixClr = u'{default}'
 
     wordBreaker = re.compile(r'[ _\-/\|#[\]():]')
     stripChars = u' \t,'
@@ -618,7 +577,6 @@
                         for bw in wordsToLat:
                             title2 = title2.replace(bw, mapLat[bw])
                         possibleAlternatives.append(title2)
-
                 if len(possibleAlternatives) > 0:
                     infoText += u", can be converted to " + u', '.join(
                         [self.MakeLink(t) for t in possibleAlternatives])
@@ -683,42 +641,37 @@
     def ColorCodeWord(self, word, toScreen=False):
         if not toScreen:
             res = u"<b>"
-        lastIsCyr = word[0] in self.localLtr
-        if lastIsCyr:
-            if toScreen:
-                SetColor(FOREGROUND_GREEN)
-            else:
+            lastIsCyr = word[0] in self.localLtr
+            if lastIsCyr:
                 res += self.lclClrFnt
-        else:
-            if toScreen:
-                SetColor(FOREGROUND_RED)
-            else:
-                res += self.latClrFnt
-
-        for l in word:
-            if l in self.localLtr:
-                if not lastIsCyr:
-                    if toScreen:
-                        SetColor(FOREGROUND_GREEN)
-                    else:
+            for l in word:
+                if l in self.localLtr:
+                    if not lastIsCyr:
                         res += self.suffixClr + self.lclClrFnt
-                    lastIsCyr = True
-            elif l in self.latLtr:
-                if lastIsCyr:
-                    if toScreen:
-                        SetColor(FOREGROUND_RED)
-                    else:
+                        lastIsCyr = True
+                elif l in self.latLtr:
+                    if lastIsCyr:
                         res += self.suffixClr + self.latClrFnt
-                    lastIsCyr = False
-            if toScreen:
-                pywikibot.output(l, newline=False)
-            else:
+                        lastIsCyr=False
                 res += l
-
-        if toScreen:
-            SetColor(FOREGROUND_WHITE)
-        else:
             return res + self.suffixClr + u"</b>"
+        else:
+            res = u""
+            lastIsCyr = word[0] in self.localLtr
+            if lastIsCyr:
+                res += self.screenLclClrFnt
+            for l in word:
+                if l in self.localLtr:
+                    if not lastIsCyr:
+                        res += self.screenLclClrFnt
+                        lastIsCyr = True
+                elif l in self.latLtr:
+                    if lastIsCyr:
+                        res += self.screenLatClrFnt
+                        lastIsCyr=False
+                res += l
+            res = formatter.color_format(res + self.screenSuffixClr)
+            pywikibot.output(res)
 
     def AddNoSuggestionTitle(self, title):
         if title in self.seenUnresolvedLinks:

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

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

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

Reply via email to