diff --git a/decode.py b/decode.py index ced02dc..8b2f337 100755 --- a/decode.py +++ b/decode.py @@ -13,7 +13,7 @@ from cbow import CBOW from namediff import Namediff def main(fname, oname = None, verbose = True, encoding = 'std', - gatherer = False, for_forum = False, for_mse = False, + gatherer = False, for_forum = False, for_mse = False, for_json = False, creativity = False, vdump = False, for_html = False): # there is a sane thing to do here (namely, produce both at the same time) @@ -104,7 +104,9 @@ def main(fname, oname = None, verbose = True, encoding = 'std', for card in cards: - if for_mse: + if for_json: + writer.write(card.to_json()) + elif for_mse: writer.write(card.to_mse().encode('utf-8')) fstring = '' if card.json: @@ -290,12 +292,14 @@ if __name__ == '__main__': help='verbose output') parser.add_argument('-mse', '--mse', action='store_true', help='use Magic Set Editor 2 encoding; will output as .mse-set file') + parser.add_argument('-j', '--json', action='store_true', + help='use JSON encoding; will output as .json file') parser.add_argument('-html', '--html', action='store_true', help='create a .html file with pretty forum formatting') args = parser.parse_args() main(args.infile, args.outfile, verbose = args.verbose, encoding = args.encoding, - gatherer = args.gatherer, for_forum = args.forum, for_mse = args.mse, + gatherer = args.gatherer, for_forum = args.forum, for_mse = args.mse, for_json = args.json, creativity = args.creativity, vdump = args.dump, for_html = args.html) exit(0) diff --git a/lib/cardlib.py b/lib/cardlib.py index fdf04ed..aacbe0c 100644 --- a/lib/cardlib.py +++ b/lib/cardlib.py @@ -1,6 +1,7 @@ # card representation import re import random +import json import utils import transforms @@ -391,6 +392,15 @@ def fields_from_format(src_text, fmt_ordered, fmt_labeled, fieldsep): # again, bsides are handled by the constructor return parsed, valid and fields_check_valid(fields), fields +class CardEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, Manatext): + return obj.format() + if isinstance(obj, Manacost): + return obj.format() + # Let the base class default method raise the TypeError + return json.JSONEncoder.default(self, obj) + # Here's the actual Card class that other files should use. class Card: @@ -565,6 +575,31 @@ class Card: for idx, value in values: self.__dict__[field_other] += [(idx, value)] + def to_json(self): + fields = {} + for key in self.fields: + fields[key] = self.fields[key][0][1] + fields["name"] = titlecase(transforms.name_unpass_1_dashes(self.__dict__[field_name])) + mtext = self.__dict__[field_text].text + mtext = transforms.text_unpass_1_choice(mtext, delimit = False) + mtext = transforms.text_unpass_2_counters(mtext) + mtext = transforms.text_unpass_3_uncast(mtext) + mtext = transforms.text_unpass_4_unary(mtext) + mtext = transforms.text_unpass_5_symbols(mtext, False, False) + mtext = sentencecase(mtext) + #mtext = transforms.text_unpass_6_cardname(mtext, fields["name"]) + mtext = transforms.text_unpass_7_newlines(mtext) + mtext = transforms.text_unpass_8_unicode(mtext) + newtext = Manatext('') + newtext.text = mtext + newtext.costs = self.__dict__[field_text].costs + fields["text"] = newtext.format(for_forum = False, for_html = False) + if self.__dict__[field_pt]: + fields["pt"] = utils.from_unary(self.__dict__[field_pt]) + if self.__dict__[field_loyalty]: + fields["loyalty"] = utils.from_unary(self.__dict__[field_loyalty]) + return json.dumps(fields, cls=CardEncoder) + # Output functions that produce various formats. encode() is specific to # the NN representation, use str() or format() for output intended for human # readers.