-
Notifications
You must be signed in to change notification settings - Fork 4
/
deckread.py
36 lines (28 loc) · 950 Bytes
/
deckread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from os.path import exists as __exists, join as __join
from commands.typing import CommandReturn, DownloadCard
deck_folder_path = "./deck/"
def __filter_card_id(cards: list[str]) -> list[int]:
"""Filters an list with card ids to remove
repeating ones and non-ids"""
ids: list[int] = list()
for c in cards:
try:
c = int(c)
if c not in ids:
ids.append(int(c))
except ValueError:
continue
return ids
def get_deck(deck_name: str) -> CommandReturn:
"""Reads a deck file and returns the
ids of the cards in it"""
deck_path = __join(deck_folder_path, f"{deck_name}.ydk")
deck_exists = __exists(deck_path)
if not deck_exists:
return None
deck = open(deck_path, mode="r", encoding="utf8")
cards = __filter_card_id([l.strip() for l in deck.readlines()])
return [
DownloadCard(c, False)
for c in cards
]