Source code for greening.greening_config

from pathlib import Path
import yaml

from greening.helpers import get_git_config_username, get_git_config_email, generate_git_section

[docs]class GreeningConfig: DEFAULT_YAML = f"""\ # Project metadata project_name: My Greening Project project_slug: my_greening_project author_name: Your Name email: {get_git_config_email() or "your@email.com"} github_username: {get_git_config_username() or "your-github-username"} # Optional GitHub integration # To enable GitHub repo creation, set GITHUB_TOKEN in your environment. # This token must have repo scope. # For more info: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token {generate_git_section()} # Create a virtual environment using venv in the root of the project venv: create: false # Whether to create a virtual environment python: python3 # Python interpreter to use (optional) # Auto-generate and deploy a Jekyll site on GitHub Pages github_pages: enabled: true # google_analytics: G-XXXXXXXXXX """
[docs] def __init__(self, path: Path = Path.cwd() / "greening.yaml"): self.path = path self.data = {} if self.path.exists(): with self.path.open("r") as f: self.data = yaml.safe_load(f) or {} # Set derived fields project_slug = self.path.parent.name self.data.setdefault("project_name", project_slug.replace("_", " ").title()) self.data.setdefault("project_slug", project_slug)
[docs] def get(self, key, default=None): return self.data.get(key, default)
[docs] def as_cookiecutter_context(self): return self.data
[docs] def write_default(self): if self.path.exists(): print("⚠️ greening.yaml already exists.") return self.path.write_text(self.DEFAULT_YAML) print(f"✅ Created default greening.yaml at {self.path}")
@property def github_pages_enabled(self) -> bool: return self.data.get("github_pages", {}).get("enabled", False) @property def docs_enabled(self): return self.data.get("docs", {}).get("init", False)
[docs] def to_cookiecutter_context(self): return { "project_name": self.data.get("project_name"), "project_slug": self.data.get("project_slug"), "github_username": self.data.get("github_username"), "author_name": self.data.get("author_name"), "email": self.data.get("email"), "venv_create": str(self.data.get("venv", {}).get("create", False)).lower(), "python": self.data.get("venv", {}).get("python", "python3"), "google_analytics": self.data.get("github_pages", {}).get("google_analytics", "G-XXXXXXXXXX") }