From a83df5f94a249a0e4793fe7d260644322463f2e4 Mon Sep 17 00:00:00 2001 From: kzrnm Date: Tue, 1 Nov 2022 19:15:17 +0900 Subject: [PATCH 1/3] token envvar --- onlinejudge_command/subcommand/download.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onlinejudge_command/subcommand/download.py b/onlinejudge_command/subcommand/download.py index bbd287e6..5276a84f 100644 --- a/onlinejudge_command/subcommand/download.py +++ b/onlinejudge_command/subcommand/download.py @@ -65,8 +65,8 @@ def add_subparser(subparsers: argparse.Action) -> None: 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('-s', '--silent', action='store_true') - subparser.add_argument('--yukicoder-token', type=str) - subparser.add_argument('--dropbox-token', type=str) + subparser.add_argument('--yukicoder-token', type=str, default=os.environ.get('YUKICODER_TOKEN'), help='[deprecated] specify the token of yukicoder. For a security reason, use the $YUKICODER_TOKEN envvar. (default: $YUKICODER_TOKEN)') + subparser.add_argument('--dropbox-token', type=str, default=os.environ.get('DROPBOX_TOKEN'), help='[deprecated] specify the token of dropbox. For a security reason, use the $DROPBOX_TOKEN envvar. (default: $DROPBOX_TOKEN)') subparser.add_argument('--log-file', type=pathlib.Path, help=argparse.SUPPRESS) From b2aa6cca790eb3352c171fd203db5a3d484e9d6d Mon Sep 17 00:00:00 2001 From: kzrnm Date: Thu, 3 Nov 2022 21:41:55 +0900 Subject: [PATCH 2/3] Read environ --- onlinejudge_command/main.py | 6 ++++++ onlinejudge_command/subcommand/download.py | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/onlinejudge_command/main.py b/onlinejudge_command/main.py index 1bd14509..331ec1e4 100644 --- a/onlinejudge_command/main.py +++ b/onlinejudge_command/main.py @@ -1,4 +1,5 @@ import argparse +import os import pathlib import sys import traceback @@ -86,6 +87,11 @@ def main(args: Optional[List[str]] = None) -> 'NoReturn': parser = get_parser() parsed = parser.parse_args(args=args) + if parsed.yukicoder_token: + os.environ['YUKICODER_TOKEN'] = parsed.yukicoder_token + if parsed.dropbox_token: + os.environ['DROPBOX_TOKEN'] = parsed.dropbox_token + # configure the logger level = INFO if parsed.verbose: diff --git a/onlinejudge_command/subcommand/download.py b/onlinejudge_command/subcommand/download.py index 5276a84f..53544901 100644 --- a/onlinejudge_command/subcommand/download.py +++ b/onlinejudge_command/subcommand/download.py @@ -65,8 +65,8 @@ def add_subparser(subparsers: argparse.Action) -> None: 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('-s', '--silent', action='store_true') - subparser.add_argument('--yukicoder-token', type=str, default=os.environ.get('YUKICODER_TOKEN'), help='[deprecated] specify the token of yukicoder. For a security reason, use the $YUKICODER_TOKEN envvar. (default: $YUKICODER_TOKEN)') - subparser.add_argument('--dropbox-token', type=str, default=os.environ.get('DROPBOX_TOKEN'), help='[deprecated] specify the token of dropbox. For a security reason, use the $DROPBOX_TOKEN envvar. (default: $DROPBOX_TOKEN)') + subparser.add_argument('--yukicoder-token', type=str, help='[deprecated] specify the token of yukicoder. For a security reason, use the $YUKICODER_TOKEN envvar. (default: $YUKICODER_TOKEN)') + subparser.add_argument('--dropbox-token', type=str, help='[deprecated] specify the token of dropbox. For a security reason, use the $DROPBOX_TOKEN envvar. (default: $DROPBOX_TOKEN)') subparser.add_argument('--log-file', type=pathlib.Path, help=argparse.SUPPRESS) @@ -96,7 +96,7 @@ def run(args: argparse.Namespace) -> bool: # get samples from the server with utils.new_session_with_our_user_agent(path=args.cookie) as sess: if isinstance(problem, AtCoderProblem) and args.system: - if not args.dropbox_token: + if 'DROPBOX_TOKEN' not in os.environ: logger.info(utils.HINT + 'You need to give the access token. Please do the following:\n%s', textwrap.dedent(""" 1. Open the following URL in your browser: https://www.dropbox.com/oauth2/authorize?client_id=153gig8dqgk3ujg&response_type=code @@ -110,9 +110,9 @@ def run(args: argparse.Namespace) -> bool: (Please take care that the access code and the access token are CONFIDENTIAL information. DON'T SHARE with other people!) """)) raise SampleParseError("--dropbox-token is not given") - sess.headers['Authorization'] = 'Bearer {}'.format(args.dropbox_token) - if args.yukicoder_token and isinstance(problem, YukicoderProblem): - sess.headers['Authorization'] = 'Bearer {}'.format(args.yukicoder_token) + sess.headers['Authorization'] = 'Bearer {}'.format(os.getenv['DROPBOX_TOKEN']) + if 'YUKICODER_TOKEN' in os.environ and isinstance(problem, YukicoderProblem): + sess.headers['Authorization'] = 'Bearer {}'.format(os.getenv['YUKICODER_TOKEN']) try: if args.system: samples = problem.download_system_cases(session=sess) From 37cb73fd3995d1092be5cbc263fac0901627c011 Mon Sep 17 00:00:00 2001 From: kzrnm Date: Thu, 5 Oct 2023 12:47:27 +0900 Subject: [PATCH 3/3] getenv is function --- onlinejudge_command/subcommand/download.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onlinejudge_command/subcommand/download.py b/onlinejudge_command/subcommand/download.py index 53544901..72d823a9 100644 --- a/onlinejudge_command/subcommand/download.py +++ b/onlinejudge_command/subcommand/download.py @@ -110,9 +110,9 @@ def run(args: argparse.Namespace) -> bool: (Please take care that the access code and the access token are CONFIDENTIAL information. DON'T SHARE with other people!) """)) raise SampleParseError("--dropbox-token is not given") - sess.headers['Authorization'] = 'Bearer {}'.format(os.getenv['DROPBOX_TOKEN']) + sess.headers['Authorization'] = 'Bearer {}'.format(os.getenv('DROPBOX_TOKEN')) if 'YUKICODER_TOKEN' in os.environ and isinstance(problem, YukicoderProblem): - sess.headers['Authorization'] = 'Bearer {}'.format(os.getenv['YUKICODER_TOKEN']) + sess.headers['Authorization'] = 'Bearer {}'.format(os.getenv('YUKICODER_TOKEN')) try: if args.system: samples = problem.download_system_cases(session=sess)