diff --git a/COMMANDS.md b/COMMANDS.md index a090b34..3d3ac0b 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -20,6 +20,24 @@ This will change the runtime environment. Currently value of `` can only be 'python' or 'cpp' are supported +* ``` + ecp config set-user + ``` + Example: + ``` + ecp config set-user tourist + ``` + This fetch info about the user from config.json file. + +* ``` + ecp config get-user + ``` + Example: + ``` + ecp config get-user + ``` + This will add necessary info about the user to config.json file. + * ``` ecp config set-proxy ``` @@ -67,4 +85,4 @@ * Get remaining time duration in a test using: ``` ecp test time - ``` \ No newline at end of file + ``` diff --git a/src/Cli/cli.py b/src/Cli/cli.py index 9abaff9..1c908cb 100644 --- a/src/Cli/cli.py +++ b/src/Cli/cli.py @@ -7,7 +7,6 @@ from .runner import run from .config import config from .test import test - @click.group() def entry_point(): pass diff --git a/src/Cli/config.py b/src/Cli/config.py index f8bf736..63f6f8f 100644 --- a/src/Cli/config.py +++ b/src/Cli/config.py @@ -66,8 +66,27 @@ def remove_proxy(): except Exception as e: click.echo(e) +@click.command() +@click.argument("user",type=str) +def set_user(user): + try: + config = Config() + config.set_user(user) + click.echo(click.style('User information have been fetched.', fg='green')) + except Exception as e: + click.echo(click.style(e,fg='red')) +@click.command() +def get_user(): + try: + config = Config() + config.get_user() + except Exception as e: + click.echo(click.style(e,fg='red')) + config.add_command(get_lang) config.add_command(set_lang) config.add_command(set_temp) config.add_command(set_proxy) -config.add_command(remove_proxy) \ No newline at end of file +config.add_command(remove_proxy) +config.add_command(set_user) +config.add_command(get_user) diff --git a/src/Config/Config.py b/src/Config/Config.py index 68022ad..794e9e1 100644 --- a/src/Config/Config.py +++ b/src/Config/Config.py @@ -1,8 +1,12 @@ import json import os +import requests +import click from pathlib import Path from ..Runner.exceptions.UnsupportedLanguage import UnsupportedLanguage - +from .exceptions.UserNotFound import UsernameNotFound +from .exceptions.UserNotSet import UserNotSet +from requests.adapters import HTTPAdapter, Retry ''' Class to manage config related commands ''' @@ -76,9 +80,9 @@ def get_proxy(self): config = json.load(config_file) proxy = config['proxy'] if len(proxy)==0: - proxy = {'http':None,'https':None} + proxy = {'http':'','https':''} else: - proxy = {'http':'http://'+proxy,'https':'https://'+proxy} + proxy = {'http':'http://'+proxy} return proxy def remove_proxy(self): @@ -88,3 +92,31 @@ def remove_proxy(self): config_file.seek(0) config_file.truncate() config_file.write(json.dumps(config)) + + def set_user(self, user): + api_url = "https://codeforces.com/api/user.info?handles=" + response = requests.get(url = api_url + user, proxies = self.get_proxy()) + if(response.status_code != 200): + raise UsernameNotFound(user) + html_content = response.json() + config_file_path = self.config_file_path + with open(config_file_path, 'r+') as config_file: + config = json.load(config_file) + config["user"]["firstname"] = html_content["result"][0]["firstName"] + config["user"]["lastname"] = html_content["result"][0]["lastName"] + config["user"]["rating"] = html_content["result"][0]["rating"] + config["user"]["contri"] = html_content["result"][0]["contribution"] + config["user"]["rank"] = html_content["result"][0]["rank"] + config["user"]["maxrating"] = html_content["result"][0]["maxRating"] + config_file.seek(0) + config_file.truncate() + config_file.write(json.dumps(config)) + + def get_user(self): + config_file_path = self.config_file_path + with open(config_file_path, 'r') as config_file: + config = json.load(config_file) + if(len(config["user"]["firstname"]) == 0): + raise UserNotSet() + for info in config["user"]: + print(info + " : " + str(config["user"][info])) diff --git a/src/Config/config.json b/src/Config/config.json index b8d4b77..4b69692 100644 --- a/src/Config/config.json +++ b/src/Config/config.json @@ -1 +1 @@ -{"test_in_progress": "False", "language": "python", "template": "D:\\Alok country\\Apun coder banega\\Projects\\CP-CLI\\test\\code.cpp", "proxy": ""} \ No newline at end of file +{"user": {"firstname": "", "lastname": "", "rating": "", "contri": "", "rank": "", "maxrating": ""}, "test_in_progress": "False", "language": "python", "template": "D:\\Alok country\\Apun coder banega\\Projects\\CP-CLI\\test\\code.cpp", "proxy": ""} diff --git a/src/Config/exceptions/UserNotFound.py b/src/Config/exceptions/UserNotFound.py new file mode 100644 index 0000000..0f31178 --- /dev/null +++ b/src/Config/exceptions/UserNotFound.py @@ -0,0 +1,3 @@ +class UsernameNotFound(Exception): + def __init__(self, userName) -> None: + super().__init__(f'{userName} not found.') \ No newline at end of file diff --git a/src/Config/exceptions/UserNotSet.py b/src/Config/exceptions/UserNotSet.py new file mode 100644 index 0000000..75ba67c --- /dev/null +++ b/src/Config/exceptions/UserNotSet.py @@ -0,0 +1,3 @@ +class UserNotSet(Exception): + def __init__(self) -> None: + super().__init__("User not set yet") diff --git a/src/Config/exceptions/__init__.py b/src/Config/exceptions/__init__.py new file mode 100644 index 0000000..e69de29