63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Parse deck list text files or pasted deck lists.
|
|
Usage:
|
|
python parse_deck.py <deck_file.txt>
|
|
python parse_deck.py --stdin < decklist.txt
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
import sys
|
|
|
|
|
|
def parse_deck_text(text: str) -> dict[str, int]:
|
|
"""Parse deck list text into {card_name: count} dict."""
|
|
cards = {}
|
|
for line in text.strip().split('\n'):
|
|
line = line.strip()
|
|
if not line or line.startswith('#'):
|
|
continue
|
|
match = re.match(r'^(\d+)x?\s+(.+)$', line, re.IGNORECASE)
|
|
if match:
|
|
count = int(match.group(1))
|
|
name = match.group(2).strip()
|
|
cards[name] = count
|
|
return cards
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='Parse deck list into JSON')
|
|
parser.add_argument('input', nargs='?', help='Deck list file')
|
|
parser.add_argument('--stdin', action='store_true', help='Read from stdin')
|
|
parser.add_argument('-o', '--output', help='Output JSON file')
|
|
args = parser.parse_args()
|
|
|
|
if args.stdin:
|
|
text = sys.stdin.read()
|
|
elif args.input:
|
|
with open(args.input, 'r') as f:
|
|
text = f.read()
|
|
else:
|
|
parser.print_help()
|
|
sys.exit(1)
|
|
|
|
cards = parse_deck_text(text)
|
|
result = {
|
|
'total_cards': sum(cards.values()),
|
|
'unique_cards': len(cards),
|
|
'cards': cards
|
|
}
|
|
|
|
output = json.dumps(result, indent=2)
|
|
if args.output:
|
|
with open(args.output, 'w') as f:
|
|
f.write(output)
|
|
else:
|
|
print(output)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|