Merlijn van Deen has uploaded a new change for review. https://gerrit.wikimedia.org/r/80779
Change subject: pep8-ified userinterfaces ...................................................................... pep8-ified userinterfaces Change-Id: If5dd6cad8eb9841dfc560842aa2c146849d96823 --- M pywikibot/userinterfaces/cgi_interface.py M pywikibot/userinterfaces/terminal_interface_base.py M pywikibot/userinterfaces/terminal_interface_unix.py M pywikibot/userinterfaces/terminal_interface_win32.py M pywikibot/userinterfaces/transliteration.py M pywikibot/userinterfaces/win32_unicode.py 6 files changed, 127 insertions(+), 122 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/79/80779/1 diff --git a/pywikibot/userinterfaces/cgi_interface.py b/pywikibot/userinterfaces/cgi_interface.py index 7cf2fd3..21fd9ff 100644 --- a/pywikibot/userinterfaces/cgi_interface.py +++ b/pywikibot/userinterfaces/cgi_interface.py @@ -1,13 +1,14 @@ import sys + class UI: def __init__(self): pass - def output(self, text, colors = None, newline = True, toStdout = False): + def output(self, text, colors=None, newline=True, toStdout=False): if not toStdout: return sys.stdout.write(text.encode('UTF-8', 'replace')) - def input(self, question, colors = None): - self.output(question + ' ', newline = False, showcgi = True) + def input(self, question, colors=None): + self.output(question + ' ', newline=False, showcgi=True) diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py index 3c1caad..84f7102 100755 --- a/pywikibot/userinterfaces/terminal_interface_base.py +++ b/pywikibot/userinterfaces/terminal_interface_base.py @@ -7,7 +7,9 @@ __version__ = '$Id$' import transliteration -import traceback, re, sys +import traceback +import re +import sys import pywikibot as wikipedia from pywikibot import config from pywikibot.bot import DEBUG, VERBOSE, INFO, STDOUT, INPUT, WARNING @@ -40,15 +42,15 @@ class UI: def __init__(self): - self.stdin = sys.stdin + self.stdin = sys.stdin self.stdout = sys.stdout self.stderr = sys.stderr self.encoding = config.console_encoding self.transliteration_target = config.transliteration_target - + self.stderr = sys.stderr self.stdout = sys.stdout - + def init_handlers(self, root_logger, default_stream='stderr'): """Initialize the handlers for user output. @@ -94,7 +96,7 @@ def printNonColorized(self, text, targetStream): # We add *** after the text as a whole if anything needed to be colorized. lines = text.split('\n') - for i,line in enumerate(lines): + for i, line in enumerate(lines): if i > 0: line = "\n" + line line, count = colorTagR.subn('', line) @@ -103,19 +105,18 @@ targetStream.write(line.encode(self.encoding, 'replace')) printColorized = printNonColorized - + def _print(self, text, targetStream): if config.colorized_output: self.printColorized(text, targetStream) else: - self.printNonColorized(text, targetStream) + self.printNonColorized(text, targetStream) def output(self, text, toStdout=False, targetStream=None): """ If a character can't be displayed in the encoding used by the user's terminal, it will be replaced with a question mark or by a transliteration. - """ if config.transliterate: # Encode our unicode string in the encoding used by the user's @@ -139,10 +140,10 @@ if codecedText[i] == '?' and text[i] != u'?': try: transliterated = transliterator.transliterate( - text[i], default='?', prev=prev, next=text[i+1]) + text[i], default='?', prev=prev, next=text[i + 1]) except IndexError: transliterated = transliterator.transliterate( - text[i], default = '?', prev=prev, next=' ') + text[i], default='?', prev=prev, next=' ') # transliteration was successful. The replacement # could consist of multiple letters. # mark the transliterated letters in yellow. @@ -158,19 +159,19 @@ transliteratedText += codecedText[i] prev = codecedText[i] text = transliteratedText - + if not targetStream: if toStdout: targetStream = self.stdout else: targetStream = self.stderr - + self._print(text, targetStream) def _raw_input(self): return raw_input() - - def input(self, question, password = False): + + def input(self, question, password=False): """ Ask the user a question and return the answer. @@ -178,7 +179,6 @@ Unlike raw_input, this function automatically adds a space after the question. - """ # sound the terminal bell to notify the user @@ -198,7 +198,7 @@ """ Ask the user a question with a predefined list of acceptable answers. """ - options = options[:] # we don't want to edit the passed parameter + options = options[:] # we don't want to edit the passed parameter for i in range(len(options)): option = options[i] hotkey = hotkeys[i] @@ -211,7 +211,7 @@ if m: pos = m.start() options[i] = '%s[%s]%s' % (option[:pos], caseHotkey, - option[pos+1:]) + option[pos + 1:]) else: options[i] = '%s [%s]' % (option, caseHotkey) # loop until the user entered a valid choice @@ -220,7 +220,7 @@ answer = self.input(prompt) if answer.lower() in hotkeys or answer.upper() in hotkeys: return answer - elif default and answer=='': # empty string entered + elif default and answer == '': # empty string entered return default def editText(self, text, jumpIndex=None, highlight=None): @@ -262,6 +262,7 @@ return wikipedia.input( u'What is the solution of the CAPTCHA at this url ?') + class TerminalHandler(logging.Handler): """A handler class that writes logging records, appropriately formatted, to a stream connected to a terminal. This class does not close the stream, @@ -299,7 +300,7 @@ def emit(self, record): text = self.format(record) - return self.UI.output(text, targetStream = self.stream) + return self.UI.output(text, targetStream=self.stream) class TerminalFormatter(logging.Formatter): @@ -320,4 +321,4 @@ if self.level: return record.levelno <= self.level else: - return True \ No newline at end of file + return True diff --git a/pywikibot/userinterfaces/terminal_interface_unix.py b/pywikibot/userinterfaces/terminal_interface_unix.py index d2eb7cf..0ba718c 100755 --- a/pywikibot/userinterfaces/terminal_interface_unix.py +++ b/pywikibot/userinterfaces/terminal_interface_unix.py @@ -36,6 +36,7 @@ 'lightyellow': chr(27) + '[33;1m', # Light Yellow start tag } + class UnixUI(terminal_interface_base.UI): def printColorized(self, text, targetStream): lastColor = None diff --git a/pywikibot/userinterfaces/terminal_interface_win32.py b/pywikibot/userinterfaces/terminal_interface_win32.py index bfa8b5d..3507701 100755 --- a/pywikibot/userinterfaces/terminal_interface_win32.py +++ b/pywikibot/userinterfaces/terminal_interface_win32.py @@ -37,6 +37,7 @@ colorTagR = re.compile('\03{(?P<name>%s)}' % '|'.join(windowsColors.keys())) + # Compat for python <= 2.5 class Win32BaseUI(terminal_interface_base.UI): def __init__(self): diff --git a/pywikibot/userinterfaces/transliteration.py b/pywikibot/userinterfaces/transliteration.py index 723452c..0bb1f3b 100644 --- a/pywikibot/userinterfaces/transliteration.py +++ b/pywikibot/userinterfaces/transliteration.py @@ -255,47 +255,47 @@ self.trans[u"³"] = u"3" # Cyrillic - self.trans.update({u"А" : u"A", u"а" : u"a", u"Б" : u"B", u"б" : u"b", - u"В" : u"V", u"в" : u"v", u"Г" : u"G", u"г" : u"g", - u"Д" : u"D", u"д" : u"d", u"Е" : u"E", u"е" : u"e", - u"Ж" : u"Zh", u"ж" : u"zh", u"З" : u"Z", u"з" : u"z", - u"И" : u"I", u"и" : u"i", u"Й" : u"J", u"й" : u"j", - u"К" : u"K", u"к" : u"k", u"Л" : u"L", u"л" : u"l", - u"М" : u"M", u"м" : u"m", u"Н" : u"N", u"н" : u"n", - u"О" : u"O", u"о" : u"o", u"П" : u"P", u"п" : u"p", - u"Р" : u"R", u"р" : u"r", u"С" : u"S", u"с" : u"s", - u"Т" : u"T", u"т" : u"t", u"У" : u"U", u"у" : u"u", - u"Ф" : u"F", u"ф" : u"f", u"х" : u"kh", u"Ц" : u"C", - u"ц" : u"c", u"Ч" : u"Ch", u"ч" : u"ch", u"Ш" : u"Sh", - u"ш" : u"sh", u"Щ" : u"Shch", u"щ" : u"shch", u"Ь" : u"'", - u"ь" : "'", u"Ъ" : u'"', u"ъ" : '"', u"Ю" : u"Yu", - u"ю" : u"yu", u"Я" : u"Ya", u"я" : u"ya", u"Х" : u"Kh", - u"Χ" : u"Kh"}) + self.trans.update({u"А": u"A", u"а": u"a", u"Б": u"B", u"б": u"b", + u"В": u"V", u"в": u"v", u"Г": u"G", u"г": u"g", + u"Д": u"D", u"д": u"d", u"Е": u"E", u"е": u"e", + u"Ж": u"Zh", u"ж": u"zh", u"З": u"Z", u"з": u"z", + u"И": u"I", u"и": u"i", u"Й": u"J", u"й": u"j", + u"К": u"K", u"к": u"k", u"Л": u"L", u"л": u"l", + u"М": u"M", u"м": u"m", u"Н": u"N", u"н": u"n", + u"О": u"O", u"о": u"o", u"П": u"P", u"п": u"p", + u"Р": u"R", u"р": u"r", u"С": u"S", u"с": u"s", + u"Т": u"T", u"т": u"t", u"У": u"U", u"у": u"u", + u"Ф": u"F", u"ф": u"f", u"х": u"kh", u"Ц": u"C", + u"ц": u"c", u"Ч": u"Ch", u"ч": u"ch", u"Ш": u"Sh", + u"ш": u"sh", u"Щ": u"Shch", u"щ": u"shch", u"Ь": u"'", + u"ь": "'", u"Ъ": u'"', u"ъ": '"', u"Ю": u"Yu", + u"ю": u"yu", u"Я": u"Ya", u"я": u"ya", u"Х": u"Kh", + u"Χ": u"Kh"}) # Additional Cyrillic letters, most occuring in only one or a few languages - self.trans.update({u"Ы" : u"Y", u"ы" : u"y", u"Ё" : u"Ë", u"ё" : u"ë", - u"Э" : u"È", u"Ѐ" : u"È", u"э" : u"è", u"ѐ" : u"è", - u"І" : u"I", u"і" : u"i", u"Ї" : u"Ji", u"ї" : u"ji", - u"Є" : u"Je", u"є" : u"je", u"Ґ" : u"G", u"Ҝ" : u"G", - u"ґ" : u"g", u"ҝ" : u"g", u"Ђ" : u"Dj", u"ђ" : u"dj", - u"Ӣ" : u"Y", u"ӣ" : u"y", u"Љ" : u"Lj", u"љ" : u"lj", - u"Њ" : u"Nj", u"њ" : u"nj", u"Ћ" : u"Cj", u"ћ" : u"cj", - u"Җ" : u"Zhj", u"җ" : u"zhj", u"Ѓ" : u"Gj", u"ѓ" : u"gj", - u"Ќ" : u"Kj", u"ќ" : u"kj", u"Ӣ" : u"Ii", u"ӣ" : u"ii", - u"Ӯ" : u"U", u"ӯ" : u"u", u"Ҳ" : u"H", u"ҳ" : u"h", - u"Ҷ" : u"Dz",u"ҷ" : u"dz", u"Ө" :u"Ô", u"Ӫ" : u"Ô", - u"ө" : u"ô", u"ӫ" : u"ô", u"Ү": u"Y", u"ү": u"y", u"Һ": u"H", - u"һ": u"h", u"Ә": u"AE", u"Ӕ": u"AE", u"ә": u"ae", - u"Ӛ": u"Ë", u"Ӭ": u"Ë", u"ӛ": u"ë", u"ӭ": u"ë", u"Җ": u"Zhj", - u"җ": u"zhj", u"Ұ": u"U", u"ұ": u"u", u"ў": u"ù", u"Ў": u"Ù", - u"ѝ": u"ì", u"Ѝ": u"Ì", u"Ӑ": u"A", u"ă": u"a", u"Ӓ": u"Ä", - u"ҿ": u"ä", u"Ҽ" : u"Ts", u"Ҿ": u"Ts", u"ҽ": u"ts", u"ҿ": u"ts", - u"Ҙ": u"Dh", u"ҙ": u"dh", u"Ӏ": u"", u"ӏ": u"", u"Ӆ": u"L", - u"ӆ": u"l", u"Ӎ": u"M", u"ӎ": u"m", u"Ӧ": u"Ö", u"ӧ": u"ö", - u"Ҩ": u"u", u"ҩ": u"u", u"Ҧ": u"Ph", u"ҧ": u"ph", u"Ҏ": u"R", - u"ҏ": u"r", u"Ҫ": u"Th", u"ҫ": u"th", u"Ҭ": u"T", u"ҭ": u"t", - u"Ӯ": u"Û", u"ӯ": u"û", u"Ұ": u"U", u"Ӹ": u"U", u"ұ": u"u", - u"ӹ": u"u", u"Ҵ": u"Tts", u"ҵ": u"tts", u"Ӵ": u"Ch", u"ӵ": u"ch"}) + self.trans.update({u"Ы": u"Y", u"ы": u"y", u"Ё": u"Ë", u"ё": u"ë", + u"Э": u"È", u"Ѐ": u"È", u"э": u"è", u"ѐ": u"è", + u"І": u"I", u"і": u"i", u"Ї": u"Ji", u"ї": u"ji", + u"Є": u"Je", u"є": u"je", u"Ґ": u"G", u"Ҝ": u"G", + u"ґ": u"g", u"ҝ": u"g", u"Ђ": u"Dj", u"ђ": u"dj", + u"Ӣ": u"Y", u"ӣ": u"y", u"Љ": u"Lj", u"љ": u"lj", + u"Њ": u"Nj", u"њ": u"nj", u"Ћ": u"Cj", u"ћ": u"cj", + u"Җ": u"Zhj", u"җ": u"zhj", u"Ѓ": u"Gj", u"ѓ": u"gj", + u"Ќ": u"Kj", u"ќ": u"kj", u"Ӣ": u"Ii", u"ӣ": u"ii", + u"Ӯ": u"U", u"ӯ": u"u", u"Ҳ": u"H", u"ҳ": u"h", + u"Ҷ": u"Dz", u"ҷ": u"dz", u"Ө": u"Ô", u"Ӫ": u"Ô", + u"ө": u"ô", u"ӫ": u"ô", u"Ү": u"Y", u"ү": u"y", u"Һ": u"H", + u"һ": u"h", u"Ә": u"AE", u"Ӕ": u"AE", u"ә": u"ae", + u"Ӛ": u"Ë", u"Ӭ": u"Ë", u"ӛ": u"ë", u"ӭ": u"ë", u"Җ": u"Zhj", + u"җ": u"zhj", u"Ұ": u"U", u"ұ": u"u", u"ў": u"ù", u"Ў": u"Ù", + u"ѝ": u"ì", u"Ѝ": u"Ì", u"Ӑ": u"A", u"ă": u"a", u"Ӓ": u"Ä", + u"ҿ": u"ä", u"Ҽ": u"Ts", u"Ҿ": u"Ts", u"ҽ": u"ts", u"ҿ": u"ts", + u"Ҙ": u"Dh", u"ҙ": u"dh", u"Ӏ": u"", u"ӏ": u"", u"Ӆ": u"L", + u"ӆ": u"l", u"Ӎ": u"M", u"ӎ": u"m", u"Ӧ": u"Ö", u"ӧ": u"ö", + u"Ҩ": u"u", u"ҩ": u"u", u"Ҧ": u"Ph", u"ҧ": u"ph", u"Ҏ": u"R", + u"ҏ": u"r", u"Ҫ": u"Th", u"ҫ": u"th", u"Ҭ": u"T", u"ҭ": u"t", + u"Ӯ": u"Û", u"ӯ": u"û", u"Ұ": u"U", u"Ӹ": u"U", u"ұ": u"u", + u"ӹ": u"u", u"Ҵ": u"Tts", u"ҵ": u"tts", u"Ӵ": u"Ch", u"ӵ": u"ch"}) for char in u"ЈӤҊ": self.trans[char] = u"J" @@ -332,10 +332,10 @@ # Archaic Cyrillic letters self.trans.update({u"Ѹ": u"Ou", u"ѹ": u"ou", u"Ѡ": u"O", u"Ѻ": u"O", u"ѡ": u"o", - u"ѻ": u"o", u"Ѿ": u"Ot", u"ѿ": u"ot", u"Ѣ": u"E", u"ѣ": u"e", - u"Ѥ": u"Ei", u"Ѧ": u"Ei", u"ѥ": u"ei", u"ѧ": u"ei", u"Ѫ": u"Ai", - u"ѫ": u"ai", u"Ѯ": u"X", u"ѯ": u"x", u"Ѱ": u"Ps", u"ѱ": u"ps", - u"Ѳ": u"Th", u"ѳ": u"th", u"Ѵ": u"Ü", u"Ѷ": u"Ü", u"ѵ": u"ü"}) + u"ѻ": u"o", u"Ѿ": u"Ot", u"ѿ": u"ot", u"Ѣ": u"E", u"ѣ": u"e", + u"Ѥ": u"Ei", u"Ѧ": u"Ei", u"ѥ": u"ei", u"ѧ": u"ei", u"Ѫ": u"Ai", + u"ѫ": u"ai", u"Ѯ": u"X", u"ѯ": u"x", u"Ѱ": u"Ps", u"ѱ": u"ps", + u"Ѳ": u"Th", u"ѳ": u"th", u"Ѵ": u"Ü", u"Ѷ": u"Ü", u"ѵ": u"ü"}) # Hebrew alphabet for char in u"אע": @@ -443,7 +443,7 @@ self.trans[u"ؤ"] = u"w'" self.trans[u"ئ"] = u"y'" for char in u"◌◌": - self.trans[char] = u"" # indicates absence of vowels + self.trans[char] = u"" # indicates absence of vowels # Arabic vowels self.trans[u"◌"] = u"a" self.trans[u"◌"] = u"u" @@ -486,22 +486,22 @@ # Greek self.trans.update({u"Α": u"A", u"α": u"a", u"Β": u"B", u"β": u"b", u"Γ": u"G", - u"γ": u"g", u"Δ": u"D", u"δ": u"d", u"Ε": u"E", u"ε": u"e", - u"Ζ": u"Z", u"ζ": u"z", u"Η": u"I", u"η": u"i", u"θ": u"th", - u"Θ": u"Th", u"Ι": u"I", u"ι": u"i", u"Κ": u"K", u"κ": u"k", - u"Λ": u"L", u"λ": u"l", u"Μ": u"M", u"μ": u"m", u"Ν": u"N", - u"ν": u"n", u"Ξ": u"X", u"ξ": u"x", u"Ο": u"O", u"ο": u"o", - u"Π": u"P", u"π": u"p", u"Ρ": u"R", u"ρ": u"r", u"Σ": u"S", - u"σ": u"s", u"ς": u"s", u"Τ": u"T", u"τ": u"t", u"Υ": u"Y", - u"υ": u"y", u"Φ": u"F", u"φ": u"f", u"Ψ": u"Ps", u"ψ": u"ps", - u"Ω": u"O", u"ω": u"o", u"ϗ": u"&", u"Ϛ": u"St", u"ϛ": u"st", - u"Ϙ": u"Q", u"Ϟ": u"Q", u"ϙ": u"q", u"ϟ": u"q", u"Ϻ": u"S", - u"ϻ": u"s", u"Ϡ": u"Ss", u"ϡ": u"ss", u"Ϸ": u"Sh", u"ϸ": u"sh", - u"·": u":", u"Ά": u"Á", u"ά": u"á", u"Έ": u"É", u"Ή": u"É", - u"έ": u"é", u"ή": u"é", u"Ί": u"Í", u"ί": u"í", u"Ϊ": u"Ï", - u"ϊ": u"ï", u"ΐ": u"ï", u"Ό": u"Ó", u"ό": u"ó", u"Ύ": u"Ý", - u"ύ": u"ý", u"Ϋ": u"Y", u"ϋ": u"ÿ", u"ΰ": u"ÿ", u"Ώ": u"Ó", - u"ώ": u"ó"}) + u"γ": u"g", u"Δ": u"D", u"δ": u"d", u"Ε": u"E", u"ε": u"e", + u"Ζ": u"Z", u"ζ": u"z", u"Η": u"I", u"η": u"i", u"θ": u"th", + u"Θ": u"Th", u"Ι": u"I", u"ι": u"i", u"Κ": u"K", u"κ": u"k", + u"Λ": u"L", u"λ": u"l", u"Μ": u"M", u"μ": u"m", u"Ν": u"N", + u"ν": u"n", u"Ξ": u"X", u"ξ": u"x", u"Ο": u"O", u"ο": u"o", + u"Π": u"P", u"π": u"p", u"Ρ": u"R", u"ρ": u"r", u"Σ": u"S", + u"σ": u"s", u"ς": u"s", u"Τ": u"T", u"τ": u"t", u"Υ": u"Y", + u"υ": u"y", u"Φ": u"F", u"φ": u"f", u"Ψ": u"Ps", u"ψ": u"ps", + u"Ω": u"O", u"ω": u"o", u"ϗ": u"&", u"Ϛ": u"St", u"ϛ": u"st", + u"Ϙ": u"Q", u"Ϟ": u"Q", u"ϙ": u"q", u"ϟ": u"q", u"Ϻ": u"S", + u"ϻ": u"s", u"Ϡ": u"Ss", u"ϡ": u"ss", u"Ϸ": u"Sh", u"ϸ": u"sh", + u"·": u":", u"Ά": u"Á", u"ά": u"á", u"Έ": u"É", u"Ή": u"É", + u"έ": u"é", u"ή": u"é", u"Ί": u"Í", u"ί": u"í", u"Ϊ": u"Ï", + u"ϊ": u"ï", u"ΐ": u"ï", u"Ό": u"Ó", u"ό": u"ó", u"Ύ": u"Ý", + u"ύ": u"ý", u"Ϋ": u"Y", u"ϋ": u"ÿ", u"ΰ": u"ÿ", u"Ώ": u"Ó", + u"ώ": u"ó"}) # Japanese (katakana and hiragana) for char in u"アァあ": @@ -736,11 +736,11 @@ self.trans[char] = u"v" for char in u"ზ": self.trans[char] = u"z" - for char in u"თ":# + for char in u"თ": self.trans[char] = u"th" for char in u"ი": self.trans[char] = u"i" - for char in u"კ":# + for char in u"კ": self.trans[char] = u"k" for char in u"ლ": self.trans[char] = u"l" @@ -750,25 +750,25 @@ self.trans[char] = u"n" for char in u"ო": self.trans[char] = u"o" - for char in u"პ":# + for char in u"პ": self.trans[char] = u"p" - for char in u"ჟ":# + for char in u"ჟ": self.trans[char] = u"zh" for char in u"რ": self.trans[char] = u"r" for char in u"ს": self.trans[char] = u"s" - for char in u"ტ":# + for char in u"ტ": self.trans[char] = u"t" for char in u"უ": self.trans[char] = u"u" - for char in u"ფ":# + for char in u"ფ": self.trans[char] = u"ph" - for char in u"ქ":# + for char in u"ქ": self.trans[char] = u"q" - for char in u"ღ":# + for char in u"ღ": self.trans[char] = u"gh" - for char in u"ყ":# + for char in u"ყ": self.trans[char] = u"q'" for char in u"შ": self.trans[char] = u"sh" @@ -778,13 +778,13 @@ self.trans[char] = u"ts" for char in u"ძ": self.trans[char] = u"dz" - for char in u"წ":# + for char in u"წ": self.trans[char] = u"ts'" - for char in u"ჭ":# + for char in u"ჭ": self.trans[char] = u"ch'" for char in u"ხ": self.trans[char] = u"kh" - for char in u"ჯ":# + for char in u"ჯ": self.trans[char] = u"j" for char in u"ჰ": self.trans[char] = u"h" @@ -1659,12 +1659,13 @@ self.trans[u"໙"] = "9" for char in self.trans: value = self.trans[char] - if value == "?": continue + if value == "?": + continue while value.encode(encoding, 'replace').decode(encoding) == "?" and value in self.trans: assert value != self.trans[value], "%r == self.trans[%r]!" % (value, value) value = self.trans[value] self.trans[char] = value - + def transliterate(self, char, default="?", prev="-", next="-"): if char in self.trans: return self.trans[char] diff --git a/pywikibot/userinterfaces/win32_unicode.py b/pywikibot/userinterfaces/win32_unicode.py index ae00ef6..3646dc0 100755 --- a/pywikibot/userinterfaces/win32_unicode.py +++ b/pywikibot/userinterfaces/win32_unicode.py @@ -2,7 +2,7 @@ ############################################## # Support for unicode in windows cmd.exe # Posted on Stack Overflow [1], available under CC-BY-SA [2] -# +# # Question: "Windows cmd encoding change causes Python crash" [3] by Alex [4], # Answered [5] by David-Sarah Hopwood [6]. # @@ -27,8 +27,8 @@ argv = sys.argv if sys.platform == "win32": import codecs - from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int, \ - create_unicode_buffer + from ctypes import WINFUNCTYPE, windll, POINTER + from ctypes import byref, c_int, create_unicode_buffer from ctypes.wintypes import BOOL, HANDLE, DWORD, LPWSTR, LPCWSTR try: from ctypes.wintypes import LPVOID @@ -66,12 +66,11 @@ GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32)) STD_INPUT_HANDLE = DWORD(-10) STD_OUTPUT_HANDLE = DWORD(-11) - STD_ERROR_HANDLE = DWORD(-12) + STD_ERROR_HANDLE = DWORD(-12) GetFileType = WINFUNCTYPE(DWORD, DWORD)(("GetFileType", windll.kernel32)) - FILE_TYPE_CHAR = 0x0002 + FILE_TYPE_CHAR = 0x0002 FILE_TYPE_REMOTE = 0x8000 - GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD)) \ - (("GetConsoleMode", windll.kernel32)) + GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(("GetConsoleMode", windll.kernel32)) INVALID_HANDLE_VALUE = DWORD(-1).value def not_a_console(handle): @@ -80,21 +79,21 @@ return ((GetFileType(handle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR or GetConsoleMode(handle, byref(DWORD())) == 0) - old_stdin_fileno = None + old_stdin_fileno = None old_stdout_fileno = None old_stderr_fileno = None if hasattr(sys.stdin, 'fileno'): - old_stdin_fileno = sys.stdin.fileno() + old_stdin_fileno = sys.stdin.fileno() if hasattr(sys.stdout, 'fileno'): old_stdout_fileno = sys.stdout.fileno() if hasattr(sys.stderr, 'fileno'): old_stderr_fileno = sys.stderr.fileno() - STDIN_FILENO = 0 + STDIN_FILENO = 0 STDOUT_FILENO = 1 STDERR_FILENO = 2 - real_stdin = (old_stdin_fileno == STDIN_FILENO) + real_stdin = (old_stdin_fileno == STDIN_FILENO) real_stdout = (old_stdout_fileno == STDOUT_FILENO) real_stderr = (old_stderr_fileno == STDERR_FILENO) @@ -102,7 +101,7 @@ hStdin = GetStdHandle(STD_INPUT_HANDLE) if not_a_console(hStdin): real_stdin = False - + if real_stdout: hStdout = GetStdHandle(STD_OUTPUT_HANDLE) if not_a_console(hStdout): @@ -116,7 +115,7 @@ if real_stdin: ReadConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPVOID, DWORD, POINTER(DWORD), \ LPVOID)(("ReadConsoleW", windll.kernel32)) - + class UnicodeInput: def __init__(self, hConsole, name, bufsize=1024): self._hConsole = hConsole @@ -124,15 +123,14 @@ self.buffer = create_unicode_buffer(bufsize) self.name = name self.encoding = 'utf-8' - + def readline(self): - maxnum = DWORD(self.bufsize-1) + maxnum = DWORD(self.bufsize - 1) numrecv = DWORD(0) result = ReadConsoleW(self._hConsole, self.buffer, maxnum, byref(numrecv), None) if not result: raise Exception("stdin failure") return self.buffer.value[:numrecv.value].encode(self.encoding) - if real_stdout or real_stderr: # BOOL WINAPI WriteConsoleW(HANDLE hOutput, LPWSTR lpBuffer, DWORD nChars, @@ -155,11 +153,14 @@ def isatty(self): return False + def close(self): # don't really close the handle, that would only cause problems self.closed = True + def fileno(self): return self._fileno + def flush(self): if self._hConsole is None: try: @@ -191,7 +192,8 @@ raise IOError("WriteConsoleW returned %r, n.value = %r" % (retval, n.value)) remaining -= n.value - if remaining == 0: break + if remaining == 0: + break text = text[n.value:] except Exception, e: _complain("%s.write: %r" % (self.name, e)) @@ -206,31 +208,29 @@ raise if real_stdin: - stdin = UnicodeInput(hStdin, name='<Unicode console stdin>') + stdin = UnicodeInput(hStdin, name='<Unicode console stdin>') if real_stdout: stdout = UnicodeOutput(hStdout, None, STDOUT_FILENO, - '<Unicode console stdout>') + '<Unicode console stdout>') else: stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, - '<Unicode redirected stdout>') + '<Unicode redirected stdout>') if real_stderr: stderr = UnicodeOutput(hStderr, None, STDERR_FILENO, - '<Unicode console stderr>') + '<Unicode console stderr>') else: stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, - '<Unicode redirected stderr>') + '<Unicode redirected stderr>') except Exception, e: _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,)) - # While we're at it, let's unmangle the command-line arguments: # This works around <http://bugs.python.org/issue2128>. GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32)) - CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int)) \ - (("CommandLineToArgvW", windll.shell32)) + CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(("CommandLineToArgvW", windll.shell32)) argc = c_int(0) argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc)) -- To view, visit https://gerrit.wikimedia.org/r/80779 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If5dd6cad8eb9841dfc560842aa2c146849d96823 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Merlijn van Deen <valhall...@arctus.nl> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits