Skip to content

Commit c0379b0

Browse files
authored
Merge pull request #166 from tcdent/env-comments
Comment out placeholder environment variables
2 parents b8ef782 + b75b283 commit c0379b0

File tree

13 files changed

+81
-32
lines changed

13 files changed

+81
-32
lines changed

agentstack/generation/files.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional, Union
22
import os, sys
3+
import string
34
from pathlib import Path
45

56
if sys.version_info >= (3, 11):
@@ -9,18 +10,23 @@
910
from agentstack import conf
1011

1112

12-
ENV_FILEMANE = ".env"
13+
ENV_FILENAME = ".env"
1314
PYPROJECT_FILENAME = "pyproject.toml"
1415

1516

1617
class EnvFile:
1718
"""
1819
Interface for interacting with the .env file inside a project directory.
1920
Unlike the ConfigFile, we do not re-write the entire file on every change,
20-
and instead just append new lines to the end of the file. This preseres
21+
and instead just append new lines to the end of the file. This preserves
2122
comments and other formatting that the user may have added and prevents
2223
opportunities for data loss.
2324
25+
If the value of a variable is None, it will be commented out when it is written
26+
to the file. This gives the user a suggestion, but doesn't override values that
27+
may have been set by the user via other means (for example, but the user's shell).
28+
Commented variable are not re-parsed when the file is read.
29+
2430
`path` is the directory where the .env file is located. Defaults to the
2531
current working directory.
2632
`filename` is the name of the .env file, defaults to '.env'.
@@ -34,47 +40,59 @@ class EnvFile:
3440

3541
variables: dict[str, str]
3642

37-
def __init__(self, filename: str = ENV_FILEMANE):
43+
def __init__(self, filename: str = ENV_FILENAME):
3844
self._filename = filename
3945
self.read()
4046

41-
def __getitem__(self, key):
47+
def __getitem__(self, key) -> str:
4248
return self.variables[key]
4349

44-
def __setitem__(self, key, value):
50+
def __setitem__(self, key, value) -> None:
4551
if key in self.variables:
4652
raise ValueError("EnvFile does not allow overwriting values.")
4753
self.append_if_new(key, value)
4854

4955
def __contains__(self, key) -> bool:
5056
return key in self.variables
5157

52-
def append_if_new(self, key, value):
58+
def append_if_new(self, key, value) -> None:
59+
"""Setting a non-existent key will append it to the end of the file."""
5360
if key not in self.variables:
5461
self.variables[key] = value
5562
self._new_variables[key] = value
5663

57-
def read(self):
58-
def parse_line(line):
64+
def read(self) -> None:
65+
def parse_line(line) -> tuple[str, str]:
66+
"""
67+
Parse a line from the .env file.
68+
Pairs are split on the first '=' character, and stripped of whitespace & quotes.
69+
Only the last occurrence of a variable is stored.
70+
"""
5971
key, value = line.split('=')
60-
return key.strip(), value.strip()
72+
return key.strip(), value.strip(string.whitespace + '"')
6173

6274
if os.path.exists(conf.PATH / self._filename):
6375
with open(conf.PATH / self._filename, 'r') as f:
64-
self.variables = dict([parse_line(line) for line in f.readlines() if '=' in line])
76+
self.variables = dict(
77+
[parse_line(line) for line in f.readlines() if '=' in line and not line.startswith('#')]
78+
)
6579
else:
6680
self.variables = {}
6781
self._new_variables = {}
6882

69-
def write(self):
83+
def write(self) -> None:
84+
"""Append new variables to the end of the file."""
7085
with open(conf.PATH / self._filename, 'a') as f:
7186
for key, value in self._new_variables.items():
72-
f.write(f"\n{key}={value}")
87+
if value is None:
88+
f.write(f'\n#{key}=""') # comment-out empty values
89+
else:
90+
f.write(f'\n{key}={value}')
7391

7492
def __enter__(self) -> 'EnvFile':
7593
return self
7694

77-
def __exit__(self, *args):
95+
def __exit__(self, *args) -> None:
7896
self.write()
7997

8098

agentstack/tools/agent_connect.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"category": "network-protocols",
55
"packages": ["agent-connect"],
66
"env": {
7-
"HOST_DOMAIN": "...",
7+
"HOST_DOMAIN": null,
88
"HOST_PORT": 80,
99
"HOST_WS_PATH": "/ws",
10-
"DID_DOCUMENT_PATH": "...",
11-
"SSL_CERT_PATH": "...",
12-
"SSL_KEY_PATH": "..."
10+
"DID_DOCUMENT_PATH": null,
11+
"SSL_CERT_PATH": null,
12+
"SSL_KEY_PATH": null
1313
},
1414
"tools": ["send_message", "receive_message"]
1515
}

agentstack/tools/browserbase.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"category": "browsing",
55
"packages": ["browserbase", "playwright"],
66
"env": {
7-
"BROWSERBASE_API_KEY": "...",
8-
"BROWSERBASE_PROJECT_ID": "..."
7+
"BROWSERBASE_API_KEY": null,
8+
"BROWSERBASE_PROJECT_ID": null
99
},
1010
"tools": ["browserbase"],
1111
"cta": "Create an API key at https://www.browserbase.com/"

agentstack/tools/composio.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"category": "unified-apis",
55
"packages": ["composio-crewai"],
66
"env": {
7-
"COMPOSIO_API_KEY": "..."
7+
"COMPOSIO_API_KEY": null
88
},
99
"tools": ["composio_tools"],
1010
"tools_bundled": true,

agentstack/tools/exa.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"category": "web-retrieval",
55
"packages": ["exa_py"],
66
"env": {
7-
"EXA_API_KEY": "..."
7+
"EXA_API_KEY": null
88
},
99
"tools": ["search_and_contents"],
1010
"cta": "Get your Exa API key at https://dashboard.exa.ai/api-keys"

agentstack/tools/firecrawl.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"category": "browsing",
55
"packages": ["firecrawl-py"],
66
"env": {
7-
"FIRECRAWL_API_KEY": "..."
7+
"FIRECRAWL_API_KEY": null
88
},
99
"tools": ["web_scrape", "web_crawl", "retrieve_web_crawl"],
1010
"cta": "Create an API key at https://www.firecrawl.dev/"

agentstack/tools/ftp.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"category": "computer-control",
44
"packages": [],
55
"env": {
6-
"FTP_HOST": "...",
7-
"FTP_USER": "...",
8-
"FTP_PASSWORD": "..."
6+
"FTP_HOST": null,
7+
"FTP_USER": null,
8+
"FTP_PASSWORD": null
99
},
1010
"tools": ["upload_files"],
1111
"cta": "Be sure to add your FTP credentials to .env"

agentstack/tools/mem0.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"category": "storage",
55
"packages": ["mem0ai"],
66
"env": {
7-
"MEM0_API_KEY": "..."
7+
"MEM0_API_KEY": null
88
},
99
"tools": ["write_to_memory", "read_from_memory"],
1010
"cta": "Create your mem0 API key at https://mem0.ai/"

agentstack/tools/neon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"url": "https://github.com/neondatabase/neon",
55
"packages": ["neon-api", "psycopg2-binary"],
66
"env": {
7-
"NEON_API_KEY": "..."
7+
"NEON_API_KEY": null
88
},
99
"tools": ["create_database", "execute_sql_ddl", "run_sql_query"],
1010
"cta": "Create an API key at https://www.neon.tech"

agentstack/tools/perplexity.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"url": "https://perplexity.ai",
44
"category": "search",
55
"env": {
6-
"PERPLEXITY_API_KEY": "..."
6+
"PERPLEXITY_API_KEY": null
77
},
88
"tools": ["query_perplexity"]
99
}

0 commit comments

Comments
 (0)