From e731f42cb9c2a566b504882edea92e30099ae6c0 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Mon, 11 Jul 2011 22:18:04 +0200 Subject: [PATCH] new method ConsoleApplication.acquire_input() to read stdin friendly --- weboob/applications/boobmsg/boobmsg.py | 7 +------ weboob/applications/pastoob/pastoob.py | 2 +- weboob/applications/weboobcfg/weboobcfg.py | 2 +- weboob/tools/application/console.py | 10 +++++++++- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/weboob/applications/boobmsg/boobmsg.py b/weboob/applications/boobmsg/boobmsg.py index e765420c..db5c758d 100644 --- a/weboob/applications/boobmsg/boobmsg.py +++ b/weboob/applications/boobmsg/boobmsg.py @@ -230,12 +230,7 @@ class Boobmsg(ReplApplication): """ receivers, text = self.parse_command_args(line, 2, 1) if text is None: - if self.interactive: - print 'Reading message content from stdin... Type ctrl-D ' \ - 'from an empty line to post message.' - text = sys.stdin.read() - if sys.stdin.encoding: - text = text.decode(sys.stdin.encoding) + text = self.acquire_input() if self.options.skip_empty and not text.strip(): return diff --git a/weboob/applications/pastoob/pastoob.py b/weboob/applications/pastoob/pastoob.py index 121ecb34..9fb1b5b8 100644 --- a/weboob/applications/pastoob/pastoob.py +++ b/weboob/applications/pastoob/pastoob.py @@ -77,7 +77,7 @@ class Pastoob(ReplApplication): The filename can be '-' for reading standard input (pipe). """ if not filename or filename == '-': - contents = sys.stdin.read().decode(sys.stdin.encoding or locale.getpreferredencoding()) + contents = self.acquire_input() else: try: with codecs.open(filename, encoding=locale.getpreferredencoding()) as fp: diff --git a/weboob/applications/weboobcfg/weboobcfg.py b/weboob/applications/weboobcfg/weboobcfg.py index abac8637..a86788d7 100644 --- a/weboob/applications/weboobcfg/weboobcfg.py +++ b/weboob/applications/weboobcfg/weboobcfg.py @@ -107,7 +107,7 @@ class WeboobCfg(ReplApplication): print >>sys.stderr, 'Error: backend "%s" does not support accounts management' % backend_name return 1 - mail = sys.stdin.read() + mail = self.acquire_input() if not backend.confirm_account(mail): print >>sys.stderr, 'Error: Unable to confirm account creation' return 1 diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index 59b80fee..f7f8a923 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -23,6 +23,7 @@ import getpass import logging import sys import os +import locale from weboob.capabilities.account import ICapAccount, Account, AccountRegisterError from weboob.core.backendscfg import BackendAlreadyExists @@ -186,7 +187,7 @@ class ConsoleApplication(BaseApplication): backends = [(b.name, b) for b in self.enabled_backends] if unique_backend and not backend_name: if len(backends) == 1: - backend_name = backends[0] + backend_name = backends[0][0] else: raise BackendNotGiven(_id, backends) if backend_name is not None and not backend_name in dict(backends): @@ -413,6 +414,13 @@ class ConsoleApplication(BaseApplication): return v.get() + def acquire_input(self): + if sys.stdin.isatty(): + print 'Reading content from stdin... Type ctrl-D ' \ + 'from an empty line to stop.' + text = sys.stdin.read() + return text.decode(sys.stdin.encoding or locale.getpreferredencoding()) + def bcall_error_handler(self, backend, error, backtrace): """ Handler for an exception inside the CallErrors exception.