init: MVP
This commit is contained in:
commit
46e870be42
17 changed files with 1202 additions and 0 deletions
0
codeforces/ui/__init__.py
Normal file
0
codeforces/ui/__init__.py
Normal file
60
codeforces/ui/config.py
Normal file
60
codeforces/ui/config.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
import json
|
||||
import sys
|
||||
from getpass import getpass
|
||||
|
||||
from codeforces.client.client import CodeforcesClient
|
||||
from codeforces.client.spinner import Spinner
|
||||
from codeforces.ui.utils import CONFIG_FILE, SESSION_FILE, get_int
|
||||
|
||||
|
||||
def add_login_data():
|
||||
login = input("Login: ")
|
||||
password = getpass("Password: ")
|
||||
|
||||
with Spinner("Logging in (this may take a long time)..."):
|
||||
c = CodeforcesClient.auth(login, password)
|
||||
|
||||
with open(SESSION_FILE, 'w') as f:
|
||||
json.dump({
|
||||
'login': login,
|
||||
'password': password,
|
||||
'session': c.to_dict()
|
||||
}, f)
|
||||
|
||||
|
||||
def add_template():
|
||||
template_path = input('Path to the template file (i.e. ~/algo_template.cpp): ')
|
||||
try:
|
||||
with open(template_path) as f:
|
||||
template = f.read()
|
||||
with open(CONFIG_FILE) as f:
|
||||
config = json.load(f)
|
||||
config.update({'template': template})
|
||||
with open(CONFIG_FILE, 'w') as f:
|
||||
json.dump(config, f)
|
||||
except Exception:
|
||||
print("Invalid path!", file=sys.stderr)
|
||||
return add_template()
|
||||
|
||||
print("Added successfuly!", end='\n\n')
|
||||
|
||||
|
||||
def config_menu():
|
||||
print('CFTool-Reborn configuration menu. Press <C-c> to exit.', end='\n\n')
|
||||
|
||||
options = [
|
||||
'1) Add/Update login data',
|
||||
'2) Add a template'
|
||||
]
|
||||
|
||||
try:
|
||||
while True:
|
||||
ans = get_int(options)
|
||||
if ans == 0:
|
||||
return
|
||||
elif ans == 1:
|
||||
add_login_data()
|
||||
elif ans == 2:
|
||||
add_template()
|
||||
except KeyboardInterrupt:
|
||||
return
|
18
codeforces/ui/utils.py
Normal file
18
codeforces/ui/utils.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
CONFIG_FILE = Path(os.path.join(os.environ['XDG_CONFIG_HOME'] if 'XDG_CONFIG_HOME' in os.environ else f"{os.environ['HOME']}/.config", 'codeforces', 'config.json'))
|
||||
SESSION_FILE = Path(os.path.join(os.environ['XDG_STATE_HOME'] if 'XDG_STATE_HOME' in os.environ else f"{os.environ['HOME']}/.local/state", 'codeforces', 'session.json'))
|
||||
|
||||
|
||||
def get_int(options: list[str]) -> int:
|
||||
while True:
|
||||
try:
|
||||
print('\n'.join(options))
|
||||
num = int(input("Enter a number: "))
|
||||
assert num <= len(options)
|
||||
assert num >= 0
|
||||
return num
|
||||
except Exception:
|
||||
print("Invalid input!\n", file=sys.stderr)
|
Loading…
Add table
Add a link
Reference in a new issue