def get_datastore_obj(new, is_item=False):
    if is_item:
        return WOWItem(key_name=get_item_key(new['name']),
                       name=new['name'],
                       slot=new['slot'],
                       id=new['id'])      
    else:
        return WOWSpell(key_name=get_spell_key(new['name']),
                        name=new['name'],
                        buff=new['buff'],
                        gcd=new['gcd'],
                        rank=new['rank'],
                        id=new['id'])
def parse_csv(in_csv, is_item=False):
    entity_map = {}
    for entity in csv.reader(open(in_csv)):
        if is_item:
            # Map this into a hash and save.
            new = {'name': entity[0],
                   'slot': int(entity[1]),
                   'gcd':  True if entity[2] == 'True' else False,
                   'id':   int(entity[3])}
            # For items, save both item id and name as searchable
            # keys.
            entity_map[get_item_key(new['name'])] = copy.deepcopy(new)
            entity_map[get_item_key(new['id'])] = copy.deepcopy(new)
        else:
            # Map this into a hash and save.
            new = {'name': entity[0],
                   'buff': True if entity[1] == 'True' else False,
                   'gcd':  True if entity[2] == 'True' else False,
                   'id':   int(entity[3]),
                   'rank': int(entity[4]) if len(entity[4]) > 0 else 0}
            entity_map[get_spell_key(new['name'])] = copy.deepcopy(new)
    return entity_map
    def map(self, entity):
        # Use the saved entity map to determine what to do with this
        # spell.
        #
        # If it is not in the map, delete.
        #
        # If it is present in the map and no fields are different,
        # then we do nothing.
        #
        # If it is in the map but has different data, we delete
        # the existing entity and upload a modified one.
        delete = []
        update = []

        # Get key for the entity
        if self.is_item:
            key = get_item_key(entity.name)
        else:
            key = get_spell_key(entity.name)          

        # Do we delete it?
        if key not in self.MAP:
            logger.info("  Adding key [%s] for deletion." % key)
            delete.append(entity)
        # No, do we need to update it?
        else:
            # Check fields, saving diff for report
            new = self.MAP[key]
            new_entity = None
            diff = []

            if self.is_item:
                if entity.id != new['id']:
                    diff.append([entity.id, new['id']])
                if entity.name != new['name']:
                    diff.append([entity.name, new['name']])
                if entity.slot != new['slot']:
                    diff.append([entity.slot, new['slot']])
            else:
                if entity.id != new['id']:
                    diff.append([entity.id, new['id']])
                if entity.name != new['name']:
                    diff.append([entity.name, new['name']])
                if entity.buff != new['buff']:
                    diff.append([entity.buff, new['buff']])
                if entity.gcd  != new['gcd']:
                    diff.append([entity.gcd, new['gcd']])        
                if entity.rank != new['rank']:
                    diff.append([entity.rank, new['rank']])        

            # If differences found, need to update this entity.
            if len(diff) > 0:
                logger.info("  Found difference for key [%s]: %s" % (key,
                                                                      ', '.join(map(lambda t: "%s -> %s" % (t[0], t[1]), diff))))
                new_entity = get_datastore_obj(new, is_item)
                update.append(new_entity)
                delete.append(entity)

            # Remove this entity once we've examined it so we
            # don't insert a dupe later.
            del self.MAP[key]
        return (update, delete)