From c07e23cafc3c59e2cb6d23c0e3028ea145c1b170 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Wed, 3 Sep 2014 01:22:18 +0200 Subject: [PATCH] Use class attributes as much as possible for application output refs #803 --- weboob/applications/boobank/boobank.py | 13 ++- weboob/applications/boobathon/boobathon.py | 82 +++++++++---------- weboob/applications/boobcoming/boobcoming.py | 15 ++-- weboob/applications/boobill/boobill.py | 11 ++- weboob/applications/booblyrics/booblyrics.py | 3 +- weboob/applications/boobmsg/boobmsg.py | 7 +- weboob/applications/boobooks/boobooks.py | 3 +- weboob/applications/boobsize/boobsize.py | 3 +- .../applications/boobtracker/boobtracker.py | 21 +++-- weboob/applications/cineoob/cineoob.py | 41 +++++----- weboob/applications/comparoob/comparoob.py | 7 +- weboob/applications/cookboob/cookboob.py | 7 +- weboob/applications/flatboob/flatboob.py | 5 +- weboob/applications/galleroob/galleroob.py | 9 +- weboob/applications/geolooc/geolooc.py | 3 +- weboob/applications/handjoob/handjoob.py | 5 +- weboob/applications/havedate/havedate.py | 19 ++--- weboob/applications/monboob/monboob.py | 15 ++-- weboob/applications/parceloob/parceloob.py | 9 +- weboob/applications/qcineoob/subtitle.py | 3 +- weboob/applications/qcineoob/torrent.py | 3 +- weboob/applications/qcookboob/recipe.py | 3 +- weboob/applications/radioob/radioob.py | 35 ++++---- weboob/applications/suboob/suboob.py | 9 +- .../applications/translaboob/translaboob.py | 3 +- weboob/applications/traveloob/traveloob.py | 11 ++- weboob/applications/videoob/videoob.py | 33 ++++---- .../webcontentedit/webcontentedit.py | 15 ++-- weboob/applications/weboobcfg/weboobcfg.py | 21 +++-- weboob/applications/weboobcli/weboobcli.py | 3 +- .../applications/weboobdebug/weboobdebug.py | 5 +- .../applications/weboobrepos/weboobrepos.py | 5 +- .../applications/weboorrents/weboorrents.py | 11 ++- weboob/tools/application/base.py | 33 +++++--- weboob/tools/application/console.py | 17 +--- 35 files changed, 228 insertions(+), 260 deletions(-) diff --git a/weboob/applications/boobank/boobank.py b/weboob/applications/boobank/boobank.py index b656c8c291..4f88bc65c8 100644 --- a/weboob/applications/boobank/boobank.py +++ b/weboob/applications/boobank/boobank.py @@ -23,7 +23,6 @@ from dateutil.relativedelta import relativedelta from dateutil.parser import parse as parse_date from decimal import Decimal, InvalidOperation -import sys from weboob.capabilities.base import empty from weboob.capabilities.bank import CapBank, Account, Transaction @@ -328,7 +327,7 @@ def show_history(self, command, line): account = self.get_object(id, 'get_account', []) if not account: - print('Error: account "%s" not found (Hint: try the command "list")' % id, file=sys.stderr) + print('Error: account "%s" not found (Hint: try the command "list")' % id, file=self.stderr) return 2 if end_date is not None: @@ -336,7 +335,7 @@ def show_history(self, command, line): end_date = parse_date(end_date) except ValueError: print('"%s" is an incorrect date format (for example "%s")' % \ - (end_date, (datetime.date.today() - relativedelta(months=1)).strftime('%Y-%m-%d')), file=sys.stderr) + (end_date, (datetime.date.today() - relativedelta(months=1)).strftime('%Y-%m-%d')), file=self.stderr) return 3 old_count = self.options.count self.options.count = None @@ -406,7 +405,7 @@ def do_transfer(self, line): account = self.get_object(id_from, 'get_account', []) if not account: - print('Error: account %s not found' % id_from, file=sys.stderr) + print('Error: account %s not found' % id_from, file=self.stderr) return 1 if not id_to: @@ -422,13 +421,13 @@ def do_transfer(self, line): id_to, backend_name_to = self.parse_id(id_to) if account.backend != backend_name_to: - print("Transfer between different backends is not implemented", file=sys.stderr) + print("Transfer between different backends is not implemented", file=self.stderr) return 4 try: amount = Decimal(amount) except (TypeError, ValueError, InvalidOperation): - print('Error: please give a decimal amount to transfer', file=sys.stderr) + print('Error: please give a decimal amount to transfer', file=self.stderr) return 2 if self.interactive: @@ -460,7 +459,7 @@ def do_investment(self, id): """ account = self.get_object(id, 'get_account', []) if not account: - print('Error: account "%s" not found (Hint: try the command "list")' % id, file=sys.stderr) + print('Error: account "%s" not found (Hint: try the command "list")' % id, file=self.stderr) return 2 self.start_format() diff --git a/weboob/applications/boobathon/boobathon.py b/weboob/applications/boobathon/boobathon.py index 32d911e493..b5f3209f14 100644 --- a/weboob/applications/boobathon/boobathon.py +++ b/weboob/applications/boobathon/boobathon.py @@ -119,7 +119,7 @@ def load(self): elif line.startswith('h3=. '): m = re.match('h3=. Event finished. Winner is "(.*)":/users/(\d+)\!', line) if not m: - print >>sys.stderr, 'Unable to parse h3=: %s' % line + print >>self.stderr, 'Unable to parse h3=: %s' % line continue self.winner = Member(int(m.group(2)), m.group(1)) elif line.startswith('h2. '): @@ -127,7 +127,7 @@ def load(self): elif line.startswith('h3. '): m = re.match('h3. "(.*)":/users/(\d+)', line) if not m: - print >>sys.stderr, 'Unable to parse user "%s"' % line + print >>self.stderr, 'Unable to parse user "%s"' % line continue member = Member(int(m.group(2)), m.group(1)) if member.id == self.my_id: @@ -166,7 +166,7 @@ def load(self): elif line.startswith('[['): m = re.match('\[\[(\w+)\]\]\|\[\[(\w+)\]\]\|(.*)\|', line) if not m: - print >>sys.stderr, 'Unable to parse task: "%s"' % line + print >>self.stderr, 'Unable to parse task: "%s"' % line continue task = Task(m.group(1), m.group(2)) member.tasks.append(task) @@ -292,7 +292,7 @@ def __init__(self, *args, **kwargs): def main(self, argv): if len(argv) < 2: - print >>sys.stderr, 'Please give the name of the boobathon' + print >>self.stderr, 'Please give the name of the boobathon' return 1 self.event = Event(argv[1], choice(self.weboob.backend_instances.values())) @@ -414,9 +414,9 @@ def do_tasks(self, line): i = -2 while not stop: if i >= 0 and not i%2: - sys.stdout.write(' #%-2d' % (i/2)) + self.stdout.write(' #%-2d' % (i/2)) else: - sys.stdout.write(' ') + self.stdout.write(' ') if i >= 0 and i%2: # second line of task, see if we'll stop stop = True @@ -425,11 +425,11 @@ def do_tasks(self, line): # there are more tasks, don't stop now stop = False if i == -2: - sys.stdout.write(' %s%-20s%s' % (self.BOLD, mem.shortname().encode('utf-8'), self.NC)) + self.stdout.write(' %s%-20s%s' % (self.BOLD, mem.shortname().encode('utf-8'), self.NC)) elif i == -1: - sys.stdout.write(' %s%-20s%s' % (self.BOLD, '-' * len(mem.shortname()), self.NC)) + self.stdout.write(' %s%-20s%s' % (self.BOLD, '-' * len(mem.shortname()), self.NC)) elif len(mem.tasks) <= (i/2): - sys.stdout.write(' ' * (20+1)) + self.stdout.write(' ' * (20+1)) else: task = mem.tasks[i/2] if task.status == task.STATUS_DONE: @@ -446,8 +446,8 @@ def do_tasks(self, line): line = u'%s %s' % (status, task.backend) else: #2nd line line = u'%s `-%s' % (status, task.capability[3:]) - sys.stdout.write((u' %-20s' % line).encode('utf-8')) - sys.stdout.write('\n') + self.stdout.write((u' %-20s' % line).encode('utf-8')) + self.stdout.write('\n') i += 1 def complete_close(self, text, line, *ignored): @@ -471,7 +471,7 @@ def do_close(self, name): print 'Event is now closed. Winner is %s!' % self.event.winner.name return - print >>sys.stderr, '"%s" not found' % name + print >>self.stderr, '"%s" not found' % name return 3 def complete_edit(self, text, line, *ignored): @@ -486,7 +486,7 @@ def do_edit(self, line): Edit information about you or about event. """ if not line: - print >>sys.stderr, 'Syntax: edit [event | me]' + print >>self.stderr, 'Syntax: edit [event | me]' return 2 self.event.load() @@ -496,12 +496,12 @@ def do_edit(self, line): elif line == 'me': mem = self.event.get_me() if not mem: - print >>sys.stderr, 'You haven\'t joined the event.' + print >>self.stderr, 'You haven\'t joined the event.' return 1 self.edit_member(mem) self.save_event('Member edited') else: - print >>sys.stderr, 'Unable to edit "%s"' % line + print >>self.stderr, 'Unable to edit "%s"' % line return 1 def do_info(self, line): @@ -558,11 +558,11 @@ def do_join(self, line): """ self.event.load() if self.event.backend.browser.get_userid() in self.event.members: - print >>sys.stderr, 'You have already joined this event.' + print >>self.stderr, 'You have already joined this event.' return 1 if self.event.is_closed(): - print >>sys.stderr, "Boobathon is closed." + print >>self.stderr, "Boobathon is closed." return 1 m = Member(self.event.backend.browser.get_userid(), None) @@ -579,17 +579,17 @@ def do_leave(self, line): self.event.load() if self.event.currently_in_event(): - print >>sys.stderr, 'Unable to leave during the event, loser!' + print >>self.stderr, 'Unable to leave during the event, loser!' return 1 if self.event.is_closed(): - print >>sys.stderr, "Boobathon is closed." + print >>self.stderr, "Boobathon is closed." return 1 try: self.event.members.pop(self.event.backend.browser.get_userid()) except KeyError: - print >>sys.stderr, "You have not joined this event." + print >>self.stderr, "You have not joined this event." return 1 else: self.save_event('Left the event') @@ -603,23 +603,23 @@ def do_remtask(self, line): self.event.load() mem = self.event.get_me() if not mem: - print >>sys.stderr, "You have not joined this event." + print >>self.stderr, "You have not joined this event." return 1 if self.event.is_closed(): - print >>sys.stderr, "Boobathon is closed." + print >>self.stderr, "Boobathon is closed." return 1 try: task_id = int(line) except ValueError: - print >>sys.stderr, 'The task ID should be a number' + print >>self.stderr, 'The task ID should be a number' return 2 try: task = mem.tasks.pop(task_id) except IndexError: - print >>sys.stderr, 'Unable to find task #%d' % task_id + print >>self.stderr, 'Unable to find task #%d' % task_id return 1 else: print 'Removing task #%d (%s,%s).' % (task_id, task.backend, task.capability) @@ -634,24 +634,24 @@ def do_addtask(self, line): self.event.load() mem = self.event.get_me() if not mem: - print >>sys.stderr, "You have not joined this event." + print >>self.stderr, "You have not joined this event." return 1 if self.event.is_closed(): - print >>sys.stderr, "Boobathon is closed." + print >>self.stderr, "Boobathon is closed." return 1 backend, capability = self.parse_command_args(line, 2, 2) if not backend[0].isupper(): - print >>sys.stderr, 'The backend name "%s" needs to start with a capital.' % backend + print >>self.stderr, 'The backend name "%s" needs to start with a capital.' % backend return 2 if not capability.startswith('Cap') or not capability[3].isupper(): - print >>sys.stderr, '"%s" is not a proper capability name (must start with Cap).' % capability + print >>self.stderr, '"%s" is not a proper capability name (must start with Cap).' % capability return 2 for task in mem.tasks: if (task.backend,task.capability) == (backend,capability): - print >>sys.stderr, "A task already exists for that." + print >>self.stderr, "A task already exists for that." return 1 task = Task(backend, capability) @@ -668,15 +668,15 @@ def do_start(self, line): self.event.load() mem = self.event.get_me() if not mem: - print >>sys.stderr, "You have not joined this event." + print >>self.stderr, "You have not joined this event." return 1 if len(mem.tasks) == 0: - print >>sys.stderr, "You don't have any task to do." + print >>self.stderr, "You don't have any task to do." return 1 if not self.event.currently_in_event(): - print >>sys.stderr, "You can't start a task, we are not in event." + print >>self.stderr, "You can't start a task, we are not in event." return 1 if line.isdigit(): @@ -695,11 +695,11 @@ def do_start(self, line): if (i == task_id or task_id < 0) and task.status == task.STATUS_NONE: break else: - print >>sys.stderr, 'Task not found.' + print >>self.stderr, 'Task not found.' return 3 if task.status == task.STATUS_DONE: - print >>sys.stderr, 'Task is already done.' + print >>self.stderr, 'Task is already done.' return 1 task.status = task.STATUS_PROGRESS @@ -716,11 +716,11 @@ def do_done(self, line): self.event.load() mem = self.event.get_me() if not mem: - print >>sys.stderr, "You have not joined this event." + print >>self.stderr, "You have not joined this event." return 1 if self.event.is_closed(): - print >>sys.stderr, "Boobathon is closed." + print >>self.stderr, "Boobathon is closed." return 1 for i, task in enumerate(mem.tasks): @@ -733,12 +733,12 @@ def do_done(self, line): self.save_event('Task accomplished') else: task.status = task.STATUS_NONE - print >>sys.stderr, 'Oops, you are out of event. Canceling the task...' + print >>self.stderr, 'Oops, you are out of event. Canceling the task...' self.save_event('Cancel task') return 1 return - print >>sys.stderr, "There isn't any task in progress." + print >>self.stderr, "There isn't any task in progress." return 1 def do_cancel(self, line): @@ -750,11 +750,11 @@ def do_cancel(self, line): self.event.load() mem = self.event.get_me() if not mem: - print >>sys.stderr, "You have not joined this event." + print >>self.stderr, "You have not joined this event." return 1 if self.event.is_closed(): - print >>sys.stderr, "Boobathon is closed." + print >>self.stderr, "Boobathon is closed." return 1 for task in mem.tasks: @@ -764,7 +764,7 @@ def do_cancel(self, line): self.save_event('Cancel task') return - print >>sys.stderr, "There isn't any task in progress." + print >>self.stderr, "There isn't any task in progress." return 1 def load_default_backends(self): diff --git a/weboob/applications/boobcoming/boobcoming.py b/weboob/applications/boobcoming/boobcoming.py index 8a77f83192..fab530f62c 100644 --- a/weboob/applications/boobcoming/boobcoming.py +++ b/weboob/applications/boobcoming/boobcoming.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . -import sys from datetime import time, datetime from weboob.tools.date import parse_date @@ -231,7 +230,7 @@ def do_list(self, line): if line: _date = parse_date(line) if not _date: - print >>sys.stderr, 'Invalid argument: %s' % self.get_command_help('list') + print >>self.stderr, 'Invalid argument: %s' % self.get_command_help('list') return 2 date_from = datetime.combine(_date, time.min) @@ -256,13 +255,13 @@ def do_info(self, _id): """ if not _id: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) return 2 event = self.get_object(_id, 'get_event') if not event: - print >>sys.stderr, 'Upcoming event not found: %s' % _id + print >>self.stderr, 'Upcoming event not found: %s' % _id return 3 self.start_format() @@ -279,7 +278,7 @@ def do_export(self, line): Export event in ICALENDAR format """ if not line: - print >>sys.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('export') + print >>self.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('export') return 2 _file, args = self.parse_command_args(line, 2, req_n=1) @@ -307,7 +306,7 @@ def retrieve_events(self, args): event = self.get_object(_id, 'get_event') if not event: - print >>sys.stderr, 'Upcoming event not found: %s' % _id + print >>self.stderr, 'Upcoming event not found: %s' % _id return 3 l.append(event) @@ -329,7 +328,7 @@ def do_attends(self, line): ID is the identifier of the event. """ if not line: - print >>sys.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('attends') + print >>self.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('attends') return 2 args = self.parse_command_args(line, 1, req_n=1) @@ -347,7 +346,7 @@ def do_unattends(self, line): """ if not line: - print >>sys.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('unattends') + print >>self.stderr, 'This command takes at leat one argument: %s' % self.get_command_help('unattends') return 2 args = self.parse_command_args(line, 1, req_n=1) diff --git a/weboob/applications/boobill/boobill.py b/weboob/applications/boobill/boobill.py index 6cca3eb3f0..58960f1a8f 100644 --- a/weboob/applications/boobill/boobill.py +++ b/weboob/applications/boobill/boobill.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from decimal import Decimal from weboob.capabilities.bill import CapBill, Detail, Subscription @@ -87,9 +86,9 @@ def exec_method(self, id, method): self.bcall_error_handler(backend, error, backtrace) if len(more_results) > 0: - print >>sys.stderr, 'Hint: There are more results available for %s (use option -n or count command)' % (', '.join(more_results)) + print >>self.stderr, 'Hint: There are more results available for %s (use option -n or count command)' % (', '.join(more_results)) for backend in not_implemented: - print >>sys.stderr, u'Error(%s): This feature is not supported yet by this backend.' % backend.name + print >>self.stderr, u'Error(%s): This feature is not supported yet by this backend.' % backend.name def do_subscriptions(self, line): """ @@ -184,7 +183,7 @@ def do_download(self, line): id, dest = self.parse_command_args(line, 2, 1) id, backend_name = self.parse_id(id) if not id: - print >>sys.stderr, 'Error: please give a bill ID (hint: use bills command)' + print >>self.stderr, 'Error: please give a bill ID (hint: use bills command)' return 2 names = (backend_name,) if backend_name is not None else None @@ -211,7 +210,7 @@ def do_download(self, line): with open(dest, 'w') as f: f.write(buf) except IOError as e: - print >>sys.stderr, 'Unable to write bill in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write bill in "%s": %s' % (dest, e) return 1 return @@ -228,7 +227,7 @@ def download_all(self, id, names): with open(dest, 'w') as f: f.write(buf) except IOError as e: - print >>sys.stderr, 'Unable to write bill in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write bill in "%s": %s' % (dest, e) return 1 return diff --git a/weboob/applications/booblyrics/booblyrics.py b/weboob/applications/booblyrics/booblyrics.py index c0eb3e484e..12f7b1f853 100644 --- a/weboob/applications/booblyrics/booblyrics.py +++ b/weboob/applications/booblyrics/booblyrics.py @@ -19,7 +19,6 @@ -import sys from weboob.capabilities.lyrics import CapLyrics from weboob.capabilities.base import empty @@ -85,7 +84,7 @@ def do_get(self, id): songlyrics = self.get_object(id, 'get_lyrics') if not songlyrics: - print >>sys.stderr, 'Song lyrics not found: %s' % id + print >>self.stderr, 'Song lyrics not found: %s' % id return 3 self.start_format() diff --git a/weboob/applications/boobmsg/boobmsg.py b/weboob/applications/boobmsg/boobmsg.py index fbdfef6523..ebec6072bc 100644 --- a/weboob/applications/boobmsg/boobmsg.py +++ b/weboob/applications/boobmsg/boobmsg.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys import os import datetime import hashlib @@ -431,7 +430,7 @@ def do_show(self, arg): """ message = None if len(arg) == 0: - print >>sys.stderr, 'Please give a message ID.' + print >>self.stderr, 'Please give a message ID.' return 2 try: @@ -448,7 +447,7 @@ def do_show(self, arg): self.weboob.do('set_message_read', message, backends=message.backend) return else: - print >>sys.stderr, 'Message not found' + print >>self.stderr, 'Message not found' return 3 def do_profile(self, id): @@ -476,7 +475,7 @@ def do_photos(self, id): """ photo_cmd = self.config.get('photo_viewer') if photo_cmd is None: - print >>sys.stderr, "Configuration error: photo_viewer is undefined" + print >>self.stderr, "Configuration error: photo_viewer is undefined" return _id, backend_name = self.parse_id(id, unique_backend=True) diff --git a/weboob/applications/boobooks/boobooks.py b/weboob/applications/boobooks/boobooks.py index 1527ba0a0e..2aff8a6177 100644 --- a/weboob/applications/boobooks/boobooks.py +++ b/weboob/applications/boobooks/boobooks.py @@ -20,7 +20,6 @@ from weboob.capabilities.library import CapBook, Book from weboob.tools.application.repl import ReplApplication from weboob.tools.application.formatters.iformatter import PrettyFormatter -import sys __all__ = ['Boobooks'] @@ -63,7 +62,7 @@ def do_renew(self, id): id, backend_name = self.parse_id(id) if not id: - print >>sys.stderr, 'Error: please give a book ID (hint: use ls command)' + print >>self.stderr, 'Error: please give a book ID (hint: use ls command)' return 2 names = (backend_name,) if backend_name is not None else None diff --git a/weboob/applications/boobsize/boobsize.py b/weboob/applications/boobsize/boobsize.py index 005676bd8b..559678af9c 100644 --- a/weboob/applications/boobsize/boobsize.py +++ b/weboob/applications/boobsize/boobsize.py @@ -23,7 +23,6 @@ from weboob.tools.application.repl import ReplApplication from weboob.tools.application.formatters.iformatter import IFormatter -import sys __all__ = ['Boobsize'] @@ -111,7 +110,7 @@ def main(self, argv): def bcall_error_handler(self, backend, error, backtrace): if isinstance(error, SensorNotFound): msg = unicode(error) or 'Sensor not found (hint: try details command)' - print >>sys.stderr, 'Error(%s): %s' % (backend.name, msg) + print >>self.stderr, 'Error(%s): %s' % (backend.name, msg) else: return ReplApplication.bcall_error_handler(self, backend, error, backtrace) diff --git a/weboob/applications/boobtracker/boobtracker.py b/weboob/applications/boobtracker/boobtracker.py index f813bff7be..f6957f8e01 100644 --- a/weboob/applications/boobtracker/boobtracker.py +++ b/weboob/applications/boobtracker/boobtracker.py @@ -23,7 +23,6 @@ from email.Header import decode_header from email.mime.text import MIMEText from smtplib import SMTP -import sys import os import re import unicodedata @@ -157,7 +156,7 @@ def do_search(self, line): elif len(path) > 0: query.project = path[0] else: - print >>sys.stderr, 'Please enter a project name' + print >>self.stderr, 'Please enter a project name' return 1 query.author = self.options.author @@ -184,12 +183,12 @@ def do_get(self, line): Get an issue and display it. """ if not line: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('get', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('get', short=True) return 2 issue = self.get_object(line, 'get_issue') if not issue: - print >>sys.stderr, 'Issue not found: %s' % line + print >>self.stderr, 'Issue not found: %s' % line return 3 self.format(issue) @@ -227,7 +226,7 @@ def do_logtime(self, line): try: hours = float(hours) except ValueError: - print >>sys.stderr, 'Error: HOURS parameter may be a float' + print >>self.stderr, 'Error: HOURS parameter may be a float' return 1 id, backend_name = self.parse_id(id, unique_backend=True) @@ -383,16 +382,16 @@ def edit_issue(self, issue, edit=True): backend = self.weboob.get_backend(issue.backend) content = self.issue2text(issue, backend) while True: - if sys.stdin.isatty(): + if self.stdin.isatty(): content = self.acquire_input(content, {'vim': "-c 'set ft=mail'"}) m = message_from_string(content.encode('utf-8')) else: - m = message_from_file(sys.stdin) + m = message_from_file(self.stdin) try: email_to = self.text2issue(issue, m) except ValueError as e: - if not sys.stdin.isatty(): + if not self.stdin.isatty(): raise raw_input("%s -- Press Enter to continue..." % unicode(e).encode("utf-8")) continue @@ -407,7 +406,7 @@ def edit_issue(self, issue, edit=True): self.send_notification(email_to, issue) return 0 except IssueError as e: - if not sys.stdin.isatty(): + if not self.stdin.isatty(): raise raw_input("%s -- Press Enter to continue..." % unicode(e).encode("utf-8")) @@ -482,7 +481,7 @@ def do_edit(self, line): _id, key, value = self.parse_command_args(line, 3, 1) issue = self.get_object(_id, 'get_issue') if not issue: - print >>sys.stderr, 'Issue not found: %s' % _id + print >>self.stderr, 'Issue not found: %s' % _id return 3 return self.edit_issue(issue, edit=True) @@ -500,4 +499,4 @@ def do_attach(self, line): Attach a file to an issue (Not implemented yet). """ - print >>sys.stderr, 'Not implemented yet.' + print >>self.stderr, 'Not implemented yet.' diff --git a/weboob/applications/cineoob/cineoob.py b/weboob/applications/cineoob/cineoob.py index 1880264a44..c3ec55536f 100644 --- a/weboob/applications/cineoob/cineoob.py +++ b/weboob/applications/cineoob/cineoob.py @@ -19,7 +19,6 @@ -import sys from datetime import datetime from weboob.applications.weboorrents.weboorrents import TorrentInfoFormatter, TorrentListFormatter @@ -231,11 +230,11 @@ def do_movies_in_common(self, line): person1 = self.get_object(id1, 'get_person', caps=CapCinema) if not person1: - print >>sys.stderr, 'Person not found: %s' % id1 + print >>self.stderr, 'Person not found: %s' % id1 return 3 person2 = self.get_object(id2, 'get_person', caps=CapCinema) if not person2: - print >>sys.stderr, 'Person not found: %s' % id2 + print >>self.stderr, 'Person not found: %s' % id2 return 3 initial_count = self.options.count @@ -264,11 +263,11 @@ def do_persons_in_common(self, line): movie1 = self.get_object(id1, 'get_movie', caps=CapCinema) if not movie1: - print >>sys.stderr, 'Movie not found: %s' % id1 + print >>self.stderr, 'Movie not found: %s' % id1 return 3 movie2 = self.get_object(id2, 'get_movie', caps=CapCinema) if not movie2: - print >>sys.stderr, 'Movie not found: %s' % id2 + print >>self.stderr, 'Movie not found: %s' % id2 return 3 initial_count = self.options.count @@ -295,7 +294,7 @@ def do_info_movie(self, id): movie = self.get_object(id, 'get_movie', caps=CapCinema) if not movie: - print >>sys.stderr, 'Movie not found: %s' % id + print >>self.stderr, 'Movie not found: %s' % id return 3 self.start_format() @@ -310,7 +309,7 @@ def do_info_person(self, id): person = self.get_object(id, 'get_person', caps=CapCinema) if not person: - print >>sys.stderr, 'Person not found: %s' % id + print >>self.stderr, 'Person not found: %s' % id return 3 self.start_format() @@ -357,7 +356,7 @@ def do_casting(self, line): movie = self.get_object(movie_id, 'get_movie', caps=CapCinema) if not movie: - print >>sys.stderr, 'Movie not found: %s' % id + print >>self.stderr, 'Movie not found: %s' % id return 3 for backend, person in self.do('iter_movie_persons', movie.id, role, backends=movie.backend, caps=CapCinema): @@ -374,7 +373,7 @@ def do_filmography(self, line): person = self.get_object(person_id, 'get_person', caps=CapCinema) if not person: - print >>sys.stderr, 'Person not found: %s' % id + print >>self.stderr, 'Person not found: %s' % id return 3 for backend, movie in self.do('iter_person_movies', person.id, role, backends=person.backend, caps=CapCinema): @@ -388,7 +387,7 @@ def do_biography(self, person_id): """ person = self.get_object(person_id, 'get_person', ('name', 'biography'), caps=CapCinema) if not person: - print >>sys.stderr, 'Person not found: %s' % person_id + print >>self.stderr, 'Person not found: %s' % person_id return 3 self.start_format() @@ -412,7 +411,7 @@ def do_releases(self, line): movie = self.get_object(id, 'get_movie', ('original_title'), caps=CapCinema) if not movie: - print >>sys.stderr, 'Movie not found: %s' % id + print >>self.stderr, 'Movie not found: %s' % id return 3 # i would like to clarify with fillobj but how could i fill the movie AND choose the country ? @@ -420,7 +419,7 @@ def do_releases(self, line): if not empty(release): movie.all_release_dates = u'%s' % (release) else: - print >>sys.stderr, 'Movie releases not found for %s' % movie.original_title + print >>self.stderr, 'Movie releases not found for %s' % movie.original_title return 3 self.start_format() self.format(movie) @@ -441,7 +440,7 @@ def do_info_torrent(self, id): torrent = self.get_object(id, 'get_torrent', caps=CapTorrent) if not torrent: - print >>sys.stderr, 'Torrent not found: %s' % id + print >>self.stderr, 'Torrent not found: %s' % id return 3 self.start_format() @@ -479,20 +478,20 @@ def do_getfile_torrent(self, line): with open(dest, 'w') as f: f.write(buf) except IOError as e: - print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) return 1 return except CallErrors as errors: for backend, error, backtrace in errors: if isinstance(error, MagnetOnly): - print >>sys.stderr, u'Error(%s): No direct URL available, ' \ + print >>self.stderr, u'Error(%s): No direct URL available, ' \ u'please provide this magnet URL ' \ u'to your client:\n%s' % (backend, error.magnet) return 4 else: self.bcall_error_handler(backend, error, backtrace) - print >>sys.stderr, 'Torrent "%s" not found' % id + print >>self.stderr, 'Torrent "%s" not found' % id return 3 @defaultcount(10) @@ -519,7 +518,7 @@ def do_search_movie_torrent(self, id): """ movie = self.get_object(id, 'get_movie', ('original_title'), caps=CapCinema) if not movie: - print >>sys.stderr, 'Movie not found: %s' % id + print >>self.stderr, 'Movie not found: %s' % id return 3 pattern = movie.original_title @@ -548,7 +547,7 @@ def do_info_subtitle(self, id): subtitle = self.get_object(id, 'get_subtitle', caps=CapCinema) if not subtitle: - print >>sys.stderr, 'Subtitle not found: %s' % id + print >>self.stderr, 'Subtitle not found: %s' % id return 3 self.start_format() @@ -585,11 +584,11 @@ def do_getfile_subtitle(self, line): with open(dest, 'w') as f: f.write(buf) except IOError as e: - print >>sys.stderr, 'Unable to write file in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write file in "%s": %s' % (dest, e) return 1 return - print >>sys.stderr, 'Subtitle "%s" not found' % id + print >>self.stderr, 'Subtitle "%s" not found' % id return 3 @defaultcount(10) @@ -658,7 +657,7 @@ def do_search_movie_subtitle(self, line): language, id = self.parse_command_args(line, 2, 2) movie = self.get_object(id, 'get_movie', ('original_title'), caps=CapCinema) if not movie: - print >>sys.stderr, 'Movie not found: %s' % id + print >>self.stderr, 'Movie not found: %s' % id return 3 pattern = movie.original_title diff --git a/weboob/applications/comparoob/comparoob.py b/weboob/applications/comparoob/comparoob.py index 90aff3776b..a7b58c5332 100644 --- a/weboob/applications/comparoob/comparoob.py +++ b/weboob/applications/comparoob/comparoob.py @@ -20,7 +20,6 @@ -import sys from weboob.capabilities.pricecomparison import CapPriceComparison from weboob.tools.html import html2text @@ -109,7 +108,7 @@ def do_prices(self, pattern): product = None if len(products) == 0: - print >>sys.stderr, 'Error: no product found with this pattern' + print >>self.stderr, 'Error: no product found with this pattern' return 1 elif len(products) == 1: product = products[0] @@ -147,12 +146,12 @@ def do_info(self, _id): Get information about a product. """ if not _id: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) return 2 price = self.get_object(_id, 'get_price') if not price: - print >>sys.stderr, 'Price not found: %s' % _id + print >>self.stderr, 'Price not found: %s' % _id return 3 self.start_format() diff --git a/weboob/applications/cookboob/cookboob.py b/weboob/applications/cookboob/cookboob.py index 7370ebde7e..4390befada 100644 --- a/weboob/applications/cookboob/cookboob.py +++ b/weboob/applications/cookboob/cookboob.py @@ -19,7 +19,6 @@ -import sys import codecs from weboob.capabilities.recipe import CapRecipe @@ -100,7 +99,7 @@ def do_info(self, id): """ recipe = self.get_object(id, 'get_recipe') if not recipe: - print >>sys.stderr, 'Recipe not found: %s' % id + print >>self.stderr, 'Recipe not found: %s' % id return 3 self.start_format() @@ -141,10 +140,10 @@ def do_export(self, line): with codecs.open(dest, 'w', 'utf-8') as f: f.write(xmlstring) except IOError as e: - print >>sys.stderr, 'Unable to write .kreml in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write .kreml in "%s": %s' % (dest, e) return 1 return - print >>sys.stderr, 'Recipe "%s" not found' % id + print >>self.stderr, 'Recipe "%s" not found' % id return 3 @defaultcount(10) diff --git a/weboob/applications/flatboob/flatboob.py b/weboob/applications/flatboob/flatboob.py index 40aab1dc95..bd450fbcd4 100644 --- a/weboob/applications/flatboob/flatboob.py +++ b/weboob/applications/flatboob/flatboob.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from weboob.capabilities.housing import CapHousing, Query from weboob.tools.application.repl import ReplApplication, defaultcount @@ -167,12 +166,12 @@ def do_info(self, _id): Get information about a housing. """ if not _id: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) return 2 housing = self.get_object(_id, 'get_housing') if not housing: - print >>sys.stderr, 'Housing not found: %s' % _id + print >>self.stderr, 'Housing not found: %s' % _id return 3 self.start_format() diff --git a/weboob/applications/galleroob/galleroob.py b/weboob/applications/galleroob/galleroob.py index 3600f7f607..ed168cd9b9 100644 --- a/weboob/applications/galleroob/galleroob.py +++ b/weboob/applications/galleroob/galleroob.py @@ -19,7 +19,6 @@ -import sys import os from re import search, sub @@ -68,7 +67,7 @@ def do_search(self, pattern): List galleries matching a PATTERN. """ if not pattern: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('search', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('search', short=True) return 2 self.start_format(pattern=pattern) @@ -98,7 +97,7 @@ def do_download(self, line): gallery = result if not gallery: - print >>sys.stderr, 'Gallery not found: %s' % _id + print >>self.stderr, 'Gallery not found: %s' % _id return 3 backend.fillobj(gallery, ('title',)) @@ -123,7 +122,7 @@ def do_download(self, line): if img.data is None: backend.fillobj(img, ('url', 'data')) if img.data is None: - print >>sys.stderr, "Couldn't get page %d, exiting" % i + print >>self.stderr, "Couldn't get page %d, exiting" % i break ext = search(r"\.([^\.]{1,5})$", img.url) @@ -150,7 +149,7 @@ def do_info(self, line): gallery = self.get_object(_id, 'get_gallery') if not gallery: - print >>sys.stderr, 'Gallery not found: %s' % _id + print >>self.stderr, 'Gallery not found: %s' % _id return 3 self.start_format() diff --git a/weboob/applications/geolooc/geolooc.py b/weboob/applications/geolooc/geolooc.py index e363fd1aa7..22d4a69e76 100644 --- a/weboob/applications/geolooc/geolooc.py +++ b/weboob/applications/geolooc/geolooc.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from weboob.capabilities.geolocip import CapGeolocIp from weboob.tools.application.repl import ReplApplication @@ -37,7 +36,7 @@ class Geolooc(ReplApplication): def main(self, argv): if len(argv) < 2: - print >>sys.stderr, 'Syntax: %s ipaddr' % argv[0] + print >>self.stderr, 'Syntax: %s ipaddr' % argv[0] return 2 for backend, location in self.do('get_location', argv[1]): diff --git a/weboob/applications/handjoob/handjoob.py b/weboob/applications/handjoob/handjoob.py index 0bf02eeac6..d17bfff017 100644 --- a/weboob/applications/handjoob/handjoob.py +++ b/weboob/applications/handjoob/handjoob.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . -import sys from weboob.capabilities.job import CapJob from weboob.tools.application.repl import ReplApplication, defaultcount @@ -123,13 +122,13 @@ def do_info(self, _id): Get information about an advert. """ if not _id: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) return 2 job_advert = self.get_object(_id, 'get_job_advert') if not job_advert: - print >>sys.stderr, 'Job advert not found: %s' % _id + print >>self.stderr, 'Job advert not found: %s' % _id return 3 self.start_format() diff --git a/weboob/applications/havedate/havedate.py b/weboob/applications/havedate/havedate.py index 2176a883b8..0eb177cbc1 100644 --- a/weboob/applications/havedate/havedate.py +++ b/weboob/applications/havedate/havedate.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from copy import copy from weboob.core import CallErrors @@ -92,7 +91,7 @@ def do_query(self, id): def edit_optims(self, backend_names, optims_names, stop=False): if optims_names is None: - print >>sys.stderr, 'Error: missing parameters.' + print >>self.stderr, 'Error: missing parameters.' return 2 for optim_name in optims_names.split(): @@ -124,14 +123,14 @@ def edit_optims(self, backend_names, optims_names, stop=False): def optims(self, function, backend_names, optims, store=True): if optims is None: - print >>sys.stderr, 'Error: missing parameters.' + print >>self.stderr, 'Error: missing parameters.' return 2 for optim_name in optims.split(): try: if store: storage_optim = set(self.storage.get('optims', optim_name, default=[])) - sys.stdout.write('%sing %s:' % (function.capitalize(), optim_name)) + self.stdout.write('%sing %s:' % (function.capitalize(), optim_name)) for backend, optim in self.do('get_optimization', optim_name, backends=backend_names): if optim: # It's useless to start a started optim, or to stop a stopped one. @@ -144,10 +143,10 @@ def optims(self, function, backend_names, optims, store=True): self.edit_optims(backend.name, optim_name) ret = getattr(optim, function)() - sys.stdout.write(' ' + backend.name) + self.stdout.write(' ' + backend.name) if not ret: - sys.stdout.write('(failed)') - sys.stdout.flush() + self.stdout.write('(failed)') + self.stdout.flush() if store: if function == 'start' and ret: storage_optim.add(backend.name) @@ -156,7 +155,7 @@ def optims(self, function, backend_names, optims, store=True): storage_optim.remove(backend.name) except KeyError: pass - sys.stdout.write('.\n') + self.stdout.write('.\n') except CallErrors as errors: for backend, error, backtrace in errors: if isinstance(error, OptimizationNotFound): @@ -206,7 +205,7 @@ def do_optim(self, line): if backend_name == '*': backend_name = None elif backend_name is not None and not backend_name in [b.name for b in self.enabled_backends]: - print >>sys.stderr, 'Error: No such backend "%s"' % backend_name + print >>self.stderr, 'Error: No such backend "%s"' % backend_name return 1 if cmd == 'start': @@ -246,7 +245,7 @@ def do_optim(self, line): line.append((b, status)) self.format(tuple(line)) return - print >>sys.stderr, "No such command '%s'" % cmd + print >>self.stderr, "No such command '%s'" % cmd return 1 def do_events(self, line): diff --git a/weboob/applications/monboob/monboob.py b/weboob/applications/monboob/monboob.py index 99909f2012..e40af14a14 100644 --- a/weboob/applications/monboob/monboob.py +++ b/weboob/applications/monboob/monboob.py @@ -26,7 +26,6 @@ from smtpd import SMTPServer import time import re -import sys import logging import asyncore import subprocess @@ -119,7 +118,7 @@ def main(self, argv): if self.config.get('interval') < 1: raise ValueError() except ValueError: - print >>sys.stderr, 'Configuration error: interval must be an integer >0.' + print >>self.stderr, 'Configuration error: interval must be an integer >0.' return 1 try: @@ -127,7 +126,7 @@ def main(self, argv): if self.config.get('html') not in (0, 1): raise ValueError() except ValueError: - print >>sys.stderr, 'Configuration error: html must be 0 or 1.' + print >>self.stderr, 'Configuration error: html must be 0 or 1.' return 2 return ReplApplication.main(self, argv) @@ -151,7 +150,7 @@ def do_post(self, line): Pipe with a mail to post message. """ - msg = message_from_file(sys.stdin) + msg = message_from_file(self.stdin) return self.process_incoming_mail(msg) def process_incoming_mail(self, msg): @@ -190,7 +189,7 @@ def process_incoming_mail(self, msg): break if len(content) == 0: - print >>sys.stderr, 'Unable to send an empty message' + print >>self.stderr, 'Unable to send an empty message' return 1 # remove signature @@ -210,7 +209,7 @@ def process_incoming_mail(self, msg): bname, id = reply_to.split('.', 1) thread_id, parent_id = id.rsplit('.', 1) except ValueError: - print >>sys.stderr, 'In-Reply-To header might be in form ' + print >>self.stderr, 'In-Reply-To header might be in form ' return 1 # Default use the To header field to know the backend to use. @@ -220,11 +219,11 @@ def process_incoming_mail(self, msg): try: backend = self.weboob.backend_instances[bname] except KeyError: - print >>sys.stderr, 'Backend %s not found' % bname + print >>self.stderr, 'Backend %s not found' % bname return 1 if not backend.has_caps(CapMessagesPost): - print >>sys.stderr, 'The backend %s does not implement CapMessagesPost' % bname + print >>self.stderr, 'The backend %s does not implement CapMessagesPost' % bname return 1 thread = Thread(thread_id) diff --git a/weboob/applications/parceloob/parceloob.py b/weboob/applications/parceloob/parceloob.py index 52a275ae4e..eb4229ae8f 100644 --- a/weboob/applications/parceloob/parceloob.py +++ b/weboob/applications/parceloob/parceloob.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from weboob.capabilities.base import empty from weboob.capabilities.parcel import CapParcel, Parcel @@ -106,7 +105,7 @@ def do_track(self, line): """ parcel = self.get_object(line, 'get_parcel_tracking') if not parcel: - print >>sys.stderr, 'Error: the parcel "%s" is not found' % line + print >>self.stderr, 'Error: the parcel "%s" is not found' % line return 2 parcels = set(self.storage.get('tracking', default=[])) @@ -134,13 +133,13 @@ def do_untrack(self, line): if not removed: parcel = self.get_object(line, 'get_parcel_tracking') if not parcel: - print >>sys.stderr, 'Error: the parcel "%s" is not found' % line + print >>self.stderr, 'Error: the parcel "%s" is not found' % line return 2 try: parcels.remove(parcel.fullid) except KeyError: - print >>sys.stderr, "Error: parcel \"%s\" wasn't tracked" % parcel.fullid + print >>self.stderr, "Error: parcel \"%s\" wasn't tracked" % parcel.fullid return 2 self.storage.set('tracking', list(parcels)) @@ -181,7 +180,7 @@ def do_info(self, id): """ parcel = self.get_object(id, 'get_parcel_tracking', []) if not parcel: - print >>sys.stderr, 'Error: parcel not found' + print >>self.stderr, 'Error: parcel not found' return 2 self.start_format() diff --git a/weboob/applications/qcineoob/subtitle.py b/weboob/applications/qcineoob/subtitle.py index 8c55d78095..ede06b75b0 100644 --- a/weboob/applications/qcineoob/subtitle.py +++ b/weboob/applications/qcineoob/subtitle.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . -import sys from PyQt4.QtCore import Qt, SIGNAL from PyQt4.QtGui import QFrame, QFileDialog @@ -83,6 +82,6 @@ def download(self): with open(dest, 'w') as f: f.write(data) except IOError as e: - print >>sys.stderr, 'Unable to write subtitle file in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write subtitle file in "%s": %s' % (dest, e) return 1 return diff --git a/weboob/applications/qcineoob/torrent.py b/weboob/applications/qcineoob/torrent.py index f10feae274..926e13dd59 100644 --- a/weboob/applications/qcineoob/torrent.py +++ b/weboob/applications/qcineoob/torrent.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . -import sys from PyQt4.QtCore import Qt, SIGNAL from PyQt4.QtGui import QFrame, QFileDialog @@ -87,6 +86,6 @@ def download(self): with open(unicode(dest), 'w') as f: f.write(data) except IOError as e: - print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) return 1 return diff --git a/weboob/applications/qcookboob/recipe.py b/weboob/applications/qcookboob/recipe.py index f0fa3a0cb8..6fa5a29f5c 100644 --- a/weboob/applications/qcookboob/recipe.py +++ b/weboob/applications/qcookboob/recipe.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . import urllib -import sys import codecs from PyQt4.QtCore import Qt, SIGNAL @@ -108,6 +107,6 @@ def export(self): with codecs.open(dest, 'w', 'utf-8') as f: f.write(data) except IOError as e: - print >>sys.stderr, 'Unable to write Krecipe file in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write Krecipe file in "%s": %s' % (dest, e) return 1 return diff --git a/weboob/applications/radioob/radioob.py b/weboob/applications/radioob/radioob.py index eb944ecf5c..39ce7a1b40 100644 --- a/weboob/applications/radioob/radioob.py +++ b/weboob/applications/radioob/radioob.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . import subprocess -import sys import os import re import requests @@ -167,18 +166,18 @@ def do_download(self, line): _id, dest = self.parse_command_args(line, 2, 1) audio = self.get_object(_id, 'get_audio', ['url']) if not audio: - print >>sys.stderr, 'Audio file not found: %s' % _id + print >>self.stderr, 'Audio file not found: %s' % _id return 3 if not audio.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 def check_exec(executable): with open('/dev/null', 'w') as devnull: process = subprocess.Popen(['which', executable], stdout=devnull) if process.wait() != 0: - print >>sys.stderr, 'Please install "%s"' % executable + print >>self.stderr, 'Please install "%s"' % executable return False return True @@ -225,7 +224,7 @@ def do_play(self, line): """ _id, stream_id = self.parse_command_args(line, 2, 1) if not _id: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('play', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('play', short=True) return 2 try: @@ -236,14 +235,14 @@ def do_play(self, line): obj = self.retrieve_obj(_id) if obj is None: - print >>sys.stderr, 'No object matches with this id:', _id + print >>self.stderr, 'No object matches with this id:', _id return 3 if isinstance(obj, Radio): try: streams = [obj.streams[stream_id]] except IndexError: - print >>sys.stderr, 'Stream %d not found' % stream_id + print >>self.stderr, 'Stream %d not found' % stream_id return 1 elif isinstance(obj, BaseAudio): streams = [obj] @@ -252,7 +251,7 @@ def do_play(self, line): streams = obj.tracks_list if len(streams) == 0: - print >>sys.stderr, 'Radio or Audio file not found:', _id + print >>self.stderr, 'Radio or Audio file not found:', _id return 3 try: @@ -317,7 +316,7 @@ def do_playlist(self, line): """ if not line: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('playlist') + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('playlist') return 2 cmd, args = self.parse_command_args(line, 2, req_n=1) @@ -327,11 +326,11 @@ def do_playlist(self, line): audio = self.get_object(_id, 'get_audio') if not audio: - print >>sys.stderr, 'Audio file not found: %s' % _id + print >>self.stderr, 'Audio file not found: %s' % _id return 3 if not audio.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 self.PLAYLIST.append(audio) @@ -343,11 +342,11 @@ def do_playlist(self, line): audio_to_remove = self.get_object(_id, 'get_audio') if not audio_to_remove: - print >>sys.stderr, 'Audio file not found: %s' % _id + print >>self.stderr, 'Audio file not found: %s' % _id return 3 if not audio_to_remove.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 for audio in self.PLAYLIST: @@ -370,7 +369,7 @@ def do_playlist(self, line): self.cached_format(audio) else: - print >>sys.stderr, 'Playlist command only support "add", "remove", "display" and "export" arguments.' + print >>self.stderr, 'Playlist command only support "add", "remove", "display" and "export" arguments.' return 2 def complete_info(self, text, line, *ignored): @@ -385,7 +384,7 @@ def do_info(self, _id): Get information about a radio or an audio file. """ if not _id: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) return 2 obj = self.retrieve_obj(_id) @@ -396,7 +395,7 @@ def do_info(self, _id): self.set_formatter('playlist_tracks_list_info') if obj is None: - print >>sys.stderr, 'No object matches with this id:', _id + print >>self.stderr, 'No object matches with this id:', _id return 3 self.format(obj) @@ -412,7 +411,7 @@ def do_search(self, pattern=None): """ if not pattern: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('playlist') + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('playlist') return 2 cmd, args = self.parse_command_args(pattern, 2, req_n=1) @@ -447,7 +446,7 @@ def do_search(self, pattern=None): self.format(playlist) else: - print >>sys.stderr, 'Search command only supports "radio", "song", "album" and "playlist" arguments.' + print >>self.stderr, 'Search command only supports "radio", "song", "album" and "playlist" arguments.' return 2 def do_ls(self, line): diff --git a/weboob/applications/suboob/suboob.py b/weboob/applications/suboob/suboob.py index 1693b5155a..7d7975a4b0 100644 --- a/weboob/applications/suboob/suboob.py +++ b/weboob/applications/suboob/suboob.py @@ -19,7 +19,6 @@ -import sys from weboob.capabilities.subtitle import CapSubtitle from weboob.capabilities.base import empty @@ -114,7 +113,7 @@ def do_info(self, id): subtitle = self.get_object(id, 'get_subtitle') if not subtitle: - print >>sys.stderr, 'Subtitle not found: %s' % id + print >>self.stderr, 'Subtitle not found: %s' % id return 3 self.start_format() @@ -139,7 +138,7 @@ def do_download(self, line): subtitle = self.get_object(id, 'get_subtitle') if not subtitle: - print >>sys.stderr, 'Subtitle not found: %s' % id + print >>self.stderr, 'Subtitle not found: %s' % id return 3 if dest is None: @@ -151,13 +150,13 @@ def do_download(self, line): for backend, buf in self.do('get_subtitle_file', subtitle.id, backends=subtitle.backend): if buf: if dest == '-': - sys.stdout.write(buf) + self.stdout.write(buf) else: try: with open(dest, 'w') as f: f.write(buf) except IOError as e: - print >>sys.stderr, 'Unable to write file in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write file in "%s": %s' % (dest, e) return 1 else: print 'Saved to %s' % dest diff --git a/weboob/applications/translaboob/translaboob.py b/weboob/applications/translaboob/translaboob.py index 8926cb5157..de5177ce06 100644 --- a/weboob/applications/translaboob/translaboob.py +++ b/weboob/applications/translaboob/translaboob.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from weboob.capabilities.translate import CapTranslate, TranslationFail, LanguageNotSupported from weboob.tools.application.repl import ReplApplication from weboob.tools.application.formatters.iformatter import IFormatter @@ -115,5 +114,5 @@ def do_translate(self, line): for backend, translation in self.do('translate', self.LANGUAGE[lan_from], self.LANGUAGE[lan_to], text): self.format(translation) except (TranslationFail, LanguageNotSupported) as error: - print >>sys.stderr, error + print >>self.stderr, error pass diff --git a/weboob/applications/traveloob/traveloob.py b/weboob/applications/traveloob/traveloob.py index 7af9d49974..a9e0e3eeb8 100644 --- a/weboob/applications/traveloob/traveloob.py +++ b/weboob/applications/traveloob/traveloob.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys import datetime from weboob.capabilities.base import Currency, empty @@ -105,7 +104,7 @@ def do_departures(self, line): if arrival: arrival_id, backend_name2 = self.parse_id(arrival) if backend_name and backend_name2 and backend_name != backend_name2: - print >>sys.stderr, 'Departure and arrival aren\'t on the same backend' + print >>self.stderr, 'Departure and arrival aren\'t on the same backend' return 1 else: arrival_id = backend_name2 = None @@ -121,8 +120,8 @@ def do_departures(self, line): try: date = self.parse_datetime(date) except ValueError as e: - print >>sys.stderr, 'Invalid datetime value: %s' % e - print >>sys.stderr, 'Please enter a datetime in form "yyyy-mm-dd HH:MM" or "HH:MM".' + print >>self.stderr, 'Invalid datetime value: %s' % e + print >>self.stderr, 'Please enter a datetime in form "yyyy-mm-dd HH:MM" or "HH:MM".' return 1 for backend, departure in self.do('iter_station_departures', station_id, arrival_id, date, backends=backends): @@ -150,8 +149,8 @@ def do_roadmap(self, line): filters.departure_time = self.parse_datetime(self.options.departure_time) filters.arrival_time = self.parse_datetime(self.options.arrival_time) except ValueError as e: - print >>sys.stderr, 'Invalid datetime value: %s' % e - print >>sys.stderr, 'Please enter a datetime in form "yyyy-mm-dd HH:MM" or "HH:MM".' + print >>self.stderr, 'Invalid datetime value: %s' % e + print >>self.stderr, 'Please enter a datetime in form "yyyy-mm-dd HH:MM" or "HH:MM".' return 1 for backend, route in self.do('iter_roadmap', departure, arrival, filters): diff --git a/weboob/applications/videoob/videoob.py b/weboob/applications/videoob/videoob.py index 96e69648f8..6104bac255 100644 --- a/weboob/applications/videoob/videoob.py +++ b/weboob/applications/videoob/videoob.py @@ -20,7 +20,6 @@ import requests import subprocess -import sys import os from weboob.capabilities.video import CapVideo, BaseVideo @@ -77,14 +76,14 @@ def main(self, argv): def download(self, video, dest, default=None): if not video.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 def check_exec(executable): with open('/dev/null', 'w') as devnull: process = subprocess.Popen(['which', executable], stdout=devnull) if process.wait() != 0: - print >>sys.stderr, 'Please install "%s"' % executable + print >>self.stderr, 'Please install "%s"' % executable return False return True @@ -147,7 +146,7 @@ def do_download(self, line): _id, dest = self.parse_command_args(line, 2, 1) video = self.get_object(_id, 'get_video', ['url']) if not video: - print >>sys.stderr, 'Video not found: %s' % _id + print >>self.stderr, 'Video not found: %s' % _id return 3 return self.download(video, dest) @@ -164,7 +163,7 @@ def do_play(self, line): Play a video with a found player. """ if not line: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('play', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('play', short=True) return 2 ret = 0 @@ -178,10 +177,10 @@ def do_play(self, line): def play(self, video, _id): if not video: - print >>sys.stderr, 'Video not found: %s' % _id + print >>self.stderr, 'Video not found: %s' % _id return 3 if not video.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 try: player_name = self.config.get('media_player') @@ -205,14 +204,14 @@ def do_info(self, line): Get information about a video. """ if not line: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('info', short=True) return 2 self.start_format() for _id in line.split(' '): video = self.get_object(_id, 'get_video') if not video: - print >>sys.stderr, 'Video not found: %s' % _id + print >>self.stderr, 'Video not found: %s' % _id return 3 self.format(video) @@ -240,11 +239,11 @@ def do_playlist(self, line): """ if not self.interactive: - print >>sys.stderr, 'This command can be used only in interactive mode.' + print >>self.stderr, 'This command can be used only in interactive mode.' return 1 if not line: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('playlist') + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('playlist') return 2 cmd, args = self.parse_command_args(line, 2, req_n=1) @@ -254,11 +253,11 @@ def do_playlist(self, line): video = self.get_object(_id, 'get_video') if not video: - print >>sys.stderr, 'Video not found: %s' % _id + print >>self.stderr, 'Video not found: %s' % _id return 3 if not video.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 self.PLAYLIST.append(video) @@ -268,11 +267,11 @@ def do_playlist(self, line): video_to_remove = self.get_object(_id, 'get_video') if not video_to_remove: - print >>sys.stderr, 'Video not found: %s' % _id + print >>self.stderr, 'Video not found: %s' % _id return 3 if not video_to_remove.url: - print >>sys.stderr, 'Error: the direct URL is not available.' + print >>self.stderr, 'Error: the direct URL is not available.' return 4 for video in self.PLAYLIST: @@ -298,7 +297,7 @@ def do_playlist(self, line): for video in self.PLAYLIST: self.play(video, video.id) else: - print >>sys.stderr, 'Playlist command only support "add", "remove", "display", "download" and "export" arguments.' + print >>self.stderr, 'Playlist command only support "add", "remove", "display", "download" and "export" arguments.' return 2 def complete_nsfw(self, text, line, begidx, endidx): @@ -332,7 +331,7 @@ def do_search(self, pattern): Search for videos matching a PATTERN. """ if not pattern: - print >>sys.stderr, 'This command takes an argument: %s' % self.get_command_help('search', short=True) + print >>self.stderr, 'This command takes an argument: %s' % self.get_command_help('search', short=True) return 2 self.change_path([u'search']) diff --git a/weboob/applications/webcontentedit/webcontentedit.py b/weboob/applications/webcontentedit/webcontentedit.py index 2f409e2126..960ce9707f 100644 --- a/weboob/applications/webcontentedit/webcontentedit.py +++ b/weboob/applications/webcontentedit/webcontentedit.py @@ -19,7 +19,6 @@ import os -import sys import tempfile import codecs @@ -53,10 +52,10 @@ def do_edit(self, line): contents += [content for backend, content in self.do('get_content', _id, backends=backend_names) if content] if len(contents) == 0: - print >>sys.stderr, 'No contents found' + print >>self.stderr, 'No contents found' return 3 - if sys.stdin.isatty(): + if self.stdin.isatty(): paths = {} for content in contents: tmpdir = os.path.join(tempfile.gettempdir(), "weboob") @@ -91,7 +90,7 @@ def do_edit(self, line): contents.remove(content) if len(contents) == 0: - print >>sys.stderr, 'No changes. Abort.' + print >>self.stderr, 'No changes. Abort.' return 1 print 'Contents changed:\n%s' % ('\n'.join(' * %s' % content.id for content in contents)) @@ -118,7 +117,7 @@ def do_edit(self, line): # stdin is not a tty if len(contents) != 1: - print >>sys.stderr, "Multiple ids not supported with pipe" + print >>self.stderr, "Multiple ids not supported with pipe" return 2 message, minor = '', False @@ -148,7 +147,7 @@ def do_log(self, line): Display log of a page """ if not line: - print >>sys.stderr, 'Error: please give a page ID' + print >>self.stderr, 'Error: please give a page ID' return 2 _id, backend_name = self.parse_id(line) @@ -167,7 +166,7 @@ def do_get(self, line): Get page contents """ if not line: - print >>sys.stderr, 'Error: please give a page ID' + print >>self.stderr, 'Error: please give a page ID' return 2 _part_line = line.strip().split(' ') @@ -180,7 +179,7 @@ def do_get(self, line): _part_line.remove('-r') if not _part_line: - print >>sys.stderr, 'Error: please give a page ID' + print >>self.stderr, 'Error: please give a page ID' return 2 _id, backend_name = self.parse_id(" ".join(_part_line)) diff --git a/weboob/applications/weboobcfg/weboobcfg.py b/weboob/applications/weboobcfg/weboobcfg.py index 739f346f95..f18f8d4dda 100644 --- a/weboob/applications/weboobcfg/weboobcfg.py +++ b/weboob/applications/weboobcfg/weboobcfg.py @@ -19,7 +19,6 @@ import os -import sys import re from weboob.capabilities.account import CapAccount @@ -53,7 +52,7 @@ def do_add(self, line): Add a backend. """ if not line: - print >>sys.stderr, 'You must specify a module name. Hint: use the "modules" command.' + print >>self.stderr, 'You must specify a module name. Hint: use the "modules" command.' return 2 name, options = self.parse_command_args(line, 2, 1) if options: @@ -67,7 +66,7 @@ def do_add(self, line): try: key, value = option.split('=', 1) except ValueError: - print >>sys.stderr, 'Parameters have to be formatted "key=value"' + print >>self.stderr, 'Parameters have to be formatted "key=value"' return 2 params[key] = value @@ -97,16 +96,16 @@ def do_confirm(self, backend_name): try: backend = self.weboob.get_backend(backend_name) except KeyError: - print >>sys.stderr, 'Error: backend "%s" not found.' % backend_name + print >>self.stderr, 'Error: backend "%s" not found.' % backend_name return 1 if not backend.has_caps(CapAccount): - print >>sys.stderr, 'Error: backend "%s" does not support accounts management' % backend_name + print >>self.stderr, 'Error: backend "%s" does not support accounts management' % backend_name return 1 mail = self.acquire_input() if not backend.confirm_account(mail): - print >>sys.stderr, 'Error: Unable to confirm account creation' + print >>self.stderr, 'Error: Unable to confirm account creation' return 1 return 0 @@ -142,14 +141,14 @@ def do_remove(self, instance_name): Remove a backend. """ if not self.weboob.backends_config.remove_backend(instance_name): - print >>sys.stderr, 'Backend instance "%s" does not exist' % instance_name + print >>self.stderr, 'Backend instance "%s" does not exist' % instance_name return 1 def _do_toggle(self, name, state): try: bname, items = self.weboob.backends_config.get_backend(name) except KeyError: - print >>sys.stderr, 'Backend instance "%s" does not exist' % name + print >>self.stderr, 'Backend instance "%s" does not exist' % name return 1 self.weboob.backends_config.edit_backend(name, bname, {'_enabled': state}) @@ -178,7 +177,7 @@ def do_edit(self, line): try: self.edit_backend(line) except KeyError: - print >>sys.stderr, 'Error: backend "%s" not found' % line + print >>self.stderr, 'Error: backend "%s" not found' % line return 1 def do_modules(self, line): @@ -202,12 +201,12 @@ def do_info(self, line): Display information about a module. """ if not line: - print >>sys.stderr, 'You must specify a module name. Hint: use the "modules" command.' + print >>self.stderr, 'You must specify a module name. Hint: use the "modules" command.' return 2 minfo = self.weboob.repositories.get_module_info(line) if not minfo: - print >>sys.stderr, 'Module "%s" does not exist.' % line + print >>self.stderr, 'Module "%s" does not exist.' % line return 1 try: diff --git a/weboob/applications/weboobcli/weboobcli.py b/weboob/applications/weboobcli/weboobcli.py index 6e92b5a09c..3503a88d28 100644 --- a/weboob/applications/weboobcli/weboobcli.py +++ b/weboob/applications/weboobcli/weboobcli.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from weboob.tools.application.repl import ReplApplication @@ -42,7 +41,7 @@ def load_default_backends(self): def main(self, argv): if len(argv) < 3: - print >>sys.stderr, "Syntax: %s capability method [args ..]" % argv[0] + print >>self.stderr, "Syntax: %s capability method [args ..]" % argv[0] return 2 cap_s = argv[1] diff --git a/weboob/applications/weboobdebug/weboobdebug.py b/weboob/applications/weboobdebug/weboobdebug.py index ef17044448..d7b3c32009 100644 --- a/weboob/applications/weboobdebug/weboobdebug.py +++ b/weboob/applications/weboobdebug/weboobdebug.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from optparse import OptionGroup from weboob.tools.application.base import BaseApplication @@ -49,12 +48,12 @@ def main(self, argv): try: backend_name = argv[1] except IndexError: - print >>sys.stderr, 'Usage: %s BACKEND' % argv[0] + print >>self.stderr, 'Usage: %s BACKEND' % argv[0] return 1 try: backend = self.weboob.load_backends(names=[backend_name])[backend_name] except KeyError: - print >>sys.stderr, u'Unable to load backend "%s"' % backend_name + print >>self.stderr, u'Unable to load backend "%s"' % backend_name return 1 locs = dict(backend=backend, browser=backend.browser, application=self, weboob=self.weboob) diff --git a/weboob/applications/weboobrepos/weboobrepos.py b/weboob/applications/weboobrepos/weboobrepos.py index 9e3a17e202..bad843a774 100644 --- a/weboob/applications/weboobrepos/weboobrepos.py +++ b/weboob/applications/weboobrepos/weboobrepos.py @@ -24,7 +24,6 @@ import tarfile import os import shutil -import sys import subprocess from copy import copy from contextlib import closing @@ -96,8 +95,8 @@ def do_build(self, line): with open(index_file, 'r') as fp: r.parse_index(fp) except IOError as e: - print >>sys.stderr, 'Unable to open repository: %s' % e - print >>sys.stderr, 'Use the "create" command before.' + print >>self.stderr, 'Unable to open repository: %s' % e + print >>self.stderr, 'Use the "create" command before.' return 1 r.build_index(source_path, index_file) diff --git a/weboob/applications/weboorrents/weboorrents.py b/weboob/applications/weboorrents/weboorrents.py index 51fef0b575..b79ca8fa9b 100644 --- a/weboob/applications/weboorrents/weboorrents.py +++ b/weboob/applications/weboorrents/weboorrents.py @@ -18,7 +18,6 @@ # along with weboob. If not, see . -import sys from weboob.capabilities.torrent import CapTorrent, MagnetOnly from weboob.tools.application.repl import ReplApplication, defaultcount @@ -117,7 +116,7 @@ def do_info(self, id): """ torrent = self.get_object(id, 'get_torrent', ('description', 'files')) if not torrent: - print >>sys.stderr, 'Torrent not found: %s' % id + print >>self.stderr, 'Torrent not found: %s' % id return 3 self.start_format() @@ -142,7 +141,7 @@ def do_getfile(self, line): torrent = self.get_object(id, 'get_torrent', ('description', 'files')) if not torrent: - print >>sys.stderr, 'Torrent not found: %s' % id + print >>self.stderr, 'Torrent not found: %s' % id return 3 dest = self.obj_to_filename(torrent, dest, '{id}-{name}.torrent') @@ -157,20 +156,20 @@ def do_getfile(self, line): with open(dest, 'w') as f: f.write(buf) except IOError as e: - print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) + print >>self.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) return 1 return except CallErrors as errors: for backend, error, backtrace in errors: if isinstance(error, MagnetOnly): - print >>sys.stderr, u'Error(%s): No direct URL available, ' \ + print >>self.stderr, u'Error(%s): No direct URL available, ' \ u'please provide this magnet URL ' \ u'to your client:\n%s' % (backend, error.magnet) return 4 else: self.bcall_error_handler(backend, error, backtrace) - print >>sys.stderr, 'Torrent "%s" not found' % id + print >>self.stderr, 'Torrent "%s" not found' % id return 3 @defaultcount(10) diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index 5a0cc164ae..7a2ea1c278 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -23,6 +23,7 @@ import logging import optparse from optparse import OptionGroup, OptionParser +import locale import os import sys import warnings @@ -134,6 +135,7 @@ def handle_application_options(self): # ------ BaseApplication methods ------------------------------- def __init__(self, option_parser=None): + self.encoding = self.guess_encoding() self.logger = getLogger(self.APPNAME) self.weboob = self.create_weboob() if self.CONFDIR is None: @@ -165,6 +167,15 @@ def __init__(self, option_parser=None): self._parser.add_option('--shell-completion', action='store_true', help=optparse.SUPPRESS_HELP) self._is_default_count = True + def guess_encoding(self, stdio=None): + if stdio is None: + stdio = self.stdout + encoding = stdio.encoding or locale.getpreferredencoding() + # ASCII or ANSII is most likely a user mistake + if not encoding or encoding.lower() == 'ascii' or encoding.lower().startswith('ansi'): + encoding = 'UTF-8' + return encoding + def deinit(self): self.weboob.want_stop() self.weboob.deinit() @@ -296,9 +307,9 @@ def bcall_error_handler(self, backend, error, backtrace): if isinstance(error, MoreResultsAvailable): return False - print(u'Error(%s): %s' % (backend.name, error), file=sys.stderr) + print(u'Error(%s): %s' % (backend.name, error), file=self.stderr) if logging.root.level == logging.DEBUG: - print(backtrace, file=sys.stderr) + print(backtrace, file=self.stderr) else: return True @@ -323,7 +334,7 @@ def bcall_errors_handler(self, errors, debugmsg='Use --debug option to print bac ask_debug_mode = True if ask_debug_mode: - print(debugmsg, file=sys.stderr) + print(debugmsg, file=self.stderr) def parse_args(self, args): self.options, args = self._parser.parse_args(args) @@ -358,7 +369,7 @@ def parse_args(self, args): if self.options.save_responses: import tempfile responses_dirname = tempfile.mkdtemp(prefix='weboob_session_') - print('Debug data will be saved in this directory: %s' % responses_dirname, file=sys.stderr) + print('Debug data will be saved in this directory: %s' % responses_dirname, file=self.stderr) log_settings['save_responses'] = True log_settings['responses_dirname'] = responses_dirname handlers.append(self.create_logging_file_handler(os.path.join(responses_dirname, 'debug.log'))) @@ -381,8 +392,8 @@ def create_default_logger(cls): # stdout logger format = '%(asctime)s:%(levelname)s:%(name)s:' + cls.VERSION +\ ':%(filename)s:%(lineno)d:%(funcName)s %(message)s' - handler = logging.StreamHandler(sys.stdout) - handler.setFormatter(createColoredFormatter(sys.stdout, format)) + handler = logging.StreamHandler(cls.stdout) + handler.setFormatter(createColoredFormatter(cls.stdout, format)) return handler @classmethod @@ -426,12 +437,12 @@ def run(cls, args=None): cls.setup_logging(logging.INFO, [cls.create_default_logger()]) if args is None: - args = [(sys.stdin.encoding and isinstance(arg, bytes) and arg.decode(sys.stdin.encoding) or to_unicode(arg)) for arg in sys.argv] + args = [(cls.stdin.encoding and isinstance(arg, bytes) and arg.decode(cls.stdin.encoding) or to_unicode(arg)) for arg in sys.argv] try: app = cls() except BackendsConfig.WrongPermissions as e: - print(e, file=sys.stderr) + print(e, file=cls.stderr) sys.exit(1) try: @@ -439,12 +450,12 @@ def run(cls, args=None): args = app.parse_args(args) sys.exit(app.main(args)) except KeyboardInterrupt: - print('Program killed by SIGINT', file=sys.stderr) + print('Program killed by SIGINT', file=cls.stderr) sys.exit(0) except EOFError: sys.exit(0) except ConfigError as e: - print('Configuration error: %s' % e, file=sys.stderr) + print('Configuration error: %s' % e, file=cls.stderr) sys.exit(1) except CallErrors as e: try: @@ -453,7 +464,7 @@ def run(cls, args=None): pass sys.exit(1) except ResultsConditionError as e: - print('%s' % e, file=sys.stderr) + print('%s' % e, file=cls.stderr) sys.exit(1) finally: app.deinit() diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index 551961f6bc..d6eb4737b0 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -26,7 +26,6 @@ import subprocess import sys import os -import locale from weboob.capabilities import UserError from weboob.capabilities.account import CapAccount, Account, AccountRegisterError @@ -82,16 +81,6 @@ def __init__(self, option_parser=None): BaseApplication.__init__(self, option_parser) self.weboob.callbacks['login'] = self.login_cb self.enabled_backends = set() - self.encoding = self.guess_encoding() - - def guess_encoding(self, stdio=None): - if stdio is None: - stdio = self.stdout - encoding = stdio.encoding or locale.getpreferredencoding() - # ASCII or ANSII is most likely a user mistake - if not encoding or encoding.lower() == 'ascii' or encoding.lower().startswith('ansi'): - encoding = 'UTF-8' - return encoding def login_cb(self, backend_name, value): return self.ask('[%s] %s' % (backend_name, @@ -517,7 +506,7 @@ def ask(self, question, default=None, masked=None, regexp=None, choices=None, ti def acquire_input(self, content=None, editor_params=None): editor = os.getenv('EDITOR', 'vi') - if sys.stdin.isatty() and editor: + if self.stdin.isatty() and editor: from tempfile import NamedTemporaryFile with NamedTemporaryFile() as f: filename = f.name @@ -534,10 +523,10 @@ def acquire_input(self, content=None, editor_params=None): f.seek(0) text = f.read() else: - if sys.stdin.isatty(): + if self.stdin.isatty(): print('Reading content from stdin... Type ctrl-D ' \ 'from an empty line to stop.') - text = sys.stdin.read() + text = self.stdin.read() return text.decode(self.encoding) def bcall_error_handler(self, backend, error, backtrace): -- GitLab