Skip to content

Commit ee5c6ee

Browse files
authored
setterを追加 (kyuridenamida#249)
* setterを追加 * setterを追加 * テストを追加 * LoginErrorの判断基準を修正 * codegenでwithout-loginが使われていなかったので変更
1 parent 99f5c78 commit ee5c6ee

File tree

14 files changed

+317
-3
lines changed

14 files changed

+317
-3
lines changed

atcodertools/atcoder_tools.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from atcodertools.tools.submit import main as submit_main
1111
from atcodertools.tools.codegen import main as codegen_main
1212
from atcodertools.tools.compiler import main as compiler_main
13+
from atcodertools.tools.setter import main as setter_main
1314
from atcodertools.release_management.version import __version__
1415
from colorama import Fore, Style
1516

@@ -39,14 +40,16 @@ def notify_if_latest_version_found():
3940
def main():
4041
notify_if_latest_version_found()
4142

42-
if len(sys.argv) < 2 or sys.argv[1] not in ("gen", "test", "submit", "codegen", "compile", "version"):
43+
if len(sys.argv) < 2 or sys.argv[1] not in ("gen", "test", "submit", "codegen", "compile", "set", "version"):
4344
print("Usage:")
4445
print("{} gen -- to generate workspace".format(sys.argv[0]))
4546
print(
4647
"{} compile -- to compile codes in your workspace".format(sys.argv[0]))
4748
print("{} test -- to test codes in your workspace".format(sys.argv[0]))
4849
print(
4950
"{} submit -- to submit a code to the contest system".format(sys.argv[0]))
51+
print(
52+
"{} set -- to set some additional option(error value, language)".format(sys.argv[0]))
5053
print(
5154
"{} version -- show atcoder-tools version".format(sys.argv[0]))
5255
sys.exit(-1)
@@ -69,5 +72,8 @@ def main():
6972
if sys.argv[1] == "compile":
7073
compiler_main(prog, args)
7174

75+
if sys.argv[1] == "set":
76+
setter_main(prog, args)
77+
7278
if sys.argv[1] == "version":
7379
print(__version__)

atcodertools/client/atcoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def login(self,
103103
"csrf_token": token
104104
}, method='POST')
105105

106-
if resp.text.find("パスワードを忘れた方はこちら") != -1:
106+
if resp.text.find("パスワードを忘れた方はこちら") != -1 or resp.text.find("Forgot your password") != -1:
107107
raise LoginError
108108

109109
if use_local_session_cache and save_session_cache:

atcodertools/common/judgetype.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class ErrorType(Enum):
1616
AbsoluteOrRelative = "absolute_or_relative"
1717

1818

19+
DEFAULT_EPS = 1e-9
20+
21+
22+
class NoJudgeTypeException(Exception):
23+
pass
24+
25+
1926
class Judge(metaclass=ABCMeta):
2027
@abstractmethod
2128
def verify(self, output, expected):

atcodertools/tools/codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def main(prog, args, output_file=sys.stdout):
163163
config = get_config(args)
164164

165165
client = AtCoderClient()
166-
if not config.etc_config.download_without_login:
166+
if not args.without_login or not config.etc_config.download_without_login:
167167
try:
168168
client.login(
169169
save_session_cache=not config.etc_config.save_no_session_cache)

atcodertools/tools/setter.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/python3
2+
3+
import argparse
4+
import os
5+
from atcodertools.common.judgetype import NormalJudge, DecimalJudge, ErrorType,\
6+
JudgeType, NoJudgeTypeException, DEFAULT_EPS
7+
from atcodertools.common.logging import logger
8+
from atcodertools.tools.models.metadata import Metadata
9+
from atcodertools.common.language import Language, ALL_LANGUAGES
10+
from atcodertools.tools.codegen import main as codegen_main
11+
12+
USER_FACING_JUDGE_TYPE_LIST = [
13+
"normal", "absolute", "relative", "absolute_or_relative"]
14+
15+
16+
def main(prog, args) -> None:
17+
if len(args) == 0:
18+
print("Usage: atcoder tools set [options]")
19+
return
20+
21+
parser = argparse.ArgumentParser(
22+
prog=prog,
23+
formatter_class=argparse.RawTextHelpFormatter)
24+
25+
parser.add_argument('--judge-type', '-j',
26+
help='error type'
27+
' must be one of [{}]'.format(
28+
', '.join(USER_FACING_JUDGE_TYPE_LIST)),
29+
type=str,
30+
default=None)
31+
32+
parser.add_argument('--error-value', '-v',
33+
help='error value for decimal number judge:'
34+
' [Default] ' + str(DEFAULT_EPS),
35+
type=float,
36+
default=None)
37+
38+
parser.add_argument("--lang", '-l',
39+
help="Programming language of your template code, {}.\n".format(
40+
" or ".join([lang.name for lang in ALL_LANGUAGES])),
41+
default=None)
42+
43+
parser.add_argument("--dir", '-d',
44+
help="Target directory to test. [Default] Current directory",
45+
default=".")
46+
47+
parser.add_argument("--without-login",
48+
action="store_true",
49+
help="Download data without login")
50+
51+
args = parser.parse_args(args)
52+
53+
old_metadata = Metadata.load_from(os.path.join(args.dir, "metadata.json"))
54+
55+
# Use the old metadata as base metadata.
56+
output_metadata = Metadata.load_from(
57+
os.path.join(args.dir, "metadata.json"))
58+
59+
old_metadata_judge_type = old_metadata.judge_method.judge_type.value
60+
61+
if args.judge_type in ["absolute", "relative", "absolute_or_relative"]:
62+
new_metadata_judge_type = "decimal"
63+
output_metadata.judge_method.error_type = ErrorType(args.judge_type)
64+
elif args.judge_type is not None:
65+
new_metadata_judge_type = args.judge_type
66+
else:
67+
new_metadata_judge_type = old_metadata_judge_type
68+
69+
if new_metadata_judge_type is not None and new_metadata_judge_type != old_metadata_judge_type:
70+
if new_metadata_judge_type == JudgeType.Normal.value:
71+
output_metadata.judge_method = NormalJudge()
72+
elif new_metadata_judge_type == JudgeType.Decimal.value:
73+
output_metadata.judge_method = DecimalJudge()
74+
if args.error_value is None:
75+
logger.warn(
76+
"Error-value is not specified. DEFAULT_EPS is set")
77+
output_metadata.judge_method.diff = DEFAULT_EPS
78+
else:
79+
raise NoJudgeTypeException()
80+
81+
if new_metadata_judge_type == JudgeType.Decimal.value and args.error_value is not None:
82+
output_metadata.judge_method.diff = args.error_value
83+
84+
if args.lang is not None:
85+
if args.lang != output_metadata.lang.name:
86+
output_metadata.lang = Language.from_name(args.lang)
87+
output_metadata.code_filename = output_metadata.lang.get_code_filename(
88+
'main')
89+
url = "https://atcoder.jp/contests/{}/tasks/{}".format(
90+
output_metadata.problem.contest.contest_id, output_metadata.problem.problem_id)
91+
main_code_filename = os.path.join(
92+
args.dir, output_metadata.code_filename)
93+
if not os.path.exists(main_code_filename):
94+
a = ["--lang", output_metadata.lang.name, url]
95+
if args.without_login:
96+
a.append("--without-login")
97+
codegen_main("", a, open(main_code_filename, 'w'))
98+
else:
99+
print("File exists: ", output_metadata.code_filename)
100+
else:
101+
print("Already set to {}. Skipping changing language...".format(args.lang))
102+
output_metadata.save_to(os.path.join(args.dir, "metadata.json"))

tests/resources/test_atcoder_client_mock/exec_on_submit/exec_after_submit_is_completed

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Kyuuridenamida
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Main {
5+
6+
// Generated by 2.9.0 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
7+
public static void main(String[] args) throws Exception {
8+
final Scanner sc = new Scanner(System.in);
9+
long T;
10+
T = sc.nextLong();
11+
long X;
12+
X = sc.nextLong();
13+
solve(T, X);
14+
}
15+
16+
static void solve(long T, long X){
17+
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"code_filename": "main.java",
3+
"judge": {
4+
"diff": 1e-07,
5+
"error_type": "relative",
6+
"judge_type": "decimal"
7+
},
8+
"lang": "java",
9+
"problem": {
10+
"alphabet": "A",
11+
"contest": {
12+
"contest_id": "abc117"
13+
},
14+
"problem_id": "abc117_a"
15+
},
16+
"sample_in_pattern": "in_*.txt",
17+
"sample_out_pattern": "out_*.txt"
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"code_filename": "main.java",
3+
"judge": {
4+
"diff": 0.001,
5+
"error_type": "relative",
6+
"judge_type": "decimal"
7+
},
8+
"lang": "java",
9+
"problem": {
10+
"alphabet": "A",
11+
"contest": {
12+
"contest_id": "abc117"
13+
},
14+
"problem_id": "abc117_a"
15+
},
16+
"sample_in_pattern": "in_*.txt",
17+
"sample_out_pattern": "out_*.txt"
18+
}

0 commit comments

Comments
 (0)