2015-07-16 06:40:15 +00:00
|
|
|
#!/usr/bin/env python
|
2015-07-01 04:25:29 +00:00
|
|
|
import sys
|
2015-07-16 06:40:15 +00:00
|
|
|
import os
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-16 06:40:15 +00:00
|
|
|
libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')
|
|
|
|
sys.path.append(libdir)
|
|
|
|
import re
|
|
|
|
import random
|
|
|
|
import utils
|
|
|
|
import jdecode
|
|
|
|
import cardlib
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-14 07:07:25 +00:00
|
|
|
def exclude_sets(cardset):
|
|
|
|
return cardset == 'Unglued' or cardset == 'Unhinged' or cardset == 'Celebration'
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-14 07:07:25 +00:00
|
|
|
def exclude_types(cardtype):
|
|
|
|
return cardtype in ['conspiracy']
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-14 07:07:25 +00:00
|
|
|
def exclude_layouts(layout):
|
|
|
|
return layout in ['token', 'plane', 'scheme', 'phenomenon', 'vanguard']
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-09-07 03:24:19 +00:00
|
|
|
def main(fname, oname = None, verbose = True, encoding = 'std',
|
|
|
|
nolinetrans = False, randomize = False, nolabel = False, stable = False):
|
2015-07-16 06:40:15 +00:00
|
|
|
fmt_ordered = cardlib.fmt_ordered_default
|
2015-09-07 03:24:19 +00:00
|
|
|
fmt_labeled = None if nolabel else cardlib.fmt_labeled_default
|
2015-07-16 06:40:15 +00:00
|
|
|
fieldsep = utils.fieldsep
|
2015-09-07 03:24:19 +00:00
|
|
|
line_transformations = not nolinetrans
|
2015-07-16 06:40:15 +00:00
|
|
|
randomize_fields = False
|
2015-09-07 03:24:19 +00:00
|
|
|
randomize_mana = randomize
|
2015-07-16 06:40:15 +00:00
|
|
|
initial_sep = True
|
|
|
|
final_sep = True
|
|
|
|
|
|
|
|
# set the properties of the encoding
|
2015-09-07 03:24:19 +00:00
|
|
|
|
|
|
|
if encoding in ['std']:
|
2015-07-29 08:21:34 +00:00
|
|
|
pass
|
2015-09-07 03:24:19 +00:00
|
|
|
elif encoding in ['named']:
|
|
|
|
fmt_ordered = cardlib.fmt_ordered_named
|
|
|
|
elif encoding in ['noname']:
|
|
|
|
fmt_ordered = cardlib.fmt_ordered_noname
|
2015-07-16 06:40:15 +00:00
|
|
|
elif encoding in ['rfields']:
|
|
|
|
randomize_fields = True
|
|
|
|
final_sep = False
|
2015-09-07 03:24:19 +00:00
|
|
|
elif encoding in ['old']:
|
|
|
|
fmt_ordered = cardlib.fmt_ordered_old
|
|
|
|
elif encoding in ['norarity']:
|
|
|
|
fmt_ordered = cardlib.fmt_ordered_norarity
|
|
|
|
elif encoding in ['vec']:
|
|
|
|
pass
|
|
|
|
elif encoding in ['custom']:
|
|
|
|
## put custom format decisions here ##########################
|
|
|
|
|
|
|
|
## end of custom format ######################################
|
|
|
|
pass
|
2015-07-16 06:40:15 +00:00
|
|
|
else:
|
|
|
|
raise ValueError('encode.py: unknown encoding: ' + encoding)
|
|
|
|
|
2015-07-14 07:07:25 +00:00
|
|
|
if verbose:
|
2015-07-16 06:40:15 +00:00
|
|
|
print 'Preparing to encode:'
|
|
|
|
print ' Using encoding ' + repr(encoding)
|
|
|
|
if stable:
|
|
|
|
print ' NOT randomizing order of cards.'
|
2015-09-07 03:24:19 +00:00
|
|
|
if randomize_mana:
|
|
|
|
print ' Randomizing order of symobls in manacosts.'
|
|
|
|
if not fmt_labeled:
|
|
|
|
print ' NOT labeling fields for this run (may be harder to decode).'
|
|
|
|
if not line_transformations:
|
|
|
|
print ' NOT using line reordering transformations'
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-14 07:07:25 +00:00
|
|
|
cards = []
|
|
|
|
valid = 0
|
|
|
|
skipped = 0
|
|
|
|
invalid = 0
|
|
|
|
unparsed = 0
|
|
|
|
|
2015-07-16 06:40:15 +00:00
|
|
|
if fname[-5:] == '.json':
|
|
|
|
if verbose:
|
|
|
|
print 'This looks like a json file: ' + fname
|
|
|
|
json_srcs = jdecode.mtg_open_json(fname, verbose)
|
|
|
|
# don't worry we randomize later
|
|
|
|
for json_cardname in sorted(json_srcs):
|
|
|
|
if len(json_srcs[json_cardname]) > 0:
|
|
|
|
jcards = json_srcs[json_cardname]
|
2015-08-01 09:26:34 +00:00
|
|
|
|
|
|
|
# look for a normal rarity version, in a set we can use
|
|
|
|
idx = 0
|
|
|
|
card = cardlib.Card(jcards[idx])
|
|
|
|
while (idx < len(jcards)
|
|
|
|
and (card.rarity == utils.rarity_special_marker
|
|
|
|
or exclude_sets(jcards[idx][utils.json_field_set_name]))):
|
|
|
|
idx += 1
|
|
|
|
if idx < len(jcards):
|
|
|
|
card = cardlib.Card(jcards[idx])
|
|
|
|
# if there isn't one, settle with index 0
|
|
|
|
if idx >= len(jcards):
|
|
|
|
idx = 0
|
|
|
|
card = cardlib.Card(jcards[idx])
|
|
|
|
# we could go back and look for a card satisfying one of the criteria,
|
|
|
|
# but eh
|
2015-07-16 06:40:15 +00:00
|
|
|
|
|
|
|
skip = False
|
2015-08-01 09:26:34 +00:00
|
|
|
if (exclude_sets(jcards[idx][utils.json_field_set_name])
|
|
|
|
or exclude_layouts(jcards[idx]['layout'])):
|
2015-07-16 06:40:15 +00:00
|
|
|
skip = True
|
|
|
|
for cardtype in card.types:
|
|
|
|
if exclude_types(cardtype):
|
|
|
|
skip = True
|
|
|
|
if skip:
|
|
|
|
skipped += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
if card.valid:
|
|
|
|
valid += 1
|
2015-09-07 03:24:19 +00:00
|
|
|
cards += [card]
|
2015-07-16 06:40:15 +00:00
|
|
|
elif card.parsed:
|
|
|
|
invalid += 1
|
|
|
|
else:
|
|
|
|
unparsed += 1
|
2015-07-17 08:13:58 +00:00
|
|
|
|
2015-07-16 06:40:15 +00:00
|
|
|
# fall back to opening a normal encoded file
|
|
|
|
else:
|
|
|
|
if verbose:
|
|
|
|
print 'Opening encoded card file: ' + fname
|
|
|
|
with open(fname, 'rt') as f:
|
|
|
|
text = f.read()
|
|
|
|
for card_src in text.split(utils.cardsep):
|
|
|
|
if card_src:
|
|
|
|
card = cardlib.Card(card_src)
|
|
|
|
if card.valid:
|
|
|
|
valid += 1
|
2015-09-07 03:24:19 +00:00
|
|
|
cards += [card]
|
2015-07-16 06:40:15 +00:00
|
|
|
elif card.parsed:
|
|
|
|
invalid += 1
|
|
|
|
else:
|
|
|
|
unparsed += 1
|
2015-06-28 01:35:11 +00:00
|
|
|
|
|
|
|
if verbose:
|
2015-07-14 07:07:25 +00:00
|
|
|
print (str(valid) + ' valid, ' + str(skipped) + ' skipped, '
|
|
|
|
+ str(invalid) + ' invalid, ' + str(unparsed) + ' failed to parse.')
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-14 07:07:25 +00:00
|
|
|
# This should give a random but consistent ordering, to make comparing changes
|
|
|
|
# between the output of different versions easier.
|
2015-07-16 06:40:15 +00:00
|
|
|
if not stable:
|
|
|
|
random.seed(1371367)
|
|
|
|
random.shuffle(cards)
|
2015-06-28 01:35:11 +00:00
|
|
|
|
2015-07-29 08:21:34 +00:00
|
|
|
def writecards(writer):
|
2015-07-14 07:07:25 +00:00
|
|
|
for card in cards:
|
2015-07-29 08:21:34 +00:00
|
|
|
if encoding in ['vec']:
|
|
|
|
writer.write(card.vectorize() + '\n\n')
|
|
|
|
else:
|
|
|
|
writer.write(card.encode(fmt_ordered = fmt_ordered,
|
2015-07-16 06:40:15 +00:00
|
|
|
fmt_labeled = fmt_labeled,
|
|
|
|
fieldsep = fieldsep,
|
|
|
|
randomize_fields = randomize_fields,
|
|
|
|
randomize_mana = randomize_mana,
|
|
|
|
initial_sep = initial_sep,
|
|
|
|
final_sep = final_sep)
|
|
|
|
+ utils.cardsep)
|
2015-07-29 08:21:34 +00:00
|
|
|
|
|
|
|
if oname:
|
|
|
|
if verbose:
|
|
|
|
print 'Writing output to: ' + oname
|
|
|
|
with open(oname, 'w') as ofile:
|
|
|
|
writecards(ofile)
|
|
|
|
else:
|
|
|
|
writecards(sys.stdout)
|
2015-07-16 06:40:15 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
2015-06-28 01:35:11 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-07-16 06:40:15 +00:00
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument('infile',
|
|
|
|
help='encoded card file or json corpus to encode')
|
|
|
|
parser.add_argument('outfile', nargs='?', default=None,
|
|
|
|
help='output file, defaults to stdout')
|
2015-09-07 03:24:19 +00:00
|
|
|
parser.add_argument('-e', '--encoding', default='std', choices=utils.formats,
|
|
|
|
#help='{' + ','.join(formats) + '}',
|
|
|
|
help='encoding format to use',
|
|
|
|
)
|
|
|
|
parser.add_argument('-r', '--randomize', action='store_true',
|
|
|
|
help='randomize the order of symbols in mana costs')
|
|
|
|
parser.add_argument('--nolinetrans', action='store_true',
|
|
|
|
help="don't reorder lines of card text")
|
|
|
|
parser.add_argument('--nolabel', action='store_true',
|
|
|
|
help="don't label fields")
|
2015-07-16 06:40:15 +00:00
|
|
|
parser.add_argument('-s', '--stable', action='store_true',
|
|
|
|
help="don't randomize the order of the cards")
|
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help='verbose output')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2015-09-07 03:24:19 +00:00
|
|
|
main(args.infile, args.outfile, verbose = args.verbose, encoding = args.encoding,
|
|
|
|
nolinetrans = args.nolinetrans, randomize = args.randomize, nolabel = args.nolabel,
|
|
|
|
stable = args.stable)
|
2015-07-16 06:40:15 +00:00
|
|
|
exit(0)
|