Revision: 4546 Author: yurik Date: 2007-11-15 05:14:45 +0000 (Thu, 15 Nov 2007)
Log Message: ----------- added retries to query request decoding. casechecker minor changes
Modified Paths: -------------- trunk/pywikipedia/casechecker.py trunk/pywikipedia/query.py
Modified: trunk/pywikipedia/casechecker.py =================================================================== --- trunk/pywikipedia/casechecker.py 2007-11-14 22:54:12 UTC (rev 4545) +++ trunk/pywikipedia/casechecker.py 2007-11-15 05:14:45 UTC (rev 4546) @@ -23,23 +23,39 @@ # Windows Concose colors # This code makes this script Windows ONLY!!! Feel free to adapt it to another platform # +# Adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496901 # -FOREGROUND_BLUE = 1 -FOREGROUND_GREEN = 2 -FOREGROUND_RED = 4 +STD_OUTPUT_HANDLE= -11
-FOREGROUND_WHITE = 1|2|4 +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): - try: - import win32console - stdout=win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE) - stdout.SetConsoleTextAttribute(color) - except: - if color == FOREGROUND_BLUE: print '(b:' - if color == FOREGROUND_GREEN: print '(g:' - if color == FOREGROUND_RED: print '(r:' + if std_out_handle: + try: + return ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color) + except: + pass
+ if color == FOREGROUND_BLUE: print '(b:', + if color == FOREGROUND_GREEN: print '(g:', + if color == FOREGROUND_RED: print '(r:', + # end of console code
import os @@ -47,6 +63,14 @@
class CaseChecker( object ): + msgRename = { + 'en': u'mixed case rename', + 'ru': u'[[ВП:КЛ]]', + } + msgLinkReplacement = { + 'en': u'Case Replacements', + 'ru': u'[[ВП:КЛ]]', + }
langs = { 'ru': { @@ -207,10 +231,10 @@ else: changed = False if self.replace: - newTitle = self.PickTarget(False, title, err[1]) + newTitle = self.PickTarget(False, title, title, err[1]) if newTitle: src = wikipedia.Page(self.site, title) - src.move( newTitle, u'mixed case rename') + src.move( newTitle, wikipedia.translate(self.site, self.msgRename)) changed = True
if not changed: @@ -238,7 +262,7 @@ if err: newTitle = None if self.replace: - newTitle = self.PickTarget(True, ltxt, err[1]) + newTitle = self.PickTarget(True, title, ltxt, err[1]) if newTitle: if pageObj is None: pageObj = wikipedia.Page(self.site, title) @@ -274,7 +298,7 @@ else: wikipedia.output(u'Case Replacements: %s' % u', '.join(msg)) try: - pageObj.put(pageTxt, u'Case Replacements: %s' % u', '.join(msg)) + pageObj.put(pageTxt, u'%s: %s' % (wikipedia.translate(self.site, self.msgLinkReplacement), u', '.join(msg))) except KeyboardInterrupt: raise except: @@ -390,7 +414,7 @@
return (infoText, possibleAlternatives)
- def PickTarget(self, isLink, original, candidates): + def PickTarget(self, isLink, title, original, candidates): if len(candidates) == 0: return None
@@ -428,7 +452,7 @@ return pagesRedir.keys()[0]
if not self.autonomous: - wikipedia.output(u'Could not auto-decide. Which should be chosen?') + wikipedia.output(u'Could not auto-decide for page [[%s]]. Which link should be chosen?' % title) wikipedia.output(u'Original title: ', newline=False) self.ColorCodeWord(original + "\n", True) count = 1
Modified: trunk/pywikipedia/query.py =================================================================== --- trunk/pywikipedia/query.py 2007-11-14 22:54:12 UTC (rev 4545) +++ trunk/pywikipedia/query.py 2007-11-15 05:14:45 UTC (rev 4546) @@ -36,15 +36,24 @@ wikipedia.output( u"Requesting %d titles from %s:%s" % (titlecount, lang, path) )
url = site.family.querypath(lang) + + retryCount = 1
- jsontext = site.getUrl( path, retry=True, data=data ) + while retryCount >= 0: + try: + jsontext = site.getUrl( path, retry=True, data=data )
+ # This will also work, but all unicode strings will need to be converted from \u notation + # decodedObj = eval( jsontext ) + decodedObj = simplejson.loads( jsontext ) + break + + except ValueError, error: + retryCount -= 1 + wikipedia.output( u"Error downloading data: %s" % error ) + + return decodedObj
- # This will also work, but all unicode strings will need to be converted from \u notation - # return eval( jsontext ) - - return simplejson.loads( jsontext ) - def GetInterwikies( lang, titles, extraParams = None ): """ Usage example: data = GetInterwikies('ru','user:yurik') titles may be either ane title (as a string), or a list of strings