Skip to content

Functions

src.layouts.assign_layout(filename: Path) -> Union[str, CardLayout]

Assign layout object to a card.

Parameters:

Name Type Description Default
filename Path

Path to the art file, filename supports optional tags:

required

Returns:

Type Description
str | CardLayout

Layout object for this card.

Source code in src\layouts.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def assign_layout(filename: Path) -> Union[str, 'CardLayout']:
    """Assign layout object to a card.

    Args:
        filename: Path to the art file, filename supports optional tags:
        - (artist name)
        - [set code]
        - {collector number}
        - $creator name

    Returns:
        Layout object for this card.
    """
    # Get basic card information
    card = parse_card_info(filename)
    name_failed = osp.basename(str(card.get('file', 'None')))

    # Get scryfall data for the card
    scryfall = get_card_data(card, cfg=CFG, logger=CONSOLE)
    if not scryfall:
        return msg_error(name_failed, reason="Scryfall search failed")
    scryfall = process_card_data(scryfall, card)

    # Instantiate layout object
    if scryfall.get('layout', 'None') in layout_map:
        try:
            layout = layout_map[scryfall['layout']](scryfall, card)
        except Exception as e:
            # Couldn't instantiate layout object
            CONSOLE.log_exception(e)
            return msg_error(name_failed, reason="Layout generation failed")
        if not ENV.TEST_MODE:
            CONSOLE.update(f"{msg_success('FOUND:')} {str(layout)}")
        return layout
    # Couldn't find an appropriate layout
    return msg_error(name_failed, reason="Layout incompatible")

src.layouts.join_dual_card_layouts(layouts: list[Union[str, CardLayout]])

Join any layout objects that are dual sides of the same card, i.e. Split cards.

Parameters:

Name Type Description Default
layouts list[str | CardLayout]

List of layout objects (or strings which are skipped).

required

Returns:

Type Description

List of layouts, with split layouts joined.

Source code in src\layouts.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def join_dual_card_layouts(layouts: list[Union[str, 'CardLayout']]):
    """Join any layout objects that are dual sides of the same card, i.e. Split cards.

    Args:
        layouts: List of layout objects (or strings which are skipped).

    Returns:
        List of layouts, with split layouts joined.
    """
    # Check if we have any split cards
    normal: list[Union[str, CardLayout]] = [
        n for n in layouts
        if isinstance(n, str)
        or n.card_class != LayoutType.Split]
    split: list[SplitLayout] = [
        n for n in layouts
        if not isinstance(n, str)
        and n.card_class == LayoutType.Split]
    if not split:
        return normal

    # Join any identical split cards
    skip, add = [], []
    for card in split:
        if card in skip:
            continue
        for c in split:
            if c == card:
                continue
            if str(c) == str(card):
                # Order them according to name position
                card.art_file = [*card.art_file, *c.art_file] if (
                        normalize_str(card.name[0]) == normalize_str(card.file['name'])
                ) else [*c.art_file, *card.art_file]
                skip.extend([card, c])
        add.append(card)
    return [*normal, *add]