1414from pathlib import Path
1515from typing import Any , Dict , List , Optional
1616
17+ try :
18+ from getv import EnvStore
19+ _HAS_GETV = True
20+ except ImportError :
21+ _HAS_GETV = False
22+
1723try :
1824 import yaml
1925 YAML_AVAILABLE = True
@@ -131,7 +137,10 @@ def save_config(config: LLMConfig) -> None:
131137
132138
133139def load_env_file (search_paths : Optional [List [Path ]] = None ) -> None :
134- """Load environment variables from .env file."""
140+ """Load environment variables from .env file.
141+
142+ Delegates to getv.EnvStore when available.
143+ """
135144 if search_paths is None :
136145 search_paths = [
137146 Path .cwd () / '.env' ,
@@ -143,20 +152,22 @@ def load_env_file(search_paths: Optional[List[Path]] = None) -> None:
143152 try :
144153 if not path .exists ():
145154 continue
146-
155+ if _HAS_GETV :
156+ store = EnvStore (path , auto_create = False )
157+ for key , value in store .items ():
158+ if key and value and not os .environ .get (key ):
159+ os .environ [key ] = value
160+ return
147161 for line in path .read_text ().splitlines ():
148162 line = line .strip ()
149163 if not line or line .startswith ('#' ) or '=' not in line :
150164 continue
151-
152165 key , value = line .split ('=' , 1 )
153166 key = key .strip ()
154167 value = value .strip ().strip ('"' ).strip ("'" )
155-
156168 if key and value and not os .environ .get (key ):
157169 os .environ [key ] = value
158-
159- return # Stop after first successful load
170+ return
160171 except Exception :
161172 continue
162173
@@ -250,7 +261,10 @@ def get_api_key(provider: str) -> Optional[str]:
250261
251262
252263def set_api_key (provider : str , key : str , env_path : Optional [Path ] = None ) -> None :
253- """Set API key for a provider in .env file."""
264+ """Set API key for a provider in .env file.
265+
266+ Delegates to getv.EnvStore when available.
267+ """
254268 env_var_map = {
255269 'openrouter' : 'OPENROUTER_API_KEY' ,
256270 'openai' : 'OPENAI_API_KEY' ,
@@ -266,28 +280,26 @@ def set_api_key(provider: str, key: str, env_path: Optional[Path] = None) -> Non
266280 if env_path is None :
267281 env_path = Path .cwd () / '.env'
268282
269- # Read existing content
270- existing_lines = []
271- if env_path .exists ():
272- existing_lines = env_path .read_text ().splitlines ()
273-
274- # Update or add the key
275- found = False
276- new_lines = []
277- for line in existing_lines :
278- if line .strip ().startswith (f'{ env_var } =' ):
283+ if _HAS_GETV :
284+ store = EnvStore (env_path )
285+ store .set (env_var , key )
286+ store .save ()
287+ else :
288+ existing_lines = []
289+ if env_path .exists ():
290+ existing_lines = env_path .read_text ().splitlines ()
291+ found = False
292+ new_lines = []
293+ for line in existing_lines :
294+ if line .strip ().startswith (f'{ env_var } =' ):
295+ new_lines .append (f'{ env_var } ={ key } ' )
296+ found = True
297+ else :
298+ new_lines .append (line )
299+ if not found :
279300 new_lines .append (f'{ env_var } ={ key } ' )
280- found = True
281- else :
282- new_lines .append (line )
283-
284- if not found :
285- new_lines .append (f'{ env_var } ={ key } ' )
286-
287- # Write back
288- env_path .write_text ('\n ' .join (new_lines ) + '\n ' )
301+ env_path .write_text ('\n ' .join (new_lines ) + '\n ' )
289302
290- # Also set in environment
291303 os .environ [env_var ] = key
292304
293305
0 commit comments