Add JSON output
This commit is contained in:
parent
ee5f26590d
commit
3e00511289
2 changed files with 42 additions and 3 deletions
10
decode.py
10
decode.py
|
@ -13,7 +13,7 @@ from cbow import CBOW
|
||||||
from namediff import Namediff
|
from namediff import Namediff
|
||||||
|
|
||||||
def main(fname, oname = None, verbose = True, encoding = 'std',
|
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):
|
creativity = False, vdump = False, for_html = False):
|
||||||
|
|
||||||
# there is a sane thing to do here (namely, produce both at the same time)
|
# 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:
|
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'))
|
writer.write(card.to_mse().encode('utf-8'))
|
||||||
fstring = ''
|
fstring = ''
|
||||||
if card.json:
|
if card.json:
|
||||||
|
@ -290,12 +292,14 @@ if __name__ == '__main__':
|
||||||
help='verbose output')
|
help='verbose output')
|
||||||
parser.add_argument('-mse', '--mse', action='store_true',
|
parser.add_argument('-mse', '--mse', action='store_true',
|
||||||
help='use Magic Set Editor 2 encoding; will output as .mse-set file')
|
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')
|
parser.add_argument('-html', '--html', action='store_true', help='create a .html file with pretty forum formatting')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
main(args.infile, args.outfile, verbose = args.verbose, encoding = args.encoding,
|
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)
|
creativity = args.creativity, vdump = args.dump, for_html = args.html)
|
||||||
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# card representation
|
# card representation
|
||||||
import re
|
import re
|
||||||
import random
|
import random
|
||||||
|
import json
|
||||||
|
|
||||||
import utils
|
import utils
|
||||||
import transforms
|
import transforms
|
||||||
|
@ -391,6 +392,15 @@ def fields_from_format(src_text, fmt_ordered, fmt_labeled, fieldsep):
|
||||||
# again, bsides are handled by the constructor
|
# again, bsides are handled by the constructor
|
||||||
return parsed, valid and fields_check_valid(fields), fields
|
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.
|
# Here's the actual Card class that other files should use.
|
||||||
|
|
||||||
class Card:
|
class Card:
|
||||||
|
@ -565,6 +575,31 @@ class Card:
|
||||||
for idx, value in values:
|
for idx, value in values:
|
||||||
self.__dict__[field_other] += [(idx, value)]
|
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
|
# Output functions that produce various formats. encode() is specific to
|
||||||
# the NN representation, use str() or format() for output intended for human
|
# the NN representation, use str() or format() for output intended for human
|
||||||
# readers.
|
# readers.
|
||||||
|
|
Loading…
Reference in a new issue