Skip to main content

Pyvider HCL: A wrapper for python-hcl2 integrating with Pyvider's Cty type system.

Project description

📄⚙️ Pyvider HCL

License Python 3.11+ uv Ruff CI

Python library for parsing HCL into pyvider.cty types

pyvider-hcl provides a simple and intuitive way to work with HCL (HashiCorp Configuration Language) data in your Python applications, with seamless integration into the pyvider ecosystem.

✨ Key Features

  • 🔄 CTY Integration - Parses HCL directly into CtyValue objects for pyvider compatibility
  • 🎯 Simplified API - Clean interface for parsing HCL and creating Terraform structures
  • 🔍 Automatic Type Inference - Infer CtyType from HCL data without explicit schemas
  • Schema Validation - Validate HCL data against CTY type schemas
  • 🏭 Factory Functions - Create Terraform variables and resources programmatically
  • 🖨️ Pretty Printing - Debug-friendly output for CTY values

Quick Start

Note: pyvider-hcl is in pre-release (v0.x.x). APIs and features may change before 1.0 release.

  1. Install: uv add pyvider-hcl
  2. Read the Getting Started guide.
  3. Try the examples in examples/README.md.

Documentation

Development

Quick Start

# Set up environment
uv sync

# Run common tasks
we run test       # Run tests
we run lint       # Check code
we run format     # Format code
we tasks          # See all available commands

See CLAUDE.md for detailed development instructions and architecture information.

For contribution guidelines, see CONTRIBUTING.md.

Format and lint

uv run ruff format .
uv run ruff check .

Contributing

See CONTRIBUTING.md for contribution guidelines.

License

Apache-2.0 License - see LICENSE for details.

Installation

To install pyvider-hcl, you can use uv:

uv add pyvider-hcl

Usage

Here's a simple example of how to use pyvider-hcl to parse an HCL string:

from pyvider.hcl import parse_hcl_to_cty, pretty_print_cty
from pyvider.cty import CtyString

hcl_string = """
  name = "Jules"
  age = 30
"""

cty_value = parse_hcl_to_cty(hcl_string)

pretty_print_cty(cty_value)

You can also parse an HCL file:

from pyvider.hcl import parse_hcl_to_cty, pretty_print_cty

with open("my_config.hcl", "r") as f:
    hcl_content = f.read()
    cty_value = parse_hcl_to_cty(hcl_content)
    pretty_print_cty(cty_value)

Schema Validation

You can validate HCL data against a CtyType schema:

from pyvider.hcl import parse_hcl_to_cty, pretty_print_cty
from pyvider.cty import CtyObject, CtyString, CtyNumber

schema = CtyObject({
    "name": CtyString(),
    "age": CtyNumber(),
})

hcl_string = """
  name = "Jules"
  age = "thirty" # Invalid type
"""

try:
    cty_value = parse_hcl_to_cty(hcl_string, schema=schema)
except Exception as e:
    print(e)

Complex Cty Integration Examples

Here are some more complex examples of how to use pyvider-hcl with pyvider.cty:

Parsing a list of objects

from pyvider.hcl import parse_hcl_to_cty, pretty_print_cty
from pyvider.cty import CtyObject, CtyList, CtyString, CtyNumber

hcl_string = """
  users = [
    {
      name = "Jules"
      age  = 30
    },
    {
      name = "Vincent"
      age  = 40
    }
  ]
"""

schema = CtyObject({
    "users": CtyList(
        element_type=CtyObject({
            "name": CtyString(),
            "age": CtyNumber(),
        })
    )
})

cty_value = parse_hcl_to_cty(hcl_string, schema=schema)

pretty_print_cty(cty_value)

Parsing nested objects

from pyvider.hcl import parse_hcl_to_cty, pretty_print_cty
from pyvider.cty import CtyObject, CtyString, CtyNumber

hcl_string = """
  config = {
    server = {
      host = "localhost"
      port = 8080
    }
    database = {
      host = "localhost"
      port = 5432
    }
  }
"""

schema = CtyObject({
    "config": CtyObject({
        "server": CtyObject({
            "host": CtyString(),
            "port": CtyNumber(),
        }),
        "database": CtyObject({
            "host": CtyString(),
            "port": CtyNumber(),
        }),
    })
})

cty_value = parse_hcl_to_cty(hcl_string, schema=schema)

pretty_print_cty(cty_value)

Creating Terraform Variables and Resources

You can use the factory functions to create CtyValue objects for Terraform variables and resources:

from pyvider.hcl import (
    parse_hcl_to_cty,
    pretty_print_cty,
    create_variable_cty,
    create_resource_cty,
)

# Create a variable
variable_cty = create_variable_cty(
    name="my_variable",
    type_str="string",
    default_py="my_default_value",
)

pretty_print_cty(variable_cty)

# Create a resource
resource_cty = create_resource_cty(
    r_type="my_resource",
    r_name="my_instance",
    attributes_py={
        "name": "my_resource_name",
        "value": 123,
    },
)

pretty_print_cty(resource_cty)

FAQ

How do I parse an HCL file?

Currently, you need to read the file manually and pass the content to parse_hcl_to_cty():

from pathlib import Path
from pyvider.hcl import parse_hcl_to_cty

hcl_content = Path("config.hcl").read_text()
result = parse_hcl_to_cty(hcl_content)

Can this library generate HCL output?

No, pyvider-hcl is currently focused on parsing HCL into CTY types. It does not generate HCL output. You can use the pretty_print_cty() function to display CTY values in a readable format, but this is not HCL syntax.

Does this support HCL expressions like var.name or length(list)?

Not yet. The library currently parses static HCL data. Expression evaluation (variables, functions, conditionals) is not implemented.

What's the difference between parse_hcl_to_cty() and parse_with_context()?

  • parse_hcl_to_cty(): Returns a CtyValue object with full type information. Use this for most cases.
  • parse_with_context(): Returns raw Python dict/list from the parser. Use this when you need the raw data structure or want enhanced error context without CTY conversion.

How do I validate HCL against a specific structure?

Pass a CTY schema to parse_hcl_to_cty():

from pyvider.hcl import parse_hcl_to_cty
from pyvider.cty import CtyObject, CtyString, CtyNumber

schema = CtyObject({
    "name": CtyString(),
    "port": CtyNumber(),
})

result = parse_hcl_to_cty(hcl_content, schema=schema)
# Raises HclParsingError if validation fails

Can I use this with Terraform configurations?

Yes! The library parses HCL syntax used by Terraform. The create_variable_cty() and create_resource_cty() factory functions help create Terraform-specific structures. Full Terraform-specific validation (provider blocks, module blocks, etc.) is limited.

What HCL version is supported?

The library uses python-hcl2 which supports HCL 2.x (the version used by Terraform 0.12+).

How do I handle parsing errors?

Wrap your parsing calls in a try/except block:

from pyvider.hcl import parse_hcl_to_cty, HclParsingError

try:
    result = parse_hcl_to_cty(hcl_content)
except HclParsingError as e:
    print(f"Parsing failed: {e}")
    # e.source_file, e.line, e.column available if set

Can I parse multiple HCL files at once?

You need to parse each file individually. For multi-file Terraform projects, parse each file separately and combine the results as needed.

What types can be automatically inferred?

When no schema is provided, the library automatically infers:

  • stringCtyString
  • number (int/float) → CtyNumber
  • boolCtyBool
  • listCtyList(CtyDynamic())
  • objectCtyObject with inferred field types

How do I contribute or report bugs?

See CONTRIBUTING.md for contribution guidelines. For bugs, please open an issue on the GitHub repository with:

  • The HCL content that fails
  • The error message
  • Expected vs. actual behavior

Related Projects

Copyright (c) provide.io LLC.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyvider_hcl-0.4.0.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyvider_hcl-0.4.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file pyvider_hcl-0.4.0.tar.gz.

File metadata

  • Download URL: pyvider_hcl-0.4.0.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pyvider_hcl-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d17040b2626ab6e631b36c1ef63ca31061919d57be1d03d22e369002d6dd326b
MD5 273b73e1998b782a9f4a53d35a309689
BLAKE2b-256 aab26974d716a3bca58868bd1ed9338e99ce96b92e6d4df200d67ffef5f21006

See more details on using hashes here.

File details

Details for the file pyvider_hcl-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pyvider_hcl-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pyvider_hcl-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f898def26cc626ef8d9d35dfefbf59bb66617ecb2863ca7da15e63b4313006a6
MD5 60c7907fb34185a117198f2445556932
BLAKE2b-256 b4cba17c95e7054ed08b51ad4d6ea23a4216c4a01b64ec6f6b2efdf302fb07f9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page