@@ -12,6 +12,7 @@ def __init__(self):
12
12
super ().__init__ ()
13
13
self .title_slug = None
14
14
self .path = None
15
+ self .command = None
15
16
self .runcode = None
16
17
self .submission_id = None
17
18
@@ -34,19 +35,30 @@ def runcode_check_url(self):
34
35
return f"https://leetcode.com/submissions/detail/{ self .runcode } /check/"
35
36
36
37
def parse_args (self , args ):
37
- self .title_slug = args .question_slug
38
+ import os
39
+ self .command = args .command
38
40
self .path = args .path
39
41
42
+
43
+ # Get the info from filename : 1234.title-slug.py
44
+ filename = os .path .basename (self .path )
45
+ self .title_slug = filename .split ('.' )[1 ]
46
+
47
+
40
48
# check if such slug exists
41
49
ProblemInfo .lookup_slug (self .title_slug )
42
50
43
51
def _execute (self , args ):
44
52
try :
45
- with Loader ('Uploading submission...' , '' ):
46
- self .parse_args (args )
47
- self .execute_submission (self .title_slug , self .path )
48
-
49
- self .show_submission_info (self .submit_response )
53
+ self .parse_args (args )
54
+ if self .command == 'submit' :
55
+ with Loader ('Uploading submission...' , '' ):
56
+ submit_response = self .execute_submission (self .title_slug , self .path )
57
+ self .show_submission_info (submit_response )
58
+ elif self .command == 'check' :
59
+ with Loader ('Checking submission...' , '' ):
60
+ check_response = self .execute_check (self .title_slug , self .path )
61
+ self .show_check_info (check_response )
50
62
except Exception as e :
51
63
console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
52
64
@@ -81,16 +93,19 @@ def execute_check(self, title_slug, filename):
81
93
response = requests .get (url = self .runcode_check_url ,
82
94
headers = self .config .headers ,
83
95
cookies = self .config .cookies )
84
- self .show_check_info (response .json ())
96
+ return response .json ()
97
+
85
98
86
99
def show_check_info (self , response ):
87
100
if response .get ('run_success' ):
88
- print (f"Runtime: { response .get ('status_runtime' )} " )
89
- print (f"Answer: { response .get ('correct_answer' )} " )
90
- print (f"Expected: { response .get ('expected_code_answer' )} " )
91
- print (f"Got answer: { response .get ('code_answer' )} " )
101
+ console .print ("\n [bold green]✓ Check Passed[/bold green]\n " )
102
+ console .print (f"[bold]Runtime:[/bold] { response .get ('status_runtime' )} " )
103
+ console .print (f"[bold]Answer:[/bold] { response .get ('correct_answer' )} " )
104
+ console .print (f"[bold]Expected:[/bold] { response .get ('expected_code_answer' )} " )
105
+ console .print (f"[bold]Got answer:[/bold] { response .get ('code_answer' )} " )
92
106
else :
93
- print (f"Exception: { response .get ('status_msg' )} " )
107
+ console .print ("\n [bold red]✗ Check Failed[/bold red]\n " )
108
+ console .print (f"[bold]Exception:[/bold] { response .get ('status_msg' )} " )
94
109
95
110
def execute_submission (self , title_slug , filename ):
96
111
# In similar way execute clicking submit button on the leetcode website
@@ -114,24 +129,24 @@ def execute_submission(self, title_slug, filename):
114
129
response = requests .get (url = self .submit_check_url ,
115
130
headers = self .config .headers ,
116
131
cookies = self .config .cookies )
117
- self . submit_response = response .json ()
132
+ return response .json ()
118
133
119
134
def show_submission_info (self , response ):
120
135
if response .get ('run_success' ):
121
136
status_msg = response .get ('status_msg' )
122
137
if status_msg == 'Accepted' : # If the solution is accepted
123
- print (f"Status: [bold green]{ status_msg } [/bold green] :tada:" )
124
- print (f"Passed { response .get ('total_correct' )} /{ response .get ('total_testcases' )} test cases -> { response .get ('status_runtime' )} " )
138
+ console . print (f"\n [bold green]✓ Submission Passed [/bold green] :tada:" )
139
+ console . print (f"Passed { response .get ('total_correct' )} /{ response .get ('total_testcases' )} test cases in { response .get ('status_runtime' )} " )
125
140
126
141
perc_evalutaion = SubmitEvaluation (f"{ response .get ('runtime_percentile' ):.2f} " , f"{ response .get ('memory_percentile' ):.2f} " )
127
- print (perc_evalutaion )
142
+ console . print (perc_evalutaion )
128
143
elif status_msg == 'Wrong Answer' : # If the solution is wrong
129
- print (f"Status: [bold red]{ status_msg } [/bold red] :tada:" )
130
- print (f"Passed { response .get ('total_correct' )} /{ response .get ('total_testcases' )} testcases" )
144
+ console . print (f"\n [bold red]✗ Submission Failed [/bold red] :tada:" )
145
+ console . print (f"Passed { response .get ('total_correct' )} /{ response .get ('total_testcases' )} testcases" )
131
146
else :
132
147
if response .get ('status_msg' ) == 'Time Limit Exceeded' :
133
- print (f"Status: [bold red]{ response . get ( 'status_msg' ) } [/bold red] :alarm_clock:" )
134
- print (f"Passed { response .get ('total_correct' )} /{ response .get ('total_testcases' )} testcases" )
148
+ console . print (f"\n [bold red]✗ Submission Failed [/bold red] :alarm_clock:" )
149
+ console . print (f"Passed { response .get ('total_correct' )} /{ response .get ('total_testcases' )} testcases" )
135
150
elif response .get ('status_msg' ) == 'Runtime Error' :
136
- print (f"Status: [bold red]{ response . get ( 'status_msg' ) } [/bold red]" )
137
- print (f"{ response .get ('runtime_error' )} " )
151
+ console . print (f"\n [bold red]✗ Submission Failed [/bold red]" )
152
+ console . print (f"{ response .get ('runtime_error' )} " )
0 commit comments