def handle_set_info(new_card: Card, existing_card: Card): """For matching cards, merges the set data from the newly imported card to that of the existing card. If the sets do not match, a new set will be added to the card object. If the sets do match, the sets conditions will be merged. :param new_card: the card being imported :param existing_card: the card that has already been imported :return: updated card with adjusted set data :rtype: Card """ new_card_set = SetInfo() try: new_card_set = new_card.get_set_info()[0] except IndexError: logger.error('Error occurred with card: ' + new_card.get_name(), IndexError) if new_card_set == SetInfo(): return None no_match_found = True existing_sets = existing_card.get_set_info() existing_set: SetInfo for existing_set in existing_sets: if DataImport.do_sets_match(existing_set, new_card_set): no_match_found = False new_condition: str = DataImport.get_key_from_value(new_card_set.get_conditions(), 1) updated_conditions = DataImport.handle_conditions(existing_set.get_conditions(), new_condition) existing_set.set_conditions(updated_conditions) break if no_match_found: existing_sets.append(new_card_set) existing_card.set_set_info(existing_sets) return existing_card
def define_card_object(indexes: dict, card_info: list): """Creates card object and sets card attributes from imported information. :param indexes: indexes of imported data :type indexes: dict :param card_info: imported card data in list format :type card_info: list :return: newly created card object :rtype: Card """ card_object = Card() # Replace empty strings with None type convert = lambda i: i or None card_info = [convert(i) for i in card_info] # Align card object information if indexes['name'] is not None: card_object.set_name(card_info[indexes['name']]) if indexes['attribute'] is not None: card_object.set_attribute(card_info[indexes['attribute']]) if indexes['level'] is not None: card_object.set_level(int(card_info[indexes['level']])) if indexes['attack'] is not None: card_object.set_attack(int(card_info[indexes['attack']])) if indexes['defense'] is not None: card_object.set_defense(int(card_info[indexes['defense']])) if indexes['pass_code'] is not None: card_object.set_pass_code(card_info[indexes['pass_code']]) if indexes['description'] is not None: card_object.set_description(card_info[indexes['description']]) if indexes['race_type'] is not None: race_types: dict = DataImport.get_monster_race_and_type(card_info[indexes['race_type']]) card_object.set_race(race_types.get('race')) card_object.set_monster_type(race_types.get('monster_type')) # Align card set info with card object. There can be more than one card set per card object set_info = SetInfo() if indexes['set_number'] is not None: set_info.set_set_number(card_info[indexes['set_number']]) if indexes['edition'] is not None: set_info.set_edition(card_info[indexes['edition']]) if indexes['rarity'] is not None: set_info.set_rarity(card_info[indexes['rarity']]) # Conditions are handled a bit differently. They are stored in dictionary and are used to determine quantity new_condition = card_info[indexes['condition']] conditions = DataImport.handle_conditions(set_info.get_conditions(), new_condition) if indexes['condition'] is not None: set_info.set_conditions(conditions) card_object.append_set_info(set_info) return card_object