-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.py
111 lines (90 loc) · 2.85 KB
/
convert.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
37
38
39
40
41
42
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
79
80
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
#!/usr/bin/env python3
import csv
from dataclasses import dataclass
from os import PathLike
from os.path import exists
from collections import defaultdict
from pathlib import Path
condition_map = defaultdict(
lambda: '',
Mint="M",
NearMint="NM",
Excellent="NM",
Good="LP",
LightPlayed="MP",
Played="HP",
Poor="D"
)
moxfield_headers = [
"Count",
"Name",
"Edition",
"Condition",
"Language",
"Foil",
"Collector Number"
]
@dataclass(frozen=True, slots=True)
class CardData:
quantity: str
trade_quantity: str
name: str
set_code: str
set_name: str
collector_num: str
condition: str
foil: str
language: str
def get_output_dict(self) -> dict[str, str]:
return {
moxfield_headers[0]: self.quantity,
moxfield_headers[1]: self.name,
moxfield_headers[2]: self.set_code,
moxfield_headers[3]: self.condition,
moxfield_headers[4]: self.language,
moxfield_headers[5]: self.foil,
moxfield_headers[6]: self.collector_num
}
def generate_cards(csv_path: PathLike) -> list[CardData]:
retval: list[CardData] = []
with open(csv_path, 'r') as csv_file:
# The first line is a seperator definition
seperator = csv_file.readline().split('=')[1].strip('"\n')
csv_reader = csv.DictReader(csv_file, delimiter=seperator)
data_row: dict
for data_row in csv_reader:
# Dragon Shield adds a junk data row at the end
if data_row['Quantity'] == '':
continue
printing = data_row['Printing'].lower()
if printing not in ('etched', 'foil'):
printing = ''
card = CardData(
data_row['Quantity'],
data_row['Trade Quantity'],
data_row['Card Name'],
data_row['Set Code'],
data_row['Set Name'],
data_row['Card Number'],
condition_map[data_row['Condition']],
printing,
data_row['Language'],
)
retval.append(card)
return retval
def convert(in_path: PathLike, out_path: PathLike) -> None:
if not exists(in_path):
raise FileNotFoundError(
f'{in_path} not found. Place file in root folder.')
card_data = generate_cards(in_path)
with open(out_path, 'w', newline='') as out_file:
writer = csv.DictWriter(out_file,
fieldnames=moxfield_headers,
dialect=csv.unix_dialect)
writer.writeheader()
for card in card_data:
writer.writerow(card.get_output_dict())
if __name__ == "__main__":
input_path = Path("cards.csv")
output_path = Path("moxfield.csv")
convert(input_path, output_path)