Revision: 3942
Author: valhallasw
Date: 2007-08-01 23:31:35 +0000 (Wed, 01 Aug 2007)
Log Message:
-----------
Changes to allow solve_disambiguation to put asynchronously:
* while waiting for input through wikipedia.input(), wikipedia.output() buffers its output and flushes when input has been received
* solve_disambiguation now uses put_async
Small bugfix: when calling the terminal_interface editor, the ImportError raised when tkinter is not installed is caught.
Modified Paths:
--------------
trunk/pywikipedia/solve_disambiguation.py
trunk/pywikipedia/userinterfaces/terminal_interface.py
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/solve_disambiguation.py
===================================================================
--- trunk/pywikipedia/solve_disambiguation.py 2007-08-01 23:17:32 UTC (rev 3941)
+++ trunk/pywikipedia/solve_disambiguation.py 2007-08-01 23:31:35 UTC (rev 3942)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Script to help a human solve disambiguations by presenting a set of options.
@@ -75,7 +75,7 @@
import wikipedia, pagegenerators, editarticle
# This is a purely interactive robot. We set the delays lower.
-wikipedia.put_throttle.setDelay(4)
+#wikipedia.put_throttle.setDelay(4)
# Summary message when working on disambiguation pages
msg = {
@@ -523,7 +523,7 @@
if choice in ['y', 'Y']:
redir_text = '#%s [[%s]]' % (self.mysite.redirect(default=True), target)
try:
- refPage.put(redir_text)
+ refPage.put_async(redir_text)
except wikipedia.PageNotSaved, error:
wikipedia.output(u'Page not saved: %s' % error.args)
else:
@@ -716,7 +716,7 @@
wikipedia.output(u'')
# save the page
try:
- refPage.put(text)
+ refPage.put_async(text)
except wikipedia.LockedPage:
wikipedia.output(u'Page not saved: page is locked')
except wikipedia.PageNotSaved, error:
@@ -899,7 +899,10 @@
generator = iter([page])
bot = DisambiguationRobot(always, alternatives, getAlternatives, generator, primary, main_only)
- bot.run()
+ try:
+ bot.run()
+ finally:
+ wikipedia.output(u'\n\nPlease wait for the asynchronous page edits to finish...')
if __name__ == "__main__":
try:
Modified: trunk/pywikipedia/userinterfaces/terminal_interface.py
===================================================================
--- trunk/pywikipedia/userinterfaces/terminal_interface.py 2007-08-01 23:17:32 UTC (rev 3941)
+++ trunk/pywikipedia/userinterfaces/terminal_interface.py 2007-08-01 23:31:35 UTC (rev 3942)
@@ -1,4 +1,4 @@
-
+
__version__ = '$Id$'
import config, transliteration
@@ -221,6 +221,10 @@
* jumpIndex - an integer: position at which to put the caret
* highlight - a substring; each occurence will be highlighted
"""
- import gui
+ try:
+ import gui
+ except ImportError, e:
+ print 'Could not load GUI modules: %s' % e
+ return text
editor = gui.EditBoxWindow()
return editor.edit(text, jumpIndex = jumpIndex, highlight = highlight)
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2007-08-01 23:17:32 UTC (rev 3941)
+++ trunk/pywikipedia/wikipedia.py 2007-08-01 23:31:35 UTC (rev 3942)
@@ -4454,7 +4454,8 @@
logfile = codecs.open(logfn, 'w', 'utf-8')
output_lock = threading.Lock()
-
+input_lock = threading.Lock()
+output_cache = []
def output(text, decoder = None, colors = [], newline = True, toStdout = False):
"""
Works like print, but uses the encoding used by the user's console
@@ -4489,7 +4490,10 @@
# save the text in a logfile (will be written in utf-8)
logfile.write(text + '\n')
logfile.flush()
- ui.output(text, colors = colors, newline = newline, toStdout = toStdout)
+ if input_lock.locked():
+ output_cache.append(((text,), {'colors': colors, 'newline': newline, 'toStdout': toStdout}))
+ else:
+ ui.output(text, colors = colors, newline = newline, toStdout = toStdout)
finally:
output_lock.release()
@@ -4506,7 +4510,17 @@
Returns a unicode string.
"""
- return ui.input(question, colors, password)
+ input_lock.acquire()
+ try:
+ data = ui.input(question, colors, password)
+ finally:
+ for output in output_cache:
+ ui.output(*output[0], **output[1])
+ input_lock.release()
+ for output in output_cache: #for output added between the start of the for loop and the lock release
+ ui.output(*output[0], **output[1])
+
+ return data
def inputChoice(question, answers, hotkeys, default = None):
"""