http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10423
Revision: 10423 Author: valhallasw Date: 2012-06-26 19:12:25 +0000 (Tue, 26 Jun 2012) Log Message: ----------- Terminal_interface improvements when using a dumb console
- allow output of non-errors to stdout while keeping errors in stderr - do not use getpass when stdin is not a tty (so that it will crash instead of hang when called with an emptyy stdin, such as in cron)
Modified Paths: -------------- branches/rewrite/pywikibot/bot.py branches/rewrite/pywikibot/config2.py branches/rewrite/pywikibot/userinterfaces/terminal_interface.py
Modified: branches/rewrite/pywikibot/bot.py =================================================================== --- branches/rewrite/pywikibot/bot.py 2012-06-26 19:10:02 UTC (rev 10422) +++ branches/rewrite/pywikibot/bot.py 2012-06-26 19:12:25 UTC (rev 10423) @@ -144,7 +144,7 @@ root_logger.handlers = [] # remove any old handlers
# configure handler(s) for display to user interface - ui.init_handlers(root_logger) + ui.init_handlers(root_logger, **config.userinterface_init_kwargs)
# if user has enabled file logging, configure file handler if moduleName in config.log or '*' in config.log:
Modified: branches/rewrite/pywikibot/config2.py =================================================================== --- branches/rewrite/pywikibot/config2.py 2012-06-26 19:10:02 UTC (rev 10422) +++ branches/rewrite/pywikibot/config2.py 2012-06-26 19:12:25 UTC (rev 10423) @@ -178,6 +178,11 @@ # tkinter isn't yet ready userinterface = 'terminal'
+# this can be used to pass variables to the UI init function +# useful for e.g. +# userinterface_init_kwargs = {'default_stream': 'stdout'} +userinterface_init_kwargs = {} + # i18n setting for user interface language # default is config.mylang or 'en' userinterface_lang = None
Modified: branches/rewrite/pywikibot/userinterfaces/terminal_interface.py =================================================================== --- branches/rewrite/pywikibot/userinterfaces/terminal_interface.py 2012-06-26 19:10:02 UTC (rev 10422) +++ branches/rewrite/pywikibot/userinterfaces/terminal_interface.py 2012-06-26 19:12:25 UTC (rev 10423) @@ -116,7 +116,7 @@ colorTagR = re.compile('\03{(?P<name>%s)}' % '|'.join(windowsColors.keys()))
class UI(object): - def init_handlers(self, root_logger): + def init_handlers(self, root_logger, default_stream=sys.stderr): """Initialize the handlers for user output.
This method initializes handler(s) for output levels VERBOSE (if @@ -125,8 +125,14 @@ others write theirs to sys.stderr.
""" + + if default_stream == 'stdout': + default_stream = sys.stdout + elif default_stream == 'stderr': + default_stream = sys.stderr + # default handler for display to terminal - default_handler = TerminalHandler(strm=sys.stderr) + default_handler = TerminalHandler(strm=default_stream) if config.verbose_output: default_handler.setLevel(VERBOSE) else: @@ -172,7 +178,7 @@ try: pywikibot.logoutput(question + ' ', newline=False, _level=pywikibot.INPUT) - if password: + if password and sys.stdin.isatty(): import getpass text = getpass.getpass('') # See PYWP-13 / http://bugs.python.org/issue11236
pywikipedia-svn@lists.wikimedia.org