1
1
#!/usr/bin/env python3
2
- import os
3
2
import argparse
3
+ import enum
4
4
import gettext
5
5
import logging
6
6
import pkg_resources
21
21
22
22
SUBMIT_URL = "https://submit.cs50.io"
23
23
24
+ class LogLevel (enum .IntEnum ):
25
+ DEBUG = logging .DEBUG
26
+ INFO = logging .INFO
27
+ WARNING = logging .WARNING
28
+ ERROR = logging .ERROR
29
+
30
+
31
+ class ColoredFormatter (logging .Formatter ):
32
+ COLORS = {
33
+ "ERROR" : "red" ,
34
+ "WARNING" : "yellow" ,
35
+ "DEBUG" : "cyan" ,
36
+ "INFO" : "magenta" ,
37
+ }
38
+
39
+ def __init__ (self , fmt , use_color = True ):
40
+ super ().__init__ (fmt = fmt )
41
+ self .use_color = use_color
42
+
43
+ def format (self , record ):
44
+ msg = super ().format (record )
45
+ return msg if not self .use_color else termcolor .colored (msg , getattr (record , "color" , self .COLORS .get (record .levelname )))
46
+
24
47
25
48
class Error (Exception ):
26
49
pass
@@ -50,6 +73,30 @@ def check_version():
50
73
"Please upgrade." ))
51
74
52
75
76
+ def setup_logging (level ):
77
+ """
78
+ Sets up logging for lib50.
79
+ level 'info' logs all git commands run to stderr
80
+ level 'debug' logs all git commands and their output to stderr
81
+ """
82
+ logger = logging .getLogger ("lib50" )
83
+
84
+ # Set verbosity level on the lib50 logger
85
+ logger .setLevel (level .upper ())
86
+
87
+ handler = logging .StreamHandler (sys .stderr )
88
+ handler .setFormatter (ColoredFormatter ("(%(levelname)s) %(message)s" , use_color = sys .stderr .isatty ()))
89
+
90
+ # Direct all logs to sys.stderr
91
+ logger .addHandler (handler )
92
+
93
+ # Don't animate the progressbar if LogLevel is either info or debug
94
+ lib50 .ProgressBar .DISABLED = logger .level < LogLevel .WARNING
95
+
96
+ # Show exceptions when debugging
97
+ excepthook .verbose = logger .level == LogLevel .DEBUG
98
+
99
+
53
100
def cprint (text = "" , color = None , on_color = None , attrs = None , ** kwargs ):
54
101
"""Colorizes text (and wraps to terminal's width)."""
55
102
# Assume 80 in case not running in a terminal
@@ -62,7 +109,7 @@ def cprint(text="", color=None, on_color=None, attrs=None, **kwargs):
62
109
color = color , on_color = on_color , attrs = attrs , ** kwargs )
63
110
64
111
65
- def prompt (included , excluded ):
112
+ def prompt (honesty , included , excluded ):
66
113
if included :
67
114
cprint (_ ("Files that will be submitted:" ), "green" )
68
115
for file in included :
@@ -76,16 +123,33 @@ def prompt(included, excluded):
76
123
for other in excluded :
77
124
cprint ("./{}" .format (other ), "yellow" )
78
125
126
+ # If there's no honesty question, continue.
127
+ if not honesty :
128
+ return True
129
+
79
130
# Prompt for honesty
80
131
try :
81
- answer = input (_ ("Keeping in mind the course's policy on academic honesty, "
82
- "are you sure you want to submit these files (yes/no)? " ))
132
+ # Show default message
133
+ if honesty == True :
134
+ honesty_question = _ (
135
+ "Keeping in mind the course's policy on academic honesty, "
136
+ "are you sure you want to submit these files (yes/no)? "
137
+ )
138
+ # If a custom message is configured, show that instead
139
+ else :
140
+ honesty_question = str (honesty )
141
+
142
+ # Get the user's answer
143
+ answer = input (honesty_question )
83
144
except EOFError :
84
145
answer = None
85
146
print ()
147
+
148
+ # If no answer given, or yes is not given, don't continue
86
149
if not answer or not re .match (f"^\s*(?:{ _ ('y|yes' )} )\s*$" , answer , re .I ):
87
150
return False
88
-
151
+
152
+ # Otherwise, do continue
89
153
return True
90
154
91
155
@@ -102,9 +166,9 @@ def excepthook(type, value, tb):
102
166
103
167
cprint (_ ("Submission cancelled." ), "red" )
104
168
105
-
106
169
excepthook .verbose = True
107
170
171
+
108
172
class LogoutAction (argparse .Action ):
109
173
def __init__ (self , option_strings , dest = argparse .SUPPRESS , default = argparse .SUPPRESS , help = _ ("logout of submit50" )):
110
174
super ().__init__ (option_strings , dest = dest , nargs = 0 , default = default , help = help )
@@ -124,20 +188,29 @@ def main():
124
188
125
189
parser = argparse .ArgumentParser (prog = "submit50" )
126
190
parser .add_argument ("--logout" , action = LogoutAction )
127
- parser .add_argument ("-v" , "--verbose" ,
128
- action = "store_true" ,
129
- help = _ ("show commands being executed" ))
130
- parser .add_argument ("-V" , "--version" , action = "version" , version = f"%(prog)s { __version__ } " )
131
- parser .add_argument ("slug" , help = _ (
132
- "prescribed identifier of work to submit" ))
191
+ parser .add_argument (
192
+ "--log-level" ,
193
+ action = "store" ,
194
+ default = "warning" ,
195
+ choices = [level .name .lower () for level in LogLevel ],
196
+ type = str .lower ,
197
+ help = _ ('warning: displays usage warnings.'
198
+ '\n info: adds all commands run.'
199
+ '\n debug: adds the output of all commands run.' )
200
+ )
201
+ parser .add_argument (
202
+ "-V" , "--version" ,
203
+ action = "version" ,
204
+ version = f"%(prog)s { __version__ } "
205
+ )
206
+ parser .add_argument (
207
+ "slug" ,
208
+ help = _ ("prescribed identifier of work to submit" )
209
+ )
133
210
134
211
args = parser .parse_args ()
135
212
136
- excepthook .verbose = args .verbose
137
- if args .verbose :
138
- logging .basicConfig (level = os .environ .get ("SUBMIT50_LOGLEVEL" , "INFO" ))
139
- # Disable progress bar so it doesn't interfere with log
140
- lib50 .ProgressBar .DISABLED = True
213
+ setup_logging (args .log_level )
141
214
142
215
check_announcements ()
143
216
check_version ()
2 commit comments
DevadassDass commentedon Aug 31, 2021
I am unable see my sccore
rongxin-liu commentedon Aug 31, 2021
Could you please submit an issue and elaborate more on the issues you are having? Which problem set are you working on exactly?