125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759 | class NormalLayout:
"""Defines unified properties for all cards and serves as the layout for any M15 style typical card."""
card_class: str = LayoutType.Normal
# Static properties
is_transform: bool = False
is_mdfc: bool = False
def __init__(self, scryfall: dict, file: dict):
# Establish core properties
self._file = file
self._scryfall = scryfall
# Cache set data and frame data
_ = self.set_data
_ = self.frame
def __str__(self):
"""String representation of the card layout object."""
return (f"{self.name}"
f"{f' [{self.set}]' if self.set else ''}"
f"{f' {{{self.collector_number_raw}}}' if self.collector_number else ''}")
"""
* Core Data
"""
@auto_prop
def file(self) -> CardDetails:
"""Dictionary containing parsed art file details."""
return self._file
@auto_prop
def scryfall(self) -> dict:
"""Card data fetched from Scryfall."""
return self._scryfall
@auto_prop_cached
def template_file(self) -> Path:
"""Template PSD file path, replaced before render process."""
return PATH.TEMPLATES / 'normal.psd'
@auto_prop_cached
def art_file(self) -> Path:
"""Path: Art image file path."""
return self.file['file']
@auto_prop_cached
def scryfall_scan(self) -> str:
"""Scryfall large image scan, if available."""
return self.card.get('image_uris', {}).get('large', '')
"""
* Set Data
"""
@auto_prop_cached
def set(self) -> str:
"""Card set code, uppercase enforced, falls back to 'MTG' if missing."""
return self.scryfall.get('set', 'MTG').upper()
@auto_prop_cached
def set_data(self) -> dict:
"""Set data from the current hexproof.io data file."""
return CON.set_data.get(self.scryfall.get('set', 'mtg').lower(), {})
@auto_prop_cached
def set_type(self) -> str:
"""str: Type of set the card was printed in, e.g. promo, draft_innovation, etc."""
return self.scryfall.get('set_type', '')
"""
* Gameplay Info
"""
@auto_prop_cached
def card(self) -> dict:
"""Main card data object to pull most relevant data from."""
for i, face in enumerate(self.scryfall.get('card_faces', [])):
# Card with multiple faces, first index is always front side
if normalize_str(face['name']) == normalize_str(self.input_name):
return face
# Treat single face cards as front
return self.scryfall
@auto_prop_cached
def first_print(self) -> dict:
"""Card data fetched from Scryfall representing the first print of this card."""
first = get_cards_oracle(self.scryfall.get('oracle_id', ''))
return first[0] if first else {}
"""
* Card Collections
"""
@auto_prop_cached
def frame_effects(self) -> list[str]:
"""Array of frame effects, e.g. nyxtouched, snow, etc."""
return self.scryfall.get('frame_effects', [])
@auto_prop_cached
def keywords(self) -> list[str]:
"""Array of keyword abilities, e.g. Flying, Haste, etc."""
return self.scryfall.get('keywords', [])
@auto_prop_cached
def promo_types(self) -> list[str]:
"""list[str]: Promo types this card matches, e.g. stamped, datestamped, etc."""
return self.scryfall.get('promo_types', [])
"""
* Text Info
"""
@auto_prop_cached
def name(self) -> str:
"""Card name, supports alternate language source."""
return self.card.get('printed_name', self.name_raw) if self.is_alt_lang else self.name_raw
@auto_prop_cached
def name_raw(self) -> str:
"""Card name, enforced English representation."""
return self.card.get('name', '')
@auto_prop_cached
def display_name(self) -> str:
"""Card name, GUI appropriate representation."""
return self.name
@auto_prop_cached
def input_name(self) -> str:
"""Card name, version provided in art file name."""
return self.file['name']
@auto_prop_cached
def mana_cost(self) -> Optional[str]:
"""Scryfall formatted card mana cost."""
return self.card.get('mana_cost', '')
@auto_prop_cached
def oracle_text(self) -> str:
"""Card rules text, supports alternate language source."""
return self.card.get('printed_text', self.oracle_text_raw) if self.is_alt_lang else self.oracle_text_raw
@auto_prop_cached
def oracle_text_raw(self) -> str:
"""Card rules text, enforced English representation."""
return self.card.get('oracle_text', '')
@auto_prop_cached
def flavor_text(self) -> str:
"""Card flavor text, alternate language version shares the same key."""
return self.card.get('flavor_text', '')
@auto_prop_cached
def rules_text(self) -> str:
"""Utility definition comprised of rules and flavor text as available."""
return (self.oracle_text or '') + (self.flavor_text or '')
@auto_prop_cached
def power(self) -> str:
"""Creature power, if provided."""
return self.card.get('power', '')
@auto_prop_cached
def toughness(self) -> str:
"""Creature toughness, if provided."""
return self.card.get('toughness', '')
"""
* Card Types
"""
@auto_prop_cached
def type_line(self) -> str:
"""Card type line, supports alternate language source."""
return self.card.get('printed_type_line', self.type_line_raw) if self.is_alt_lang else self.type_line_raw
@auto_prop_cached
def type_line_raw(self) -> str:
"""Card type line, enforced English representation."""
return self.card.get('type_line', '')
@auto_prop_cached
def types_raw(self) -> list[str]:
"""List of types extracted from the raw typeline."""
return self.type_line_raw.replace(' —', '').split(' ')
@auto_prop_cached
def types(self) -> list[str]:
"""Main cards types represented, e.g. Sorcery, Instant, Creature, etc."""
return [n for n in self.types_raw if n in CardTypes]
@auto_prop_cached
def supertypes(self) -> list[str]:
"""Supertypes represented, e.g. Basic, Legendary, Snow, etc."""
return [n for n in self.types_raw if n in CardTypesSuper]
@auto_prop_cached
def subtypes(self) -> list[str]:
"""Subtypes represented, e.g. Elf, Human, Goblin, etc."""
return [
n for n in self.types_raw
if n not in self.supertypes
and n not in self.types
]
"""
* Color Info
"""
@auto_prop_cached
def color_identity(self) -> list[str]:
"""Commander relevant color identity array, e.g. [W, U]."""
return self.card.get('color_identity', [])
@auto_prop_cached
def color_indicator(self) -> str:
"""Color indicator identity array, e.g. [W, U]."""
return get_ordered_colors(self.card.get('color_indicator', []))
"""
* Collector Info
"""
@auto_prop_cached
def symbol_code(self) -> str:
"""Code used to match a symbol to this card's set. Provided by hexproof.io."""
if CFG.symbol_force_default:
return CFG.symbol_default.upper()
code = self.set_data.get('code_symbol', 'DEFAULT').upper()
return CFG.symbol_default.upper() if code == 'DEFAULT' else code
@auto_prop_cached
def lang(self) -> str:
"""Card print language, uppercase enforced, falls back to settings defined value."""
return self.scryfall.get('lang', CFG.lang).upper()
@auto_prop_cached
def rarity(self) -> str:
"""Card rarity, interprets 'special' rarities based on card data."""
return self.rarity_raw if self.rarity_raw in [
Rarity.C, Rarity.U, Rarity.R, Rarity.M, Rarity.T
] else get_special_rarity(self.rarity_raw, self.scryfall)
@auto_prop_cached
def rarity_raw(self) -> str:
"""Card rarity, doesn't interpret 'special' rarities."""
return self.scryfall.get('rarity', Rarity.C)
@auto_prop_cached
def rarity_letter(self) -> str:
"""First letter of card rarity, uppercase enforced."""
return self.rarity[0].upper()
@auto_prop_cached
def artist(self) -> str:
"""Card artist name, prioritizes user provided artist name. Controls for duplicate last names."""
if self.file.get('artist'):
return self.file['artist']
# Check for duplicate last names
artist, count = self.card.get('artist', 'Unknown'), []
if '&' in artist:
for w in artist.split(' '):
if w in count:
count.remove(w)
count.append(w)
return ' '.join(count)
return artist
@auto_prop_cached
def collector_number(self) -> int:
"""int: Card number assigned within release set. Non-digit characters are ignored, falls back to 0."""
if self.collector_number_raw:
return int(''.join(char for char in self.collector_number_raw if char.isdigit()))
return 0
@auto_prop_cached
def collector_number_raw(self) -> Optional[str]:
"""str | None: Card number assigned within release set. Raw string representation, allows non-digits."""
return self.scryfall.get('collector_number')
@auto_prop_cached
def card_count(self) -> Optional[int]:
"""int | None: Number of cards within the card's release set. Only required in 'Normal' Collector Mode."""
# Skip if collector mode doesn't require it or if collector number is bad
if CFG.collector_mode != CollectorMode.Normal or not self.collector_number_raw:
return
# Prefer printed count, fallback to card count, skip if count isn't found
count = self.set_data.get('count_printed', self.set_data.get('count_cards'))
if count is None:
return
# Skip if count is smaller than collector number
return count if int(count) >= self.collector_number else None
@auto_prop_cached
def collector_data(self) -> str:
"""str: Formatted collector info line, e.g. 050/230 M."""
if self.card_count:
return f"{str(self.collector_number).zfill(3)}/{str(self.card_count).zfill(3)} {self.rarity_letter}"
if self.collector_number_raw:
return f"{self.rarity_letter} {str(self.collector_number).zfill(4)}"
return ''
@auto_prop_cached
def creator(self) -> str:
"""str: Optional creator string provided by user in art file name."""
return self.file.get('creator', '')
"""
* Symbols
"""
@auto_prop_cached
def symbol_svg(self) -> Optional[Path]:
"""SVG path definition for card's expansion symbol."""
# Does SVG exist?
path = (PATH.SRC_IMG_SYMBOLS / 'set' / self.symbol_code / self.rarity_letter).with_suffix('.svg')
if path.is_file():
return path
# Revert to mythic for special rarities
if self.rarity not in [Rarity.C, Rarity.U, Rarity.R, Rarity.M]:
path = (PATH.SRC_IMG_SYMBOLS / 'set' / self.symbol_code / 'M').with_suffix('.svg')
if path.is_file():
return path
# Revert to default symbol or None
path = (PATH.SRC_IMG_SYMBOLS / 'set' / 'DEFAULT' / self.rarity_letter).with_suffix('.svg')
if path.is_file():
return path
return
@auto_prop_cached
def watermark(self) -> Optional[str]:
"""Name of the card's watermark file that is actually used, if provided."""
if not self.watermark_svg:
return
if self.watermark_svg.stem.upper() == 'WM':
return self.watermark_svg.parent.stem.lower()
return self.watermark_svg.stem.lower()
@auto_prop_cached
def watermark_raw(self) -> Optional[str]:
"""Name of the card's watermark from raw Scryfall data, if provided."""
return self.card.get('watermark')
@auto_prop_cached
def watermark_svg(self) -> Optional[Path]:
"""Path to the watermark SVG file, if provided."""
def _find_watermark_svg(wm: str) -> Optional[Path]:
"""Try to find a watermark SVG asset, allowing for special cases and set code fallbacks.
Args:
wm: Watermark name or set code to look for.
Returns:
Path to a watermark SVG file if found, otherwise None.
Notes:
- 'set' maps to the symbol collection of the set this card was first printed in.
- 'symbol' maps to the symbol collection of this card object's set.
"""
if not wm:
return
wm = wm.lower()
# Special case watermarks
if wm in ['set', 'symbol']:
return get_watermark_svg_from_set(
self.first_print.get('set', self.set) if wm == 'set' else self.set)
# Look for normal watermark
return get_watermark_svg(wm)
# WatermarkMode: Disabled, Forced
if CFG.watermark_mode == WatermarkMode.Disabled:
return
elif CFG.watermark_mode == WatermarkMode.Forced:
return _find_watermark_svg(CFG.watermark_default)
# WatermarkMode: Automatic
path = _find_watermark_svg(self.watermark_raw)
if path or CFG.watermark_mode == WatermarkMode.Automatic:
return path
# WatermarkMode: Fallback
return _find_watermark_svg(CFG.watermark_default)
@auto_prop_cached
def watermark_basic(self) -> Optional[Path]:
"""Optional[Path]: Path to basic land watermark, if card is a Basic Land."""
if not self.is_basic_land:
return
# Map pinlines to basic land type
_map = {
'W': 'plains',
'U': 'island',
'B': 'swamp',
'R': 'mountain',
'G': 'forest',
'Land': 'wastes'}
if basic_type := _map.get(self.pinlines):
return (PATH.SRC_IMG_SYMBOLS / 'watermark' / basic_type).with_suffix('.svg')
return
"""
* Bool Properties
"""
@auto_prop_cached
def is_creature(self) -> bool:
"""True if card is a Creature."""
return bool(self.power and self.toughness)
@auto_prop_cached
def is_land(self) -> bool:
"""True if card is a Land."""
return 'Land' in self.type_line_raw
@auto_prop_cached
def is_basic_land(self) -> bool:
"""True if card is a Basic Land."""
return self.type_line_raw.startswith('Basic')
@auto_prop_cached
def is_legendary(self) -> bool:
"""True if card is Legendary."""
return 'Legendary' in self.type_line_raw
@auto_prop_cached
def is_colorless(self) -> bool:
"""True if card is colorless or devoid."""
return self.frame['is_colorless']
@auto_prop_cached
def is_hybrid(self) -> bool:
"""True if card is a hybrid frame."""
return self.frame['is_hybrid']
@auto_prop_cached
def is_artifact(self) -> bool:
"""True if card is an Artifact."""
return 'Artifact' in self.type_line_raw
@auto_prop_cached
def is_vehicle(self) -> bool:
"""True if card is a Vehicle."""
return 'Vehicle' in self.type_line_raw
@auto_prop_cached
def is_promo(self) -> bool:
"""True if card is a promotional print."""
if self.scryfall.get('promo', False):
return True
if self.set_type == 'promo':
return True
if self.promo_types:
return True
return False
@auto_prop_cached
def is_front(self) -> bool:
"""True if card is front face."""
return bool(self.scryfall.get('front', True))
@auto_prop_cached
def is_alt_lang(self) -> bool:
"""True if language selected isn't English."""
return bool(self.lang != 'EN')
"""
* Cosmetic Bool
"""
@auto_prop_cached
def is_token(self) -> bool:
"""bool: True if card is a Token or Emblem."""
return bool('Token' in self.type_line_raw or self.is_emblem)
@auto_prop_cached
def is_emblem(self) -> bool:
"""bool: True on card is an Emblem."""
return bool('Emblem' in self.type_line_raw)
@auto_prop_cached
def is_nyx(self) -> bool:
"""True if card has Nyx enchantment background texture."""
if 'nyxtouched' in self.frame_effects:
return True
# Nyxtouched often not provided, check for 'Enchantment Creature'
return bool(self.is_creature and 'Enchantment' in self.type_line_raw)
@auto_prop_cached
def is_companion(self) -> bool:
"""True if card is a Companion."""
return "companion" in self.frame_effects
@auto_prop_cached
def is_miracle(self) -> bool:
"""True if card is a 'Miracle' card."""
return bool("Miracle" in self.frame_effects)
@auto_prop_cached
def is_snow(self) -> bool:
"""True if card is a 'Snow' card."""
return bool('Snow' in self.type_line_raw)
"""
* Frame Details
"""
@auto_prop_cached
def frame(self) -> FrameDetails:
"""Dictionary containing calculated frame information."""
return get_frame_details(self.card)
@auto_prop_cached
def twins(self) -> str:
"""Identity of the name and title boxes."""
return self.frame['twins']
@auto_prop_cached
def pinlines(self) -> str:
"""Identity of the pinlines."""
return self.frame['pinlines']
@auto_prop_cached
def background(self) -> str:
"""Identity of the background."""
return self.frame['background']
@auto_prop_cached
def identity(self) -> str:
"""Frame appropriate color identity of the card."""
return self.frame['identity']
"""
* Opposing Face Properties
"""
@auto_prop_cached
def other_face(self) -> dict:
"""Card data from opposing face if provided."""
for face in self.scryfall.get('card_faces', []):
if face.get('name') != self.name_raw:
return face
return {}
@auto_prop_cached
def other_face_frame(self) -> Union[FrameDetails, dict]:
"""Calculated frame information of opposing face, if provided."""
return get_frame_details(self.other_face) if self.other_face else {}
@auto_prop_cached
def other_face_twins(self) -> str:
"""Name and title box identity of opposing face."""
return self.other_face_frame.get('twins', '')
@auto_prop_cached
def transform_icon(self) -> str:
"""Transform icon if provided, data possibly deprecated in modern practice."""
for effect in self.frame_effects:
if effect in TransformIcons:
return effect
# Fallback: New Transform cards use 'convert' arrow introduced in Transformer set
return TransformIcons.UPSIDEDOWN if self.is_land else TransformIcons.CONVERT
@auto_prop_cached
def other_face_mana_cost(self) -> str:
"""Mana cost of opposing face."""
return self.other_face.get('mana_cost', '')
@auto_prop_cached
def other_face_type_line(self) -> str:
"""Type line of opposing face."""
return self.other_face.get('type_line', '')
@auto_prop_cached
def other_face_type_line_raw(self) -> str:
"""Type line of opposing face, English language enforced."""
if self.is_alt_lang:
return self.other_face.get('printed_type_line', self.other_face_type_line)
return self.other_face_type_line
@auto_prop_cached
def other_face_oracle_text(self) -> str:
"""Rules text of opposing face."""
if self.is_alt_lang:
return self.other_face.get('printed_text', self.other_face_oracle_text_raw)
return self.other_face_oracle_text_raw
@auto_prop_cached
def other_face_oracle_text_raw(self) -> str:
"""Rules text of opposing face."""
return self.other_face.get('oracle_text', '')
@auto_prop_cached
def other_face_power(self) -> str:
"""Creature power of opposing face, if provided."""
return self.other_face.get('power', '')
@auto_prop_cached
def other_face_toughness(self) -> str:
"""Creature toughness of opposing face, if provided."""
return self.other_face.get('toughness', '')
@auto_prop_cached
def other_face_left(self) -> Optional[str]:
"""Abridged type of the opposing side to display on bottom MDFC bar."""
return self.other_face_type_line_raw.split(' ')[-1] if self.other_face else ''
@auto_prop_cached
def other_face_right(self) -> str:
"""Mana cost or mana ability of opposing side, depending on land or nonland."""
if not self.other_face:
return ''
# Other face is not a land
if 'Land' not in self.other_face_type_line_raw:
return self.other_face_mana_cost
# Other face is a land, find the mana tap ability
for line in self.other_face_oracle_text.split('\n'):
if line.startswith('{T}'):
return f"{line.split('.')[0]}."
return self.other_face_oracle_text
|