diff --git a/cft/__init__.py b/cft/__init__.py index 77f1c8e..51ed7c4 100644 --- a/cft/__init__.py +++ b/cft/__init__.py @@ -1 +1 @@ -__version__ = '1.5.0' +__version__ = '1.5.1' diff --git a/cft/utils/config.py b/cft/utils/config.py index f7555e6..6feef42 100644 --- a/cft/utils/config.py +++ b/cft/utils/config.py @@ -8,17 +8,23 @@ def config(args): try_upgrade() - print('Choose one of the following (type an integer):', - ' 1. change the template file', - ' 2. change username and password', - ' 3. change password', - ' 4. change language', - ' 5. set compile command', - ' 6. set run command', - sep='\n') + + print('Choose one of the following (type an integer):') + options = [('1. change the template file', get_config('template', strict=False)), + ('2. change username and password', get_config('username', strict=False)), + ('3. change programming language', get_config('language', strict=False).name), + ('4. set compile command', get_config('compile', strict=False)), + ('5. set run command', get_config('run', strict=False))] + for option, current in options: + if current and option.startswith('1') and not os.path.exists(current): + current = ' ' + info_style('[') + error_style(current) + info_style(']') + elif current: + current = ' ' + info_style('[') + neutral_style(current) + info_style(']') + print(f' {option}{current}') + try: - while (choice := input()) not in {'1', '2', '3', '4', '5', '6'}: - print(warning_style('Type an integer 1-6:')) + while (choice := input()) not in {'1', '2', '3', '4', '5'}: + print(warning_style('Type an integer 1-5:')) choice = int(choice) except KeyboardInterrupt: print(info_style('Aborted.')) @@ -37,18 +43,11 @@ def config(args): config_dict['template'] = input('Path to the template: ') if choice == 2: - config_dict['username'] = input('Username: ') - - if choice in (2, 3): - try: - username = config_dict['username'] - except KeyError: - print(error_style('First enter your username.')) - sys.exit() + username = config_dict['username'] = input('Username: ') password = getpass.getpass('Password: ') keyring.set_password('codeforces-toolbox', username, password) - if choice == 4: + if choice == 3: print('Change your programming language and the program that Codeforces will use to run your solution.') print('Choose one of the following (type an integer):') for key, lan in LANGUAGES.items(): @@ -57,16 +56,16 @@ def config(args): print(warning_style('Type an integer 1-5:')) config_dict['language'] = (LANGUAGES[language].n, LANGUAGES[language].name, LANGUAGES[language].ext) - if choice == 5: + if choice == 4: print('Set compile command, e.g. `g++ -Wall -O1`.') print('If you are using ' + neutral_style('Python') + ' or do not want to compile your solutions, just press enter.') config_dict['compile'] = input('Compile command: ') - if choice == 6: + if choice == 5: print('Set run command, e.g. `python` or enter an absolute path to the interpreter.') - print('If you are using ' + neutral_style('C++') + ' or ' + neutral_style('C') + - ' you can just press enter - your run command will be just `./`.') + print('If you are using any language other than ' + neutral_style('Python') + ' or ' + neutral_style('Java') + + ', you can just press enter, your run command will be just `./`.') config_dict['run'] = input('Run command: ') except KeyboardInterrupt: diff --git a/cft/utils/constants.py b/cft/utils/constants.py index 28c3d88..dd30dfd 100644 --- a/cft/utils/constants.py +++ b/cft/utils/constants.py @@ -70,64 +70,38 @@ def translate_problem_name(problem): return contest, problem_letter -def get_template(): - try: - config_dict = json.load(open(CONFIG_FILE)) - template = config_dict['template'] - except FileNotFoundError: - print(error_style('Configuration file has not been found.')) - sys.exit() - except KeyError: - print(error_style('Specify your template file first.')) - sys.exit() - return template +def get_config(setting, strict=True): + if setting == 'language': + return _get_config_language(strict=strict) - -def get_username(): try: config_dict = json.load(open(CONFIG_FILE)) - username = config_dict['username'] + setting = config_dict[setting] except FileNotFoundError: + if not strict: + return '' print(error_style('Configuration file has not been found.')) sys.exit() except KeyError: - print(error_style('Specify your username first.')) + if setting in ('compile', 'run') or not strict: + return '' + print(error_style(f'Specify your {setting} first.')) sys.exit() - return username + return setting -def get_language(): +def _get_config_language(strict=True): try: config_dict = json.load(open(CONFIG_FILE)) language = config_dict['language'] except FileNotFoundError: - print(error_style('Configuration file has not been found.')) + if not strict: + return Language(0, '', '') + print(error_style('Configuration file has not been found. Use `cft config`.')) sys.exit() except KeyError: + if not strict: + return Language(0, '', '') print(error_style('Specify your programming language first.')) sys.exit() return Language(*language) - - -def get_run_command(): - try: - config_dict = json.load(open(CONFIG_FILE)) - run_command = config_dict['run'] - except FileNotFoundError: - print(error_style('Configuration file has not been found.')) - sys.exit() - except KeyError: - return '' - return run_command - - -def get_compile_command(): - try: - config_dict = json.load(open(CONFIG_FILE)) - compile_command = config_dict['compile'] - except FileNotFoundError: - print(error_style('Configuration file has not been found.')) - sys.exit() - except KeyError: - return '' - return compile_command diff --git a/cft/utils/race.py b/cft/utils/race.py index f2d6826..47abdfc 100644 --- a/cft/utils/race.py +++ b/cft/utils/race.py @@ -7,9 +7,9 @@ def race(args): - language = get_language() + language = get_config('language') contest = args.contest - template = get_template() + template = get_config('template') if not os.path.exists(template): print(error_style('Template file does not exist.')) sys.exit() diff --git a/cft/utils/submit.py b/cft/utils/submit.py index 1b73524..4f4a475 100644 --- a/cft/utils/submit.py +++ b/cft/utils/submit.py @@ -14,7 +14,7 @@ def submit(args): site = s.get('https://codeforces.com/enter') soup = bs4.BeautifulSoup(site.content, 'html.parser') csrf_token = soup.select_one('.csrf-token')['data-csrf'] - username = get_username() + username = get_config('username') password = keyring.get_password('codeforces-toolbox', username) login_data = {'csrf_token': csrf_token, 'action': 'enter', 'handleOrEmail': username, 'password': password} login_response = s.post('https://codeforces.com/enter', data=login_data) @@ -32,7 +32,7 @@ def submit(args): site = s.get(f'https://codeforces.com/contest/{contest}/submit') soup = bs4.BeautifulSoup(site.content, 'html.parser') csrf_token = soup.select_one('.csrf-token')['data-csrf'] - language = get_language() + language = get_config('language') with open(os.path.join(os.getcwd(), f'{contest}{problem_letter}.{language.ext}')) as f: solution = f.read() submit_data = {'csrf_token': csrf_token, 'submittedProblemIndex': problem_letter, 'programTypeId': language.n, diff --git a/cft/utils/test.py b/cft/utils/test.py index 09aaa84..b58558b 100644 --- a/cft/utils/test.py +++ b/cft/utils/test.py @@ -39,7 +39,7 @@ def test(args): with open(os.path.join('ans', f'{i}.out'), 'w') as answer_file: answer_file.write(test_ans.string.strip()) - compile_command = get_compile_command() + compile_command = get_config('compile') if compile_command: if compile_solution(problem, compile_command).returncode != 0: print(error_style('Solution has not been compiled.')) @@ -54,7 +54,7 @@ def test(args): def compile_solution(problem, compile_command): - language = get_language() + language = get_config('language') try: if language.ext in ('c', 'cpp', 'kt'): return subprocess.run([*compile_command.split(' '), f'{problem}.{language.ext}', '-o', problem], timeout=10) @@ -75,8 +75,8 @@ def test_solution(problem, i, args): with open(os.path.join('ans', f'{i}.out')) as answer_file: test_ans = answer_file.read() - language = get_language() - run_command = get_run_command() + language = get_config('language') + run_command = get_config('run') try: if run_command: if language.ext == 'java': diff --git a/cft/utils/upgrade.py b/cft/utils/upgrade.py index 8553556..02902f0 100644 --- a/cft/utils/upgrade.py +++ b/cft/utils/upgrade.py @@ -20,7 +20,7 @@ def try_upgrade(): if d.lower() in ('y', 'yes'): s = subprocess.run([sys.executable, '-m', 'pip', 'install', '--upgrade', 'codeforces-toolbox'], stdout=subprocess.DEVNULL) - if s.returncode: + if s.returncode != 0: print(error_style('Installation failed.\n')) else: print(info_style('Installation was successful.\n'))