MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / EnvManager

Class EnvManager

env_manager.py:18–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16 logger.propagate = False
17
18class EnvManager:
19 def __init__(self, project_root=None):
20 """
21 Initializes the EnvManager.
22 It determines the project root and the path to the .env file.
23 """
24 if project_root:
25 self.project_root = project_root
26 else:
27 # The script is in the project root, so just use its directory
28 self.project_root = os.path.dirname(os.path.abspath(__file__))
29
30 self.env_file_path = os.path.join(self.project_root, '.env')
31 logger.info(f"Project root identified as: {self.project_root}")
32 logger.info(f".env file path set to: {self.env_file_path}")
33
34 # ------------------------------------------------------------------
35 # Generic helpers for multi-key .env management
36 # ------------------------------------------------------------------
37
38 def _read_env_dict(self):
39 """Read all key=value pairs from the .env file into a dict."""
40 env = {}
41 if os.path.exists(self.env_file_path):
42 with open(self.env_file_path, 'r') as f:
43 for line in f:
44 line = line.strip()
45 if line and not line.startswith('#') and '=' in line:
46 key, value = line.split('=', 1)
47 env[key] = value
48 return env
49
50 def _write_env_dict(self, env):
51 """Persist a full dict back to the .env file."""
52 with open(self.env_file_path, 'w') as f:
53 for key, value in env.items():
54 f.write(f'{key}={value}\n')
55
56 def get_env_key(self, key):
57 """Return the value of *key* from os.environ or the .env file."""
58 val = os.environ.get(key)
59 if val:
60 return val
61 env = self._read_env_dict()
62 return env.get(key)
63
64 def set_env_key(self, key, value):
65 """Set a single key in the .env file (preserves other keys)."""
66 env = self._read_env_dict()
67 env[key] = value
68 self._write_env_dict(env)
69 os.environ[key] = value
70
71 def delete_env_key(self, key):
72 """Remove a single key from the .env file."""
73 env = self._read_env_dict()
74 if key in env:
75 del env[key]

Callers 11

__init__Method · 0.90
get_ai_tokenFunction · 0.90
save_ai_tokenFunction · 0.90
remove_ai_tokenFunction · 0.90
get_pushover_keysFunction · 0.90
save_pushover_keysFunction · 0.90
remove_pushover_keysFunction · 0.90
test_env_managerFunction · 0.90
_get_keysMethod · 0.90
test_env_managerFunction · 0.90

Calls

no outgoing calls

Tested by 2

test_env_managerFunction · 0.72
test_env_managerFunction · 0.72