@@ -58,71 +58,43 @@ def configure(
5858 existing_config = get_existing_config ()
5959
6060 # Normalize no_metrics to bool
61- input_no_metrics = no_metrics .lower () if no_metrics else None
62- if input_no_metrics == 'yes' :
63- no_metrics_bool = True
64- elif input_no_metrics == 'no' :
65- no_metrics_bool = False
61+ if no_metrics is not None :
62+ no_metrics_bool = no_metrics .lower () == 'yes'
6663 else :
67- no_metrics_bool = bool ( existing_config .get ("no_metrics" , "false" ) == "true" )
64+ no_metrics_bool = existing_config .get ("no_metrics" , "false" ) == "true"
6865
69- # Default token to existing token if None provided
66+ # Fetch existing token if None provided
7067 if token is None :
7168 token = existing_config .get ("token" , "" )
7269
73- # Provide default logging level for NOTICE if not in LEVELS
74- notice_level = LEVELS .get ("NOTICE" , 20 ) # Assuming 20 is a reasonable default
75-
76- if not token and not no_metrics :
77- notice_message = """
78- You can find your token at https://hub.guardrailsai.com/tokens
79- """
80- logger .log (level = notice_level , msg = notice_message )
81-
82- try :
83- # If token or no_metrics was updated, save the configuration
84- token_was_updated = token and token != existing_config .get ("token" , "" )
85- no_metrics_was_updated = no_metrics_bool != (existing_config .get ("no_metrics" , "false" ) == "true" )
86-
87- if token_was_updated or no_metrics_was_updated :
88- logger .info ("Configuring..." )
89- save_configuration_file (token , no_metrics_bool )
90-
91- # Authenticate with the Hub if token was updated
92- if token_was_updated :
93- logger .info ("Validating credentials..." )
94- get_auth ()
95- success_message = """
96- Login successful.
97-
98- Get started by installing our RegexMatch validator:
99- https://hub.guardrailsai.com/validator/guardrails_ai/regex_match
100-
101- You can install it by running:
102- guardrails hub install hub://guardrails/regex_match
103-
104- Find more validators at https://hub.guardrailsai.com
105- """
106- logger .log (level = LEVELS .get ("SUCCESS" , 25 ), msg = success_message ) # Assuming 25 is the SUCCESS level
107- elif not token :
108- print ("No token provided. Skipping authentication." )
70+ # Only save configuration if both token and no_metrics are valid
71+ if token and no_metrics is not None :
72+ save_configuration_file (token , no_metrics_bool )
73+ logger .info ("Configuration saved." )
10974
110- if not (token_was_updated or no_metrics_was_updated ) and not (token and no_metrics ):
111- print ("Existing configuration found. Skipping re-authentication." )
75+ # Authenticate with the Hub if token was updated
76+ if token != existing_config .get ("token" , "" ):
77+ logger .info ("Validating credentials..." )
78+ get_auth ()
79+ success_message = """
80+ Login successful.
11281
113- except AuthenticationError as auth_error :
114- logger .error (auth_error )
115- logger .error (
116- """
117- Check that your token is correct and try again.
82+ Get started by installing our RegexMatch validator:
83+ https://hub.guardrailsai.com/validator/guardrails_ai/regex_match
11884
119- If you don't have your token credentials you can find them here:
85+ You can install it by running:
86+ guardrails hub install hub://guardrails/regex_match
12087
121- https://hub.guardrailsai.com/tokens
88+ Find more validators at https://hub.guardrailsai.com
12289 """
123- )
124- sys .exit (1 )
125- except Exception as e :
126- logger .error ("An unexpected error occurred!" )
127- logger .error (e )
128- sys .exit (1 )
90+ logger .log (level = LEVELS .get ("SUCCESS" , 25 ), msg = success_message ) # Assuming 25 is the SUCCESS level
91+
92+ else :
93+ if not token :
94+ print ("No token provided. Skipping authentication." )
95+ if no_metrics is None :
96+ print ("No metrics preference provided. Skipping configuration update." )
97+
98+ # Log an information message if neither token nor no_metrics provided
99+ if not token and no_metrics is None :
100+ logger .info ("No updates to configuration required." )
0 commit comments