Index
pyvider.hcl
¶
pyvider-hcl: HCL parsing with CTY type system integration.
This package provides HCL (HashiCorp Configuration Language) parsing capabilities with seamless integration into the pyvider ecosystem through the CTY type system.
Classes¶
HclError
¶
HclError(
message: str,
*,
code: str | None = None,
context: dict[str, Any] | None = None,
cause: Exception | None = None,
**extra_context: Any
)
Bases: FoundationError
Base class for errors related to HCL processing in Pyvider.
Source code in provide/foundation/errors/base.py
HclFactoryError
¶
Bases: ValueError
Custom exception for errors during HCL factory operations.
HclParsingError
¶
HclParsingError(
message: str,
*,
code: str | None = None,
context: dict[str, Any] | None = None,
cause: Exception | None = None,
**extra_context: Any
)
Bases: HclError
Raised when HCL parsing or schema validation fails.
This is an attrs-based exception class for structured error reporting.
Source code in provide/foundation/errors/base.py
Functions¶
__str__
¶
Provides a detailed error message including source location if available.
Source code in pyvider/hcl/exceptions.py
HclTypeParsingError
¶
Bases: ValueError
Custom exception for errors during HCL type string parsing.
Functions¶
auto_infer_cty_type
¶
Automatically infer CTY type from raw Python data.
This function takes Python data structures (typically from HCL parsing) and automatically infers appropriate CTY types using pyvider-cty's canonical inference implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_data
|
Any
|
Python data structure to infer types for |
required |
Returns:
| Type | Description |
|---|---|
CtyValue[Any]
|
CTY value with inferred types |
Example
data = {"name": "test", "count": 5} result = auto_infer_cty_type(data) isinstance(result.type, CtyObject) True
Note
This delegates to pyvider.cty.conversion.infer_cty_type_from_raw() which provides sophisticated type inference including: - List element type analysis (e.g., [1,2,3] → list(number)) - Object attribute inference - Type unification for mixed collections - Caching and cycle detection
Source code in pyvider/hcl/parser/inference.py
create_resource_cty
¶
create_resource_cty(
r_type: str,
r_name: str,
attributes_py: dict[str, Any],
attributes_schema_py: dict[str, str] | None = None,
) -> CtyValue[Any]
Create a Terraform resource CTY structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r_type
|
str
|
Resource type (e.g., "aws_instance") |
required |
r_name
|
str
|
Resource name |
required |
attributes_py
|
dict[str, Any]
|
Resource attributes as Python dict |
required |
attributes_schema_py
|
dict[str, str] | None
|
Optional type strings for attributes |
None
|
Returns:
| Type | Description |
|---|---|
CtyValue[Any]
|
CTY value representing Terraform resource structure |
Raises:
| Type | Description |
|---|---|
HclFactoryError
|
If validation fails |
Example
resource = create_resource_cty( ... r_type="aws_instance", ... r_name="web", ... attributes_py={"ami": "ami-123", "instance_type": "t2.micro"}, ... attributes_schema_py={"ami": "string", "instance_type": "string"} ... )
Source code in pyvider/hcl/factories/resources.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
create_variable_cty
¶
create_variable_cty(
name: str,
type_str: str,
default_py: Any | None = None,
description: str | None = None,
sensitive: bool | None = None,
nullable: bool | None = None,
) -> CtyValue[Any]
Create a Terraform variable CTY structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Variable name (must be valid identifier) |
required |
type_str
|
str
|
HCL type string (e.g., "string", "list(number)") |
required |
default_py
|
Any | None
|
Optional default value |
None
|
description
|
str | None
|
Optional description |
None
|
sensitive
|
bool | None
|
Optional sensitive flag |
None
|
nullable
|
bool | None
|
Optional nullable flag |
None
|
Returns:
| Type | Description |
|---|---|
CtyValue[Any]
|
CTY value representing Terraform variable structure |
Raises:
| Type | Description |
|---|---|
HclFactoryError
|
If validation fails |
Example
var = create_variable_cty( ... name="region", ... type_str="string", ... default_py="us-west-2" ... )
Source code in pyvider/hcl/factories/variables.py
parse_hcl_to_cty
¶
Parse HCL directly into validated CtyValues using pyvider.cty types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hcl_content
|
str
|
HCL string to parse |
required |
schema
|
CtyType[Any] | None
|
Optional CTY type schema for validation |
None
|
Returns:
| Type | Description |
|---|---|
CtyValue[Any]
|
Parsed and validated CTY value |
Raises:
| Type | Description |
|---|---|
HclParsingError
|
If parsing or validation fails |
Example
hcl = 'name = "example"' result = parse_hcl_to_cty(hcl) result.value["name"].value 'example'
Source code in pyvider/hcl/parser/base.py
parse_terraform_config
¶
Parse Terraform configuration file.
This is a placeholder function for future Terraform-specific configuration parsing. Currently returns a placeholder dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Path
|
Path to Terraform configuration file |
required |
Returns:
| Type | Description |
|---|---|
TerraformConfig
|
Placeholder dict with status information |
Note
This function is not fully implemented yet. Future versions will provide: - Provider block parsing - Resource block parsing with validation - Variable, output, locals blocks - Module block parsing - Data source block parsing
Example
from pathlib import Path result = parse_terraform_config(Path("main.tf")) result["status"] 'not_implemented'
Source code in pyvider/hcl/terraform/config.py
parse_with_context
¶
Parse HCL content with enhanced error context.
This function parses HCL content and provides rich error context if parsing fails. It returns the raw parsed data (dict/list), not CTY values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
HCL content string to parse |
required |
source_file
|
Path | None
|
Optional source file path for error reporting |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Raw parsed data (typically dict or list) |
Raises:
| Type | Description |
|---|---|
HclParsingError
|
If parsing fails, with source location information |
Example
content = 'name = "example"' data = parse_with_context(content) data['name'] 'example'