Skip to content

Commit 5b6c552

Browse files
Enhance CLI output with color coding for user activity and error messages; improve input validation for user IDs
1 parent ccc1dfb commit 5b6c552

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

src/cli/cli_handler.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def draw_graph(input_file, output_file):
272272
graph = GraphRepresentation.build_graph(input_file)
273273
analyzer:NetworkAnalysis = NetworkAnalysis(graph)
274274
GraphVisualizer(graph).visualize(output_file, most_active_users=analyzer.get_most_active_user(), most_influential_users=analyzer.get_most_influencer_user())
275+
print(f"{Fore.GREEN}Graph visualization saved to {output_file}")
275276
except Exception as e:
276277
print(f"{Fore.RED}Error: did not produce an output file.")
277278

@@ -286,7 +287,7 @@ def most_active_user(input_file):
286287
try:
287288
graph = GraphRepresentation.build_graph(input_file)
288289
user = NetworkAnalysis(graph).get_most_active_user()
289-
print(f"Most active user:",*user)
290+
print(f"{Fore.GREEN}Most active user(s):",*user)
290291
except Exception as e:
291292
print(f"{Fore.RED}Error finding most active user: {e}")
292293

@@ -301,14 +302,24 @@ def most_influencer_user(input_file):
301302
try:
302303
graph = GraphRepresentation.build_graph(input_file)
303304
user = NetworkAnalysis(graph).get_most_influencer_user()
304-
print(f"Most active user:",*user)
305+
print(f"{Fore.GREEN}Most influential user(s):",*user)
305306
except Exception as e:
306307
print(f"{Fore.RED}Error finding most influential user: {e}")
307308

308-
def mutual_users(input_file, ids):
309+
def mutual_users(input_file, ids:str):
309310
if os.path.splitext(input_file)[1] != ".xml" and os.path.splitext(input_file)[1]:
310311
print(f"{Fore.RED}Error: Invalid input file. Please provide a valid XML file.")
311312
return
313+
try:
314+
ids = ids.split(",")
315+
except ValueError:
316+
print(f"{Fore.RED}Error: Invalid user IDs. Please provide a comma-separated list of integer user IDs.")
317+
return
318+
try:
319+
ids = list(map(int, ids))
320+
except ValueError:
321+
print(f"{Fore.RED}Error: Invalid user IDs. Please provide a comma-separated list of integer user IDs.")
322+
return
312323
print(f"{Style.BRIGHT}{Fore.CYAN}Finding mutual users for IDs {ids} in {input_file}{Style.RESET_ALL}")
313324
if not os.path.splitext(input_file)[1]:
314325
input_file = f"{input_file}.xml" # Append .xml if no extension is present
@@ -317,14 +328,19 @@ def mutual_users(input_file, ids):
317328
graph = GraphRepresentation.build_graph(input_file)
318329
analyzer = NetworkAnalysis(graph)
319330
mutuals = analyzer.get_mutual_users(ids)
320-
print(f"Mutual users: {mutuals}")
331+
print(f"{Fore.GREEN}Mutual users: {mutuals}")
321332
except Exception as e:
322333
print(f"{Fore.RED}Error finding mutual users: {e}")
323334

324335
def suggest_users(input_file, user_id):
325336
if os.path.splitext(input_file)[1] != ".xml" and os.path.splitext(input_file)[1]:
326337
print(f"{Fore.RED}Error: Invalid input file. Please provide a valid XML file.")
327338
return
339+
try:
340+
user_id = int(user_id)
341+
except ValueError:
342+
print(f"{Fore.RED}Error: Invalid user ID. Please provide a valid integer user ID.")
343+
return
328344
print(f"{Style.BRIGHT}{Fore.CYAN}Suggesting users for user ID {user_id} in {input_file}{Style.RESET_ALL}")
329345
if not os.path.splitext(input_file)[1]:
330346
input_file = f"{input_file}.xml" # Append .xml if no extension is present
@@ -333,7 +349,7 @@ def suggest_users(input_file, user_id):
333349
graph = GraphRepresentation.build_graph(input_file)
334350
analyzer = NetworkAnalysis(graph)
335351
suggestions = analyzer.get_suggested_users(user_id)
336-
print(f"Suggested users: {suggestions}")
352+
print(f"{Fore.GREEN}Suggested users: {suggestions}")
337353
except Exception as e:
338354
print(f"{Fore.RED}Error suggesting users: {e}")
339355

@@ -446,7 +462,7 @@ def main():
446462
elif args.command == "most_influencer":
447463
most_influencer_user(args.input)
448464
elif args.command == "mutual":
449-
mutual_users(args.input, args.ids.split(","))
465+
mutual_users(args.input, args.ids)
450466
elif args.command == "suggest":
451467
suggest_users(args.input, args.id)
452468
elif args.command == "search":

0 commit comments

Comments
 (0)