Skip to content

Commit

Permalink
#201: add --json option for download
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Dec 17, 2018
1 parent 46c8093 commit 9373eec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions onlinejudge/implementation/command/download.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# Python Version: 3.x
import onlinejudge
import onlinejudge.problem
import onlinejudge.implementation.utils as utils
import onlinejudge.implementation.logging as log
import os
import colorama
import sys
import json
from typing import *
if TYPE_CHECKING:
import argparse

def convert_sample_to_dict(sample: onlinejudge.problem.TestCase) -> dict:
data = {}
data["input"] = sample.input.data
data["output"] = sample.output.data
if sample.input.name == sample.output.name:
data["name"] = sample.input.name
return data

def download(args: 'argparse.Namespace') -> None:
# prepare values
problem = onlinejudge.dispatch.problem_from_url(args.url)
Expand Down Expand Up @@ -62,3 +72,7 @@ def download(args: 'argparse.Namespace') -> None:
with open(path, 'w') as fh:
fh.write(data)
log.success('saved to: %s', path)

# print json
if args.json:
print(json.dumps(list(map(convert_sample_to_dict, samples))))
1 change: 1 addition & 0 deletions onlinejudge/implementation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def get_parser() -> argparse.ArgumentParser:
subparser.add_argument('--overwrite', action='store_true')
subparser.add_argument('-n', '--dry-run', action='store_true', help='don\'t write to files')
subparser.add_argument('-a', '--system', action='store_true', help='download system testcases')
subparser.add_argument('--json', action='store_true')

# login
subparser = subparsers.add_parser('login',
Expand Down

0 comments on commit 9373eec

Please sign in to comment.