Initial upload: man-card CLI tool with PDF/PNG generation, templates, and tests
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
53
man_card/config.py
Normal file
53
man_card/config.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"""Configuration management module."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Config:
|
||||||
|
"""Application configuration."""
|
||||||
|
default_format: str = "pdf"
|
||||||
|
default_dpi: int = 150
|
||||||
|
default_template: str = "default"
|
||||||
|
page_size: str = "a4"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls) -> "Config":
|
||||||
|
"""Load configuration from environment variables."""
|
||||||
|
return cls(
|
||||||
|
default_format=os.getenv("MANCARD_DEFAULT_FORMAT", "pdf"),
|
||||||
|
default_dpi=int(os.getenv("MANCARD_DEFAULT_DPI", "150")),
|
||||||
|
default_template=os.getenv("MANCARD_DEFAULT_TEMPLATE", "default"),
|
||||||
|
page_size=os.getenv("MANCARD_PAGE_SIZE", "a4")
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_dotenv(cls, path: Optional[str] = None) -> "Config":
|
||||||
|
"""Load configuration from .env file."""
|
||||||
|
try:
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
if path is None:
|
||||||
|
path = Path(__file__).parent.parent / ".env"
|
||||||
|
elif isinstance(path, str):
|
||||||
|
path = Path(path)
|
||||||
|
|
||||||
|
if path.exists():
|
||||||
|
load_dotenv(path)
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return cls.from_env()
|
||||||
|
|
||||||
|
def merge(self, **overrides) -> "Config":
|
||||||
|
"""Create a new config with overridden values."""
|
||||||
|
data = {
|
||||||
|
"default_format": self.default_format,
|
||||||
|
"default_dpi": self.default_dpi,
|
||||||
|
"default_template": self.default_template,
|
||||||
|
"page_size": self.page_size
|
||||||
|
}
|
||||||
|
data.update({k: v for k, v in overrides.items() if v is not None})
|
||||||
|
return cls(**data)
|
||||||
Reference in New Issue
Block a user