Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8249846
Link your codeforces profile with cli
ashutoshsuthar2020 Oct 10, 2022
ea7f49f
added necessary chancges to link wiith cofeforces profile
ashutoshsuthar2020 Oct 11, 2022
7d5bab2
Merge branch 'ContriHUB:main' into main
ashutoshsuthar2020 Oct 11, 2022
7c51d6b
made necessary changes to Config.py
ashutoshsuthar2020 Oct 11, 2022
bd767dc
Update config.json
ashutoshsuthar2020 Oct 11, 2022
7bec931
Update COMMANDS.md
ashutoshsuthar2020 Oct 11, 2022
44c8c1b
Update Config.py
ashutoshsuthar2020 Oct 12, 2022
a931d45
Update Config.py
ashutoshsuthar2020 Oct 13, 2022
985d911
Update config.py
ashutoshsuthar2020 Oct 13, 2022
fb226bd
added correct implemenatation of exception
ashutoshsuthar2020 Oct 14, 2022
7176f95
organised exception in config.py
ashutoshsuthar2020 Oct 14, 2022
51a66e9
UserNotSet exception class created
ashutoshsuthar2020 Oct 16, 2022
1c10403
api url varible formed
ashutoshsuthar2020 Oct 16, 2022
e51e877
modified UserNotSet.py and UserNotFound.py
ashutoshsuthar2020 Oct 18, 2022
6443bb6
fixed the error on no proxy
ashutoshsuthar2020 Oct 18, 2022
bdd40d8
Update UserNotSet.py
ashutoshsuthar2020 Oct 20, 2022
5bf67ed
Update config.py
ashutoshsuthar2020 Oct 20, 2022
d06f904
Update config.py
ashutoshsuthar2020 Oct 20, 2022
993c76f
Update Config.py
ashutoshsuthar2020 Oct 21, 2022
c44e8d5
Update Config.py
ashutoshsuthar2020 Oct 21, 2022
c029516
Update config.json
ashutoshsuthar2020 Oct 21, 2022
a12898e
Update Config.py
ashutoshsuthar2020 Oct 22, 2022
22a768f
Update config.py
ashutoshsuthar2020 Oct 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
This will change the runtime environment.
Currently value of `<language>` can only be 'python' or 'cpp' are supported

* ```
ecp config set-user <username>
```
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 <username:password@ip:port>
```
Expand Down Expand Up @@ -67,4 +85,4 @@
* Get remaining time duration in a test using:
```
ecp test time
```
```
1 change: 0 additions & 1 deletion src/Cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .runner import run
from .config import config
from .test import test

@click.group()
def entry_point():
pass
Expand Down
21 changes: 20 additions & 1 deletion src/Cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment on lines +80 to +82
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not printing anything

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected it

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)
config.add_command(remove_proxy)
config.add_command(set_user)
config.add_command(get_user)
38 changes: 35 additions & 3 deletions src/Config/Config.py
Original file line number Diff line number Diff line change
@@ -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
'''
Expand Down Expand Up @@ -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):
Expand All @@ -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]))
2 changes: 1 addition & 1 deletion src/Config/config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"test_in_progress": "False", "language": "python", "template": "D:\\Alok country\\Apun coder banega\\Projects\\CP-CLI\\test\\code.cpp", "proxy": ""}
{"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": ""}
3 changes: 3 additions & 0 deletions src/Config/exceptions/UserNotFound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class UsernameNotFound(Exception):
def __init__(self, userName) -> None:
super().__init__(f'{userName} not found.')
3 changes: 3 additions & 0 deletions src/Config/exceptions/UserNotSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class UserNotSet(Exception):
def __init__(self) -> None:
super().__init__("User not set yet")
Empty file.