From d4c4fde6bcf08a29fc90dee90bba24cb0de1fdab Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Sat, 30 Nov 2013 15:11:20 -0500 Subject: [PATCH 1/4] adding item to list includes quantity if configd --- scan.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/scan.py b/scan.py index ec22c86..19c7327 100644 --- a/scan.py +++ b/scan.py @@ -118,7 +118,48 @@ def match_description_rule(trello_db, desc): return r return None - +def matching_card(item, cards): + """Return first card whose name matches item. + >>> cards = [{'name': 'random', 'id':1}, {'name': 'test', 'id': 2}, {'name': 'test', 'id': 3}] + >>> matching_card('test', cards) + {'name': 'test', 'id': 2} + """ + card = None + matches = filter(lambda c: c['name'] == item, cards) + if len(matches) > 0: + card = matches[0] + return card + +def add_quantity(card_name, qty): + """ Return name with quantity. + >>> add_quantity('name', 2) + 'name (2)' + """ + return card_name + "(%i)" % qty + +def remove_quantity(item): + """Return name without quantity. + >>> remove_quantity('name (2)') + 'name' + """ + return card_name.split(' (')[0] + +def item_quantity(card_name): + """Return quantity in card_name. + >>> item_quantity('name (2)') + 2 + >>> item_quantity('name') + 0 + """ + qty = 0 + if card_name.find(' ('): + qty = int(card_name.split('(')[1].split(')')[0]) + return qty + +def add_item_to_list(item): + print "Adding '{0}' to grocery list".format(item) + trello_api.lists.new_card(grocery_list['id'], item) + def add_grocery_item(trello_api, item): """Adds the given item to the grocery list (if it's not already present).""" # Get the current grocery list @@ -126,15 +167,26 @@ def add_grocery_item(trello_api, item): all_lists = trello_api.boards.get_list(grocery_board_id) grocery_list = [x for x in all_lists if x['name'] == conf.get()['trello_grocery_list']][0] cards = trello_api.lists.get_card(grocery_list['id']) - card_names = [card['name'] for card in cards] - - # Add item if it's not there already - if item not in card_names: - print "Adding '{0}' to grocery list".format(item) - trello_api.lists.new_card(grocery_list['id'], item) + card_names = [remove_quantity(card['name']) for card in cards] + + if bool(conf.get()['track_item_quantity']): + # Include item quantity in item name if oscar is configd + matching_card = matching_card(item, cards) + if matching_card is not None: + # Update existing item's quantity + qty = item_quantity(matching_card['name']) + name_w_qty = add_quantity(item, qty + 1) + matching_card.update_name(name_w_quantity) + else: + # Add new item with qty 1 + name_w_qty = add_quantity(item, 1) + add_item_to_list(name_w_quantity) else: - print "Item '{0}' is already on the grocery list; not adding".format(item) - + # Add item if it's not there already + if item not in card_names: + add_item_to_list(item) + else: + print "Item '{0}' is already on the grocery list; not adding".format(item) trello_api = trello.TrelloApi(conf.get()['trello_app_key']) trello_api.set_token(conf.get()['trello_token']) From 4a234706c60930f5ab5f58798edd31073b19d5aa Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Sat, 30 Nov 2013 15:11:35 -0500 Subject: [PATCH 2/4] install script asks user if quantities should be tracked --- install.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/install.py b/install.py index 1c63273..d13a2cf 100755 --- a/install.py +++ b/install.py @@ -29,6 +29,16 @@ def run_command(command): sys.exit(1) +####################################### Quantity tracking +print "Would you like oscar to track quantities of scanned items?" +print "If yes, every time an item is scanned Trello will increase a quantity displayed next to the item's name." +track_item_quantity = raw_input('y/n: ') +while track_item_quantity not in ['y', 'n']: + track_item_quantity = raw_input("Please input 'y' or 'n': ") +# Convert to boolean +track_item_quantity = {'y': True, 'n': False}[track_item_quantity] + + ######################################## Digit-Eyes print "You need accounts with a few APIs to use Oscar. First of all," print "go to" @@ -201,6 +211,8 @@ def run_command(command): digiteyes_app_key: '{digiteyes_app_key}' digiteyes_auth_key: '{digiteyes_auth_key}' + +track_item_quantity: {track_item_quantity} '''.format(**locals())) oscar_yaml.close() From 7549fefa96b07b2320f39627d8ea269d57c6a410 Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Sat, 30 Nov 2013 17:44:50 -0500 Subject: [PATCH 3/4] Added space between name and qty --- scan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scan.py b/scan.py index 19c7327..4d9f2fd 100644 --- a/scan.py +++ b/scan.py @@ -135,7 +135,7 @@ def add_quantity(card_name, qty): >>> add_quantity('name', 2) 'name (2)' """ - return card_name + "(%i)" % qty + return card_name + " (%i)" % qty def remove_quantity(item): """Return name without quantity. From deadae34896daa9de7bf7ff110c10021cc4b5961 Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Sat, 30 Nov 2013 17:45:10 -0500 Subject: [PATCH 4/4] fixed typos --- scan.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/scan.py b/scan.py index 4d9f2fd..640d294 100644 --- a/scan.py +++ b/scan.py @@ -137,22 +137,22 @@ def add_quantity(card_name, qty): """ return card_name + " (%i)" % qty -def remove_quantity(item): +def remove_quantity(card_name): """Return name without quantity. >>> remove_quantity('name (2)') 'name' """ - return card_name.split(' (')[0] + return card_name.split(' (')[0].strip() def item_quantity(card_name): """Return quantity in card_name. >>> item_quantity('name (2)') 2 >>> item_quantity('name') - 0 + 1 """ - qty = 0 - if card_name.find(' ('): + qty = 1 + if card_name.find(' (') != -1: qty = int(card_name.split('(')[1].split(')')[0]) return qty @@ -171,16 +171,16 @@ def add_grocery_item(trello_api, item): if bool(conf.get()['track_item_quantity']): # Include item quantity in item name if oscar is configd - matching_card = matching_card(item, cards) - if matching_card is not None: + mcard = matching_card(item, cards) + if mcard is not None: # Update existing item's quantity - qty = item_quantity(matching_card['name']) + qty = item_quantity(mcard['name']) name_w_qty = add_quantity(item, qty + 1) - matching_card.update_name(name_w_quantity) + trello_api.cards.update_name(mcard['id'], name_w_qty) else: # Add new item with qty 1 name_w_qty = add_quantity(item, 1) - add_item_to_list(name_w_quantity) + add_item_to_list(name_w_qty) else: # Add item if it's not there already if item not in card_names: