#!/usr/bin/python3 import argparse import sys from envec import iloadJSON parser = argparse.ArgumentParser() parser.add_argument('infile', type=argparse.FileType('r'), default=sys.stdin) args = parser.parse_args() with args.infile: for card in iloadJSON(args.infile): print(card.toText1(0,1))
def main(): parser = argparse.ArgumentParser() parser.add_argument('-L', '--latex', action='store_const', dest='format', const='latex') parser.add_argument('-P', '--postscript', action='store_const', dest='format', const='postscript') parser.add_argument('-d', '--dir', default='lists') parser.add_argument('-o', '--outfile', type=argparse.FileType('w', encoding='utf-8')) parser.add_argument('infile', type=argparse.FileType('r'), default=sys.stdin) args = parser.parse_args() if args.outfile and args.format is not None: raise SystemExit('Single-file output is not implemented for' ' LaTeX/PostScript') if not args.outfile: if not os.path.isdir(args.dir): os.mkdir(args.dir) if args.format is None: ext, showSet = '.txt', showTextSet elif args.format == 'latex': ext, showSet = '.tex', showLaTeXSet elif args.format == 'postscript': ext, showSet = '.ps', showPSSet setdb = envec.CardSetDB() sets = defaultdict(list) for card in envec.iloadJSON(args.infile): st = stats(card.part1) if card.isMultipart(): st["part2"] = stats(card.part2) for p in card.printings: try: rarity = envec.Rarity.fromString(p.rarity) except KeyError: raise SystemExit('Unknown rarity %r for %s in %s' % (p.rarity, card.name, p.set)) sets[str(p.set)].append(dict(st, num=p.effectiveNum(), rarity=rarity.longname.split()[0])) for cardset in setdb: cardset = str(cardset) if cardset not in sets: continue if args.outfile: outf = args.outfile else: filename = re.sub(r'[\'"*?[\]]+', '', cardset).replace(' ', '_') outf = open(os.path.join(args.dir, filename + ext), 'w', encoding='utf-8') special = [] cards = [] sets[cardset].sort(key=lambda c: (c["num"] or 0, c["name"])) for c in sets[cardset]: c["num"] = str(c["num"]) + '.' if c["num"] is not None else '' if c["special"]: special.append(c) else: cards.append(c) if "part2" in c: cards.append(dict(c["part2"], num='//', rarity='')) if special and cards: cards = special + [None] + cards else: cards = special or cards showSet(cardset, outf, cards, args.outfile is not None) if not args.outfile: outf.close()