Re-upload: CI infrastructure issue resolved, all tests verified passing
This commit is contained in:
54
envschema/generator.py
Normal file
54
envschema/generator.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""Generator for .env.example files from schema definitions."""
|
||||
|
||||
from envschema.schema import Schema
|
||||
|
||||
|
||||
def generate_env_example(schema: Schema, include_descriptions: bool = True) -> str:
|
||||
"""Generate an .env.example file content from a schema.
|
||||
|
||||
Args:
|
||||
schema: The schema to generate from.
|
||||
include_descriptions: Whether to include description comments.
|
||||
|
||||
Returns:
|
||||
Formatted .env.example content.
|
||||
"""
|
||||
lines = []
|
||||
|
||||
lines.append("# Environment Variables Schema")
|
||||
if schema.version:
|
||||
lines.append(f"# Version: {schema.version}")
|
||||
lines.append("")
|
||||
|
||||
for var in schema.envvars:
|
||||
if include_descriptions and var.description:
|
||||
lines.append(f"# {var.description}")
|
||||
|
||||
if var.required:
|
||||
lines.append("# REQUIRED")
|
||||
elif var.default is not None:
|
||||
lines.append(f"# Default: {var.default}")
|
||||
|
||||
if var.required:
|
||||
lines.append(f"{var.name}=")
|
||||
elif var.default is not None:
|
||||
lines.append(f"{var.name}={var.default}")
|
||||
else:
|
||||
lines.append(f"{var.name}=")
|
||||
|
||||
lines.append("")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def generate_env_example_to_file(schema: Schema, output_path: str, include_descriptions: bool = True) -> None:
|
||||
"""Generate and write an .env.example file.
|
||||
|
||||
Args:
|
||||
schema: The schema to generate from.
|
||||
output_path: Path to write the .env.example file.
|
||||
include_descriptions: Whether to include description comments.
|
||||
"""
|
||||
content = generate_env_example(schema, include_descriptions)
|
||||
with open(output_path, "w") as f:
|
||||
f.write(content)
|
||||
Reference in New Issue
Block a user