11import logging
2- import os
32from dataclasses import dataclass
4- from os .path import expanduser
53from typing import Optional
4+ from typing_extensions import deprecated
65
7- from guardrails .classes .generic .serializeable import Serializeable
6+ from guardrails .classes .generic .serializeable import SerializeableJSONEncoder
7+ from guardrails .classes .rc import RC
88
99BOOL_CONFIGS = set (["no_metrics" , "enable_metrics" , "use_remote_inferencing" ])
1010
1111
12+ @deprecated (
13+ (
14+ "The `Credentials` class is deprecated and will be removed in version 0.6.x."
15+ " Use the `RC` class instead."
16+ ),
17+ category = DeprecationWarning ,
18+ )
1219@dataclass
13- class Credentials (Serializeable ):
14- id : Optional [str ] = None
15- token : Optional [str ] = None
20+ class Credentials (RC ):
1621 no_metrics : Optional [bool ] = False
17- enable_metrics : Optional [bool ] = True
18- use_remote_inferencing : Optional [bool ] = True
1922
2023 @staticmethod
2124 def _to_bool (value : str ) -> Optional [bool ]:
@@ -27,51 +30,16 @@ def _to_bool(value: str) -> Optional[bool]:
2730
2831 @staticmethod
2932 def has_rc_file () -> bool :
30- home = expanduser ("~" )
31- guardrails_rc = os .path .join (home , ".guardrailsrc" )
32- return os .path .exists (guardrails_rc )
33+ return RC .exists ()
3334
3435 @staticmethod
3536 def from_rc_file (logger : Optional [logging .Logger ] = None ) -> "Credentials" :
36- try :
37- if not logger :
38- logger = logging .getLogger ()
39- home = expanduser ("~" )
40- guardrails_rc = os .path .join (home , ".guardrailsrc" )
41- with open (guardrails_rc , encoding = "utf-8" ) as rc_file :
42- lines = rc_file .readlines ()
43- filtered_lines = list (filter (lambda l : l .strip (), lines ))
44- creds = {}
45- for line in filtered_lines :
46- line_content = line .split ("=" , 1 )
47- if len (line_content ) != 2 :
48- logger .warning (
49- """
50- Invalid line found in .guardrailsrc file!
51- All lines in this file should follow the format: key=value
52- Ignoring line contents...
53- """
54- )
55- logger .debug (f".guardrailsrc file location: { guardrails_rc } " )
56- else :
57- key , value = line_content
58- key = key .strip ()
59- value = value .strip ()
60- if key in BOOL_CONFIGS :
61- value = Credentials ._to_bool (value )
62-
63- creds [key ] = value
64-
65- rc_file .close ()
66-
67- # backfill no_metrics, handle defaults
68- # remove in 0.5.0
69- no_metrics_val = creds .pop ("no_metrics" , None )
70- if no_metrics_val is not None and creds .get ("enable_metrics" ) is None :
71- creds ["enable_metrics" ] = not no_metrics_val
72-
73- creds_dict = Credentials .from_dict (creds )
74- return creds_dict
75-
76- except FileNotFoundError :
77- return Credentials .from_dict ({}) # type: ignore
37+ rc = RC .load (logger )
38+ return Credentials (
39+ id = rc .id ,
40+ token = rc .token ,
41+ enable_metrics = rc .enable_metrics ,
42+ use_remote_inferencing = rc .use_remote_inferencing ,
43+ no_metrics = (not rc .enable_metrics ),
44+ encoder = SerializeableJSONEncoder (),
45+ )
0 commit comments