[shopoob] Improvements + pep8 fixes

This commit is contained in:
Kitof 2015-03-03 17:29:31 +01:00 committed by Florent Fourcot
commit 8ae6ad68aa

80
weboob/applications/shopoob/shopoob.py Executable file → Normal file
View file

@ -30,29 +30,24 @@ __all__ = ['Shopoob']
class OrdersFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'date', 'discount', 'shipping', 'tax', 'total')
MANDATORY_FIELDS = ('id', 'date', 'total')
def start_format(self, **kwargs):
self.output('%sId Date Discount Shipping Tax Total ' % ((' ' * 15) if not self.interactive else ''))
self.output('-------------%s+------------+----------+----------+----------+----------' % (('-' * 15) if not self.interactive else ''))
self.output(' Id Date Total ')
self.output('-----------------------------+------------+-----------')
def format_obj(self, obj, alias):
date = obj.date.strftime('%Y-%m-%d') if not empty(obj.date) else ''
discount = obj.discount or Decimal('0')
shipping = obj.shipping or Decimal('0')
tax = obj.tax or Decimal('0')
total = obj.total or Decimal('0')
result = u'%s %s %s %s %s %s' % (self.colored('%-28s' % obj.fullid, 'yellow'),
self.colored('%-10s' % date, 'blue'),
self.colored('%9.2f' % discount, 'green'),
self.colored('%9.2f' % shipping, 'green'),
self.colored('%9.2f' % tax, 'green'),
self.colored('%9.2f' % total, 'green'))
result = u'%s %s %s' % (self.colored('%-28s' % obj.fullid, 'yellow'),
self.colored('%-10s' % date, 'blue'),
self.colored('%9.2f' % total, 'green'))
return result
def flush(self):
self.output(u'-------------%s+------------+----------+----------+----------+----------' % (('-' * 15) if not self.interactive else ''))
self.output(u'----------------------------+------------+-----------')
class ItemsFormatter(IFormatter):
MANDATORY_FIELDS = ('label', 'url', 'price')
@ -64,14 +59,15 @@ class ItemsFormatter(IFormatter):
def format_obj(self, obj, alias):
price = obj.price or Decimal('0')
result = u'%s %s %s' % (self.colored('%-75s' % obj.label[:75], 'yellow'),
self.colored('%-43s' % obj.url, 'magenta'),
self.colored('%9.2f' % price, 'green'))
self.colored('%-43s' % obj.url, 'magenta'),
self.colored('%9.2f' % price, 'green'))
return result
def flush(self):
self.output(u'---------------------------------------------------------------------------+---------------------------------------------+----------')
class PaymentsFormatter(IFormatter):
MANDATORY_FIELDS = ('date', 'method', 'amount')
@ -83,20 +79,21 @@ class PaymentsFormatter(IFormatter):
date = obj.date.strftime('%Y-%m-%d') if not empty(obj.date) else ''
amount = obj.amount or Decimal('0')
result = u'%s %s %s' % (self.colored('%-10s' % date, 'blue'),
self.colored('%-17s' % obj.method, 'yellow'),
self.colored('%9.2f' % amount, 'green'))
self.colored('%-17s' % obj.method, 'yellow'),
self.colored('%9.2f' % amount, 'green'))
return result
def flush(self):
self.output(u'-----------+-----------------+----------')
class Shopoob(ReplApplication):
APPNAME = 'shopoob'
VERSION = '1.1'
COPYRIGHT = 'Copyright(C) 2015 Christophe Lampin'
DESCRIPTION = 'Console application to obtain details and status of e-commerce orders.'
SHORT_DESCRIPTION = "obtain details and status of e-commerce orders"
SHORT_DESCRIPTION = "Obtain details and status of e-commerce orders"
CAPS = CapShop
COLLECTION_OBJECTS = (Order, )
EXTRA_FORMATTERS = {'orders': OrdersFormatter,
@ -108,7 +105,7 @@ class Shopoob(ReplApplication):
'items': 'items',
'payments': 'payments',
'ls': 'orders',
}
}
def main(self, argv):
self.load_config()
@ -117,27 +114,46 @@ class Shopoob(ReplApplication):
@defaultcount(10)
def do_orders(self, line):
"""
orders
orders [BACKEND_NAME]
List all orders.
Get orders of a backend.
If no BACKEND_NAME given, display all orders of all backends.
"""
if len(line) > 0:
backend_name = line
else:
backend_name = None
self.do_count(str(self.options.count)) # Avoid raise of MoreResultsAvailable
l = []
for order in self.do('iter_orders', backends=backend_name):
l.append(order)
self.start_format()
for order in self.do('iter_orders'):
for order in sorted(l, self.comp_object):
self.format(order)
# Order by date DESC
def comp_object(self, obj1, obj2):
if obj1.date == obj2.date:
return 0
elif obj1.date < obj2.date:
return 1
else:
return -1
def do_items(self, id):
"""
items [ID]
Get items of orders.
If no ID given, display all details of all backends.
"""
l = []
id, backend_name = self.parse_id(id)
id, backend_name = self.parse_id(id, unique_backend=True)
if not id:
for order in self.get_object_list('iter_orders'):
l.append((order.id, order.backend))
print('Error: please give a order ID (hint: use orders command)', file=self.stderr)
return 2
else:
l.append((id, backend_name))
@ -163,7 +179,13 @@ class Shopoob(ReplApplication):
Get payments of orders.
If no ID given, display payment of all backends.
"""
self.start_format()
for payment in self.do('iter_payments', id):
self.format(payment)
id, backend_name = self.parse_id(id, unique_backend=True)
if not id:
print('Error: please give a order ID (hint: use orders command)', file=self.stderr)
return 2
self.start_format()
for payment in self.do('iter_payments', id, backends=backend_name):
self.format(payment)