@@ -43,10 +43,7 @@ def __init__(self):
4343
4444 @property
4545 def playbook_path (self ):
46- """
47- Create an environment variable 'DYNAMIC'
48- containing the path of dynamic_playbook.json and returns it
49- """
46+ # Create an environment variable 'DYNAMIC' containing the path of dynamic_playbook.json and returns it
5047 if not os .getenv (self .key ):
5148 if (sys .platform == 'linux' ):
5249 os .environ [self .key ] = os .path .join (self .linux_path , self .file_name )
@@ -137,28 +134,19 @@ def delete_from_playbook(self, stackoverflow_object, question_id):
137134 def display_panel (self ):
138135 playbook_data = self .playbook_content
139136 if (len (playbook_data ['items_stackoverflow' ]) == 0 ):
140- SearchError ("You have no entries in the playbook" ,
141- "Browse and save entries in playbook with 'p' key" )
137+ SearchError ("You have no entries in the playbook" , "Browse and save entries in playbook with 'p' key" )
142138 sys .exit ()
143- # Creates QuestionPanelStackoverflow object
144- # populates its question_data and answer_data and displays it
139+ # Creates QuestionPanelStackoverflow object, populates its question_data and answer_data and displays it
145140 question_panel = QuestionsPanelStackoverflow ()
146141 for item in playbook_data ['items_stackoverflow' ]:
147- question_panel .questions_data .append ( [item ['question_title' ],
148- item ['question_id' ],
149- item ['question_link' ]] )
142+ question_panel .questions_data .append ( [item ['question_title' ], item ['question_id' ], item ['question_link' ]] )
150143 question_panel .answer_data [item ['question_id' ]] = item ['answer_body' ]
151144 question_panel .display_panel ([], playbook = True )
152145
153146class QuestionsPanelStackoverflow ():
154147 def __init__ (self ):
155- # list( list( question_title, question_id, question_link )... )
156- self .questions_data = []
157-
158- # dict( question_id:list( body, link ))
159- # corresponding to self.questions_data
160- self .answer_data = defaultdict (lambda : False )
161-
148+ self .questions_data = [] # list( list( question_title, question_id, question_link )... )
149+ self .answer_data = defaultdict (lambda : False ) # dict( question_id:list( body, link )) corresponding to self.questions_data
162150 self .line_color = "bold red"
163151 self .heading_color = "bold blue"
164152 self .utility = Utility ()
@@ -167,9 +155,8 @@ def __init__(self):
167155 def populate_question_data (self , questions_list ):
168156 """
169157 Function to populate question data property
170- Creates batch request to stackexchange API and to get question
171- details of questions with id in the list. Stores the returned
172- data in the following format:
158+ Creates batch request to stackexchange API and to get question details of
159+ questions with id in the list. Stores the returned data data in the following format:
173160 list( list( question_title, question_link, question_id ) )
174161 """
175162 with console .status ("Getting the questions..." ):
@@ -181,9 +168,7 @@ def populate_question_data(self, questions_list):
181168 SearchError ("Search Failed" , "Try connecting to the internet" )
182169 sys .exit ()
183170 json_ques_data = resp .json ()
184- self .questions_data = [[item ['title' ].replace ('|' ,'' ),
185- item ['question_id' ], item ['link' ]]
186- for item in json_ques_data ["items" ]]
171+ self .questions_data = [[item ['title' ].replace ('|' ,'' ), item ['question_id' ], item ['link' ]] for item in json_ques_data ["items" ]]
187172
188173 def populate_answer_data (self , questions_list ):
189174 """
@@ -204,19 +189,16 @@ def populate_answer_data(self, questions_list):
204189 for item in json_ans_data ["items" ]:
205190 if not (self .answer_data [item ['question_id' ]]):
206191 self .answer_data [item ['question_id' ]] = item ['body_markdown' ]
207- # Sometimes the StackExchange API fails to deliver some answers.
208- # The below code is to fetch them
209- failed_ques_id = [question [1 ] for question in self .questions_data
210- if not (self .answer_data [question [1 ]])]
192+ # Sometimes the StackExchange API fails to deliver some answers. The below code is to fetch them
193+ failed_ques_id = [question [1 ] for question in self .questions_data if not (self .answer_data [question [1 ]])]
211194 if not (len (failed_ques_id ) == 0 ):
212195 self .populate_answer_data (failed_ques_id )
213196
214197 def return_formatted_ans (self , ques_id ):
215198 # This function uses uses Rich Markdown to format answers body.
216199 body_markdown = self .answer_data [int (ques_id )]
217200 body_markdown = str (body_markdown )
218- xml_markup_replacement = [("&" , "&" ), ("<" , "<" ), (">" , ">" ),
219- (""" , "\" " ), ("'" , "\' " ), ("'" , "\' " )]
201+ xml_markup_replacement = [("&" , "&" ), ("<" , "<" ), (">" , ">" ), (""" , "\" " ), ("'" , "\' " ), ("'" , "\' " )]
220202 for convert_from , convert_to in xml_markup_replacement :
221203 body_markdown = body_markdown .replace (convert_from , convert_to )
222204 width = os .get_terminal_size ().columns
@@ -225,32 +207,20 @@ def return_formatted_ans(self, ques_id):
225207 with console .capture () as capture :
226208 console .print (markdown )
227209 highlighted = capture .get ()
228- if not ('UTF' in locale .getlocale ()[1 ]):
229- box_replacement = [("─" , "-" ), ("═" ,"=" ), ("║" ,"|" ), ("│" , "|" ),
230- ('┌' , '+' ), ("└" , "+" ), ("┐" , "+" ), ("┘" , "+" ),
231- ("╔" , "+" ), ("╚" , "+" ), ("╗" ,"+" ), ("╝" , "+" ), ("•" ,"*" )]
210+ if locale .getlocale ()[1 ] != 'UTF-8' :
211+ box_replacement = [("─" , "-" ), ("═" ,"=" ), ("║" ,"|" ), ("│" , "|" ), ('┌' , '+' ), ("└" , "+" ), ("┐" , "+" ), ("┘" , "+" ), ("╔" , "+" ), ("╚" , "+" ), ("╗" ,"+" ), ("╝" , "+" ), ("•" ,"*" )]
232212 for convert_from , convert_to in box_replacement :
233213 highlighted = highlighted .replace (convert_from , convert_to )
234214 return highlighted
235215
236216 def navigate_questions_panel (self , playbook = False ):
237217 # Code for navigating through the question panel
238- if playbook :
239- message = 'Playbook Questions'
240- instructions = ". Press 'd' to delete from playbook"
241- keys = ('enter' , 'd' )
242- else :
243- message = 'Relevant Questions'
244- instructions = ". Press 'p' to save in playbook"
245- keys = ('enter' , 'p' )
218+ (message , instructions , keys ) = ('Playbook Questions' , ". Press 'd' to delete from playbook" , ('enter' , 'd' )) if (playbook ) else ('Relevant Questions' , ". Press 'p' to save in playbook" , ('p' , 'enter' ))
246219 console .rule ('[bold blue] {}' .format (message ), style = "bold red" )
247- console .print ("[yellow] Use arrow keys to navigate. \
248- 'q' or 'Esc' to quit. 'Enter' to open in a browser" +
249- instructions )
220+ console .print ("[yellow] Use arrow keys to navigate. 'q' or 'Esc' to quit. 'Enter' to open in a browser" + instructions )
250221 console .print ()
251222 options = ["|" .join (map (str , question )) for question in self .questions_data ]
252- question_menu = TerminalMenu (options , preview_command = self .return_formatted_ans ,
253- preview_size = 0.75 , accept_keys = keys )
223+ question_menu = TerminalMenu (options , preview_command = self .return_formatted_ans , preview_size = 0.75 , accept_keys = keys )
254224 quitting = False
255225 while not (quitting ):
256226 options_index = question_menu .show ()
@@ -303,15 +273,10 @@ def get_batch_ans_url(self, ques_id_list):
303273
304274 def make_request (self , que , tag : str ):
305275 """
306- This function uses the requests library to make
307- the rest api call to the stackexchange server.
308- :param que: The user questions that servers as
309- a question in the api.
276+ This function uses the requests library to make the rest api call to the stackexchange server.
277+ :param que: The user questions that servers as a question in the api.
310278 :type que: String
311- :param tag: The tags that user wants for searching the relevant
312- answers. For e.g. TypeError might be for multiple
313- languages so is tag is used as "Python" then the
314- api will return answers based on the tags and question.
279+ :param tag: The tags that user wants for searching the relevant answers. For e.g. TypeError might be for multiple languages so is tag is used as "Python" then the api will return answers based on the tags and question.
315280 :type tag: String
316281 :return: Json response from the api call.
317282 :rtype: Json format data
@@ -320,8 +285,7 @@ def make_request(self, que, tag: str):
320285 try :
321286 resp = requests .get (self .__get_search_url (que , tag ))
322287 except :
323- SearchError ("\U0001F613 Search Failed" ,
324- "\U0001F4BB Try connecting to the internet" )
288+ SearchError ("\U0001F613 Search Failed" , "\U0001F4BB Try connecting to the internet" )
325289 sys .exit ()
326290 return resp .json ()
327291
@@ -350,23 +314,19 @@ def get_ans(self, questions_list):
350314 @classmethod
351315 def setCustomKey (self ):
352316 client_id = 20013
353- """
354- scopes possible values:
355- read_inbox - access a user's global inbox
356- no_expiry - access_token's with this scope do not expire
357- write_access - perform write operations as a user
358- private_info - access full history of a user's private
359- actions on the site
360- """
317+
318+ # scopes possible values:
319+ # read_inbox - access a user's global inbox
320+ # no_expiry - access_token's with this scope do not expire
321+ # write_access - perform write operations as a user
322+ # private_info - access full history of a user's private actions on the site
361323 scopes = 'read_inbox'
362324
363325 authorization_url = 'https://stackoverflow.com/oauth/dialog'
364326 redirect_uri = 'https://stackexchange.com/oauth/login_success'
365327
366- # Create an OAuth session and open the auth_url in a browser
367- # for the user to authenticate
368- stackApps = OAuth2Session (client = MobileApplicationClient (client_id = client_id ),
369- scope = scopes , redirect_uri = redirect_uri )
328+ # Create an OAuth session and open the auth_url in a browser for the user to authenticate
329+ stackApps = OAuth2Session (client = MobileApplicationClient (client_id = client_id ), scope = scopes , redirect_uri = redirect_uri )
370330 auth_url , state = stackApps .authorization_url (authorization_url )
371331
372332 # Try to install web drivers for one of these browsers
@@ -380,8 +340,7 @@ def setCustomKey(self):
380340 try :
381341 driver = webdriver .Edge (EdgeChromiumDriverManager ().install ())
382342 except ValueError :
383- print ("You do not have one of these supported browsers: \
384- Chrome, Firefox, Edge" )
343+ print ("You do not have one of these supported browsers: Chrome, Firefox, Edge" )
385344
386345 # Open auth_url in one of the supported browsers
387346 driver .get (auth_url )
0 commit comments