Beispiel #1
0
def load_replacements():
    global card_name_replacements

    # ======================
    #   UNICODE CHARACTERS
    # ======================

    # Load unicode card names, such as Æther Flash
    utranslation_table = {
        u"Æ": u"ae",
        u"æ": u"ae",
        u"â": u"a",
        u"á": u"a",
        u"à": u"a",
        u"é": u"e",
        u"í": u"i",
        u"ö": u"o",
        u"û": u"u",
        u"ú": u"u",
    }
    unicode_names = set()
    # First pass - identify all cards with unicode characters
    for uchar in utranslation_table:
        match = u"%{}%".format(uchar)
        unicode_names |= set(
            card_name.name for card_name in DBSession.query(CardName).filter(CardName.name.like(match)).all()
        )

    # Second pass - for each unicode card name, apply all replacements at once
    utranslation_table = {ord(k): v for k, v in utranslation_table.iteritems()}
    for unicode_name in unicode_names:
        replacement_name = unicode_name.translate(utranslation_table).lower()
        card_name_replacements[replacement_name] = unicode_name

    # ======================
    #   SPLIT CARDS
    # ======================

    # Load split cards, such as Dead // Gone
    split_names = DBSession.query(CardName).filter(CardName.name.ilike("%//%")).all()
    split_names = [card_name.name for card_name in split_names]
    for split_name in split_names:
        for half in split_name.split(u"//"):
            card_name_replacements[half.strip().lower()] = split_name
Beispiel #2
0
def card_from(name, set):
    """
        name and set are both strings.  set can be None.

        if the specified set can't be found, will fall back to the most recent printing of the named card
        """
    if not name:
        raise InvalidDataException(u"Card name cannot be blank")
    name = card_name_replacements.get(name.lower(), name)

    card_name = DBSession.query(CardName).filter(CardName.name.ilike(name)).first()
    if card_name is None:
        raise InvalidDataException(u"Unknown card {}".format(name))

    possible_sets = [c.set.set for c in card_name.cards]
    best_set = resolve_set(set, possible_sets, card_name.name)
    if best_set is None:
        raise InvalidDataException(u"Unknown set {}".format(set))

    card_set = DBSession.query(CardSet).filter(CardSet.set.ilike(best_set)).first()
    card = DBSession.query(Card).filter_by(name=card_name, set=card_set).first()
    return card