Commit c07e23ca authored by Laurent Bachelier's avatar Laurent Bachelier 🐧
Browse files

Use class attributes as much as possible for application output

refs #803
parent ee8e7cee
......@@ -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()
......
......@@ -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):
......
......@@ -17,7 +17,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
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)
......
......@@ -18,7 +18,6 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
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
......@@ -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()
......
......@@ -18,7 +18,6 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
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)
......
......@@ -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
......
......@@ -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)
......
......@@ -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: