mtgencode/encode.py

86 lines
2.4 KiB
Python
Raw Normal View History

2015-06-28 01:35:11 +00:00
import re
import random
2015-07-01 04:25:29 +00:00
import sys
2015-06-28 01:35:11 +00:00
import lib.utils as utils
from lib.cardlib import Card
import lib.jdecode as jdecode
2015-06-28 01:35:11 +00:00
valid_encoded_char = r'[abcdefghijklmnopqrstuvwxyz\'+\-*",.:;WUBRGPV/XTQ|\\&^\{\}@ \n=~%\[\]]'
2015-06-28 01:35:11 +00:00
def exclude_sets(cardset):
return cardset == 'Unglued' or cardset == 'Unhinged' or cardset == 'Celebration'
2015-06-28 01:35:11 +00:00
def exclude_types(cardtype):
return cardtype in ['conspiracy']
2015-06-28 01:35:11 +00:00
def exclude_layouts(layout):
return layout in ['token', 'plane', 'scheme', 'phenomenon', 'vanguard']
2015-06-28 01:35:11 +00:00
def compile_duplicated(jcards):
# Boring solution: only write out the first one...
card = Card(jcards[0])
if (exclude_sets(jcards[0][utils.json_field_set_name])
or exclude_layouts(jcards[0]['layout'])):
return None
for cardtype in card.types:
if exclude_types(cardtype):
return None
return card
2015-06-28 01:35:11 +00:00
def main(fname, oname = None, verbose = True):
if verbose:
print 'Opening json file: ' + fname
2015-06-28 01:35:11 +00:00
jcards = jdecode.mtg_open_json(fname, verbose)
cards = []
valid = 0
skipped = 0
invalid = 0
unparsed = 0
for jcard_name in jcards:
card = compile_duplicated(jcards[jcard_name])
if card:
if card.valid:
valid += 1
cards += [card]
elif card.parsed:
invalid += 1
else:
unparsed += 1
2015-06-28 01:35:11 +00:00
else:
skipped += 1
2015-06-28 01:35:11 +00:00
if verbose:
print (str(valid) + ' valid, ' + str(skipped) + ' skipped, '
+ str(invalid) + ' invalid, ' + str(unparsed) + ' failed to parse.')
2015-06-28 01:35:11 +00:00
# This should give a random but consistent ordering, to make comparing changes
# between the output of different versions easier.
random.seed(1371367)
random.shuffle(cards)
2015-06-28 01:35:11 +00:00
if oname:
2015-06-28 01:35:11 +00:00
if verbose:
print 'Writing output to: ' + oname
with open(oname, 'w') as ofile:
for card in cards:
ofile.write(card.encode() + utils.cardsep)
else:
for card in cards:
sys.stdout.write(card.encode() + utils.cardsep)
sts.stdout.flush()
2015-06-28 01:35:11 +00:00
if __name__ == '__main__':
import sys
if len(sys.argv) == 2:
main(sys.argv[1])
elif len(sys.argv) == 3:
main(sys.argv[1], oname = sys.argv[2])
else:
print 'Usage: ' + sys.argv[0] + ' ' + '<JSON file> [output filename]'
exit(1)