|
| 1 | +Configuration Export |
| 2 | +==================== |
| 3 | + |
| 4 | +Varlord provides functionality to export the current configuration to various file formats, |
| 5 | +making it easy to save, share, or version control your configuration. |
| 6 | + |
| 7 | +Basic Usage |
| 8 | +----------- |
| 9 | + |
| 10 | +After loading your configuration, you can export it to different formats: |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | + :linenos: |
| 14 | +
|
| 15 | + from varlord import Config, sources |
| 16 | + from dataclasses import dataclass, field |
| 17 | +
|
| 18 | + @dataclass |
| 19 | + class AppConfig: |
| 20 | + api_key: str = field() |
| 21 | + host: str = field(default="0.0.0.0") |
| 22 | + port: int = field(default=8000) |
| 23 | +
|
| 24 | + # Create and load config |
| 25 | + cfg = Config( |
| 26 | + model=AppConfig, |
| 27 | + sources=[sources.Env(), sources.CLI()], |
| 28 | + ) |
| 29 | + cfg.handle_cli_commands() |
| 30 | + config = cfg.load() |
| 31 | +
|
| 32 | + # Export to different formats |
| 33 | + cfg.dump_json("config.json") |
| 34 | + cfg.dump_yaml("config.yaml") |
| 35 | + cfg.dump_toml("config.toml") |
| 36 | + cfg.dump_env(".env", prefix="APP_") |
| 37 | +
|
| 38 | +Export Methods |
| 39 | +-------------- |
| 40 | + |
| 41 | +Get Configuration as Dictionary |
| 42 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 43 | + |
| 44 | +Use ``to_dict()`` to get the current configuration as a dictionary without writing to a file: |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + config_dict = cfg.to_dict() |
| 49 | + print(config_dict["host"]) |
| 50 | + print(config_dict["port"]) |
| 51 | +
|
| 52 | +This is useful when you need to programmatically access configuration values or pass them to other functions. |
| 53 | + |
| 54 | +JSON Export |
| 55 | +~~~~~~~~~~~ |
| 56 | + |
| 57 | +Export configuration to JSON format: |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + cfg.dump_json("config.json", indent=4) |
| 62 | +
|
| 63 | +**Parameters:** |
| 64 | +- ``file_path``: Path to output JSON file (str or Path) |
| 65 | +- ``validate``: Whether to validate required fields before export (default: True) |
| 66 | +- ``indent``: JSON indentation level (default: 2) |
| 67 | + |
| 68 | +**Example output:** |
| 69 | + |
| 70 | +.. code-block:: json |
| 71 | +
|
| 72 | + { |
| 73 | + "api_key": "sk-...", |
| 74 | + "host": "0.0.0.0", |
| 75 | + "port": 8000 |
| 76 | + } |
| 77 | +
|
| 78 | +YAML Export |
| 79 | +~~~~~~~~~~~ |
| 80 | + |
| 81 | +Export configuration to YAML format: |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + cfg.dump_yaml("config.yaml", default_flow_style=False) |
| 86 | +
|
| 87 | +**Parameters:** |
| 88 | +- ``file_path``: Path to output YAML file (str or Path) |
| 89 | +- ``validate``: Whether to validate required fields before export (default: True) |
| 90 | +- ``default_flow_style``: Use flow style (default: False, uses block style) |
| 91 | + |
| 92 | +**Dependencies:** Requires ``pyyaml`` package. Install with: ``pip install pyyaml`` |
| 93 | + |
| 94 | +**Example output:** |
| 95 | + |
| 96 | +.. code-block:: yaml |
| 97 | +
|
| 98 | + api_key: sk-... |
| 99 | + host: 0.0.0.0 |
| 100 | + port: 8000 |
| 101 | +
|
| 102 | +TOML Export |
| 103 | +~~~~~~~~~~~ |
| 104 | + |
| 105 | +Export configuration to TOML format: |
| 106 | + |
| 107 | +.. code-block:: python |
| 108 | +
|
| 109 | + cfg.dump_toml("config.toml") |
| 110 | +
|
| 111 | +**Parameters:** |
| 112 | +- ``file_path``: Path to output TOML file (str or Path) |
| 113 | +- ``validate``: Whether to validate required fields before export (default: True) |
| 114 | + |
| 115 | +**Dependencies:** Requires ``tomli-w`` package. Install with: ``pip install tomli-w`` |
| 116 | + |
| 117 | +**Example output:** |
| 118 | + |
| 119 | +.. code-block:: toml |
| 120 | +
|
| 121 | + api_key = "sk-..." |
| 122 | + host = "0.0.0.0" |
| 123 | + port = 8000 |
| 124 | +
|
| 125 | +.env Export |
| 126 | +~~~~~~~~~~~ |
| 127 | + |
| 128 | +Export configuration to .env file format (for environment variables): |
| 129 | + |
| 130 | +.. code-block:: python |
| 131 | +
|
| 132 | + cfg.dump_env( |
| 133 | + ".env", |
| 134 | + prefix="APP_", |
| 135 | + uppercase=True, |
| 136 | + nested_separator="__" |
| 137 | + ) |
| 138 | +
|
| 139 | +**Parameters:** |
| 140 | +- ``file_path``: Path to output .env file (str or Path) |
| 141 | +- ``validate``: Whether to validate required fields before export (default: True) |
| 142 | +- ``prefix``: Optional prefix for all environment variable names (e.g., ``APP_``) |
| 143 | +- ``uppercase``: Convert keys to uppercase (default: True) |
| 144 | +- ``nested_separator``: Separator for nested keys (default: "__") |
| 145 | + |
| 146 | +**Example output:** |
| 147 | + |
| 148 | +.. code-block:: text |
| 149 | +
|
| 150 | + APP_API_KEY=sk-... |
| 151 | + APP_HOST=0.0.0.0 |
| 152 | + APP_PORT=8000 |
| 153 | +
|
| 154 | +Nested Configuration |
| 155 | +-------------------- |
| 156 | + |
| 157 | +All export methods correctly handle nested dataclass structures: |
| 158 | + |
| 159 | +.. code-block:: python |
| 160 | + :linenos: |
| 161 | +
|
| 162 | + @dataclass |
| 163 | + class DBConfig: |
| 164 | + host: str = field(default="localhost") |
| 165 | + port: int = field(default=5432) |
| 166 | +
|
| 167 | + @dataclass |
| 168 | + class AppConfig: |
| 169 | + api_key: str = field() |
| 170 | + db: DBConfig = field(default_factory=lambda: DBConfig()) |
| 171 | +
|
| 172 | + cfg = Config(model=AppConfig, sources=[...]) |
| 173 | + cfg.dump_json("config.json") |
| 174 | +
|
| 175 | +**JSON output:** |
| 176 | + |
| 177 | +.. code-block:: json |
| 178 | +
|
| 179 | + { |
| 180 | + "api_key": "sk-...", |
| 181 | + "db": { |
| 182 | + "host": "localhost", |
| 183 | + "port": 5432 |
| 184 | + } |
| 185 | + } |
| 186 | +
|
| 187 | +**YAML output:** |
| 188 | + |
| 189 | +.. code-block:: yaml |
| 190 | +
|
| 191 | + api_key: sk-... |
| 192 | + db: |
| 193 | + host: localhost |
| 194 | + port: 5432 |
| 195 | +
|
| 196 | +**TOML output:** |
| 197 | + |
| 198 | +.. code-block:: toml |
| 199 | +
|
| 200 | + api_key = "sk-..." |
| 201 | + [db] |
| 202 | + host = "localhost" |
| 203 | + port = 5432 |
| 204 | +
|
| 205 | +**.env output (with nested separator):** |
| 206 | + |
| 207 | +.. code-block:: text |
| 208 | +
|
| 209 | + API_KEY=sk-... |
| 210 | + DB__HOST=localhost |
| 211 | + DB__PORT=5432 |
| 212 | +
|
| 213 | +Use Cases |
| 214 | +--------- |
| 215 | + |
| 216 | +Configuration Backup |
| 217 | +~~~~~~~~~~~~~~~~~~~~~ |
| 218 | + |
| 219 | +Export your current configuration to save a snapshot: |
| 220 | + |
| 221 | +.. code-block:: python |
| 222 | +
|
| 223 | + # Save current config as backup |
| 224 | + cfg.dump_yaml(f"config_backup_{datetime.now().isoformat()}.yaml") |
| 225 | +
|
| 226 | +Configuration Templates |
| 227 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 228 | + |
| 229 | +Generate configuration templates for users: |
| 230 | + |
| 231 | +.. code-block:: python |
| 232 | +
|
| 233 | + # Create template with defaults |
| 234 | + template_cfg = Config(model=AppConfig, sources=[]) |
| 235 | + template_cfg.dump_json("config.template.json") |
| 236 | +
|
| 237 | +Environment Setup |
| 238 | +~~~~~~~~~~~~~~~~~ |
| 239 | + |
| 240 | +Generate .env files for different environments: |
| 241 | + |
| 242 | +.. code-block:: python |
| 243 | +
|
| 244 | + # Development environment |
| 245 | + cfg.dump_env(".env.development", prefix="APP_DEV_") |
| 246 | +
|
| 247 | + # Production environment |
| 248 | + cfg.dump_env(".env.production", prefix="APP_PROD_") |
| 249 | +
|
| 250 | +Configuration Sharing |
| 251 | +~~~~~~~~~~~~~~~~~~~~~ |
| 252 | + |
| 253 | +Export configuration to share with team members or for documentation: |
| 254 | + |
| 255 | +.. code-block:: python |
| 256 | +
|
| 257 | + # Export to YAML for documentation |
| 258 | + cfg.dump_yaml("docs/example_config.yaml") |
| 259 | +
|
| 260 | +Dependencies |
| 261 | +------------ |
| 262 | + |
| 263 | +- **JSON**: Built-in (no extra dependencies required) |
| 264 | +- **YAML**: Requires ``pyyaml`` (``pip install pyyaml``) |
| 265 | +- **TOML**: Requires ``tomli-w`` (``pip install tomli-w``) |
| 266 | +- **.env**: Built-in (no extra dependencies required) |
| 267 | + |
| 268 | +If a required dependency is missing, the export method will raise an ``ImportError`` with a clear message indicating which package needs to be installed. |
| 269 | + |
| 270 | +Best Practices |
| 271 | +-------------- |
| 272 | + |
| 273 | +1. **Validate before export**: By default, all export methods validate required fields. Set ``validate=False`` only if you're intentionally exporting incomplete configurations. |
| 274 | + |
| 275 | +2. **Use appropriate formats**: |
| 276 | + - Use JSON for machine-readable configs and APIs |
| 277 | + - Use YAML for human-readable configs and documentation |
| 278 | + - Use TOML for Python projects (pyproject.toml style) |
| 279 | + - Use .env for environment variable exports |
| 280 | + |
| 281 | +3. **Handle nested structures**: All export methods automatically handle nested dataclasses, so you don't need to manually flatten structures. |
| 282 | + |
| 283 | +4. **Use prefixes for .env**: When exporting to .env format, use prefixes to avoid conflicts with other environment variables. |
| 284 | + |
| 285 | +5. **Version control**: Consider adding exported config files to ``.gitignore`` if they contain sensitive information like API keys. |
0 commit comments