#!/usr/bin/env python """Verda Steleto's Shopping List: A Chron X Desiderata Generator Compare two deck files, a target and a collection. Make a list of cards the collection lacks to make the target, in deck file format. To produce a deck file in Chron X deck editor, 'Export to File...' from a context menu. Usage: desiderata.py [wants file] [haves file] @version: 1.0 @since: 2005-12-31 @author: Robin Lionheart @license: http://creativecommons.org/licenses/by-sa/2.5/ """ DEFAULT_WANTS_FILE = "all.txt" DEFAULT_HAVES_FILE = "collection.txt" import sys def desiderata(wants_filename, haves_filename): """Compare cards we want to cards we have. List cards we don't have. @param wants_filename: name of deck file we wish to make @param haves_filename: name of deck file of cards we have @return: string listing cards we lack, in deck file format """ cards_in_order = [] amounts_to_get = {} # Read file listing cards we want, # remembering order of cards and how many we want of each try: wants_file = file(wants_filename) except (IOError, OSError): sys.exit("Could not open deck file %s\n" % (wants_filename)) for line in wants_file: quantity, card_name = line.rstrip().split('\t',2) cards_in_order.append(card_name) amounts_to_get[card_name] = int(quantity) # Chron X deck files are a tab separated list of quantities and cards: # [quantity][card name] # ex. "4 Zapp" wants_file.close() # Read file of cards we have, # subtracting what we have from what we want try: haves_file = file(haves_filename) except (IOError, OSError): sys.exit("Could not open deck file %s\n" % (haves_filename)) for line in haves_file: quantity, card_name = line.rstrip().split('\t',2) if card_name in amounts_to_get: amounts_to_get[card_name] -= int(quantity) haves_file.close() # Return list of cards we want but do not have, # in string formatted to write to a deck file. # List cards in same order as target file. return "\n".join(["%d\t%s" % (amounts_to_get[card_name], card_name) for card_name in cards_in_order if amounts_to_get[card_name] > 0]) # Execute if run as program, not imported as module if __name__ == "__main__": print desiderata(len(sys.argv)>1 and sys.argv[1] or DEFAULT_WANTS_FILE, len(sys.argv)>2 and sys.argv[2] or DEFAULT_HAVES_FILE)