Skip to content

Commit

Permalink
Show current configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbrnowski authored Aug 16, 2021
2 parents 7be77b4 + 3207639 commit 7c448ae
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 75 deletions.
2 changes: 1 addition & 1 deletion cft/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.5.0'
__version__ = '1.5.1'
45 changes: 22 additions & 23 deletions cft/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'))
Expand All @@ -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():
Expand All @@ -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:
Expand Down
58 changes: 16 additions & 42 deletions cft/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions cft/utils/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cft/utils/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions cft/utils/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'))
Expand All @@ -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)
Expand All @@ -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':
Expand Down
2 changes: 1 addition & 1 deletion cft/utils/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 7c448ae

Please sign in to comment.