ccproxy.config.toml_generator¶
ccproxy.config.toml_generator
¶
Utility functions for generating TOML configuration from Pydantic models.
is_hidden_in_example
¶
Determine if a field should be omitted from generated example configs.
Source code in ccproxy/config/toml_generator.py
get_field_description
¶
Get a human-readable description from a Pydantic field.
generate_config_from_model
¶
Generate a default configuration dictionary from a Pydantic model class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
type[T]
|
The Pydantic model class to generate config from |
required |
include_hidden
|
bool
|
Whether to include fields marked as hidden in examples |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing the configuration data |
Source code in ccproxy/config/toml_generator.py
generate_nested_config
¶
Generate configuration for nested Pydantic models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel
|
The Pydantic model instance to generate config from |
required |
include_hidden
|
bool
|
Whether to include fields marked as hidden in examples |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing the nested configuration data |
Source code in ccproxy/config/toml_generator.py
format_value_for_toml
¶
Format a configuration value for TOML output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to format |
required |
Returns:
| Type | Description |
|---|---|
str
|
String representation suitable for TOML |
Source code in ccproxy/config/toml_generator.py
generate_toml_section
¶
Generate a TOML section string with proper indentation and commenting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary of configuration data |
required |
prefix
|
str
|
Comment prefix (e.g., "# " for commented sections) |
''
|
level
|
int
|
Nesting level for proper formatting |
0
|
Returns:
| Type | Description |
|---|---|
str
|
TOML section as a string |
Source code in ccproxy/config/toml_generator.py
generate_toml_config
¶
generate_toml_config(
config_data,
model_class,
header_comment=None,
commented=True,
root_field=None,
)
Generate TOML configuration string from config data and model class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_data
|
dict[str, Any]
|
Configuration dictionary to convert to TOML |
required |
model_class
|
type[BaseModel]
|
The Pydantic model class (used for field descriptions) |
required |
header_comment
|
str | None
|
Optional custom header comment |
None
|
commented
|
bool
|
Whether to comment out all settings (default True) |
True
|
root_field
|
str | None
|
Optional root section path (e.g., "plugins.max_tokens") to nest all fields under |
None
|
Returns:
| Type | Description |
|---|---|
str
|
TOML configuration as a string |
Source code in ccproxy/config/toml_generator.py
write_toml_config
¶
write_toml_config(
output,
model_class,
config_data=None,
header_comment=None,
commented=True,
include_hidden=False,
root_field=None,
)
Write TOML configuration directly to a stream or file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
TextIO | Path | str
|
Output destination - can be a TextIO stream (file, StringIO, stdout), a Path object, or a string path to a file |
required |
model_class
|
type[BaseModel]
|
The Pydantic model class to generate config from |
required |
config_data
|
dict[str, Any] | None
|
Optional config dictionary. If None, generates from model defaults |
None
|
header_comment
|
str | None
|
Optional custom header comment |
None
|
commented
|
bool
|
Whether to comment out all settings (default True) |
True
|
include_hidden
|
bool
|
Whether to include fields marked as hidden in examples |
False
|
root_field
|
str | None
|
Optional root section path (e.g., "plugins.max_tokens") |
None
|
Examples:
Write to stdout¶
write_toml_config(sys.stdout, Settings)
Write to file¶
write_toml_config("config.toml", Settings) write_toml_config(Path("config.toml"), Settings)
Write to StringIO¶
buffer = StringIO() write_toml_config(buffer, Settings) content = buffer.getvalue()
Write to file object¶
with open("config.toml", "w") as f: write_toml_config(f, Settings)
Write with root field¶
write_toml_config(sys.stdout, MaxTokensConfig, root_field="plugins.max_tokens")