Skip to main content

pyhcl

Implements a parser for HCL (HashiCorp Configuration Language) in Python. This implementation aims to be compatible with the original golang version of the parser.

pyhcl does not support HCL2 (which is what modern terraform uses). You might try https://pypi.org/project/python-hcl2/ instead (though I’ve never personally tried it).

The grammar and many of the tests/fixtures were copied/ported from the golang parser into pyhcl. All releases are tested with a variety of python versions from Python 2.7 onward.

This version has been modified to work with terraform 0.12 syntax. It should be backward compatible with earlier versions. It doesn’t cover every situation. See discussion in pull request: https://github.com/virtuald/pyhcl/pull/57

Installation

pip install pyhcl

Usage

This module is intended to be used in mostly the same way that one would use the json module in python, and load/loads/dumps are implemented.

import hcl

with open('file.hcl', 'r') as fp:
    obj = hcl.load(fp)

Currently the dumps function outputs JSON, and not HCL.

Convert HCL to JSON

pyhcl comes with a script that you can use to easily convert HCL to JSON, similar to the json.tool that comes with python:

hcltool INFILE [OUTFILE]

Structure Validation

Similar to JSON, the output of parsing HCL is a python dictionary with no defined structure. The golang library for HCL implements support for parsing HCL according to defined objects, but this implementation does not currently support such constructs.

Instead, I recommend that you use tools designed to validate JSON, such as the schematics library.

Syntax

  • Single line comments start with # or //

  • Multi-line comments are wrapped in /* and */

  • Values are assigned with the syntax key = value (whitespace doesn’t matter). The value can be any primitive: a string, number, boolean, object, or list.

  • Strings are double-quoted and can contain any UTF-8 characters. Example: "Hello, World"

  • Numbers are assumed to be base 10. If you prefix a number with 0x, it is treated as a hexadecimal. If it is prefixed with 0, it is treated as an octal. Numbers can be in scientific notation: “1e10”.

  • Boolean values: true, false

  • Arrays can be made by wrapping it in []. Example: ["foo", "bar", 42]. Arrays can contain primitives and other arrays, but cannot contain objects. Objects must use the block syntax shown below.

Objects and nested objects are created using the structure shown below:

variable "ami" {
    description = "the AMI to use"
}

Testing

To run the tests:

pip install -r testing-requirements.txt
tests/run_tests.sh

Debug Mode

To enable debug mode:

import hcl
hcl.parser.DEBUG = True

Authors

Dustin Spicuzza (dustin@virtualroadside.com)

Note: This project is not associated with Hashicorp

Release files for pyhcl 0.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyhcl 0.4.5
File Size Uploaded
pyhcl-0.4.5.tar.gz 61.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyhcl 0.4.5
File Interpreter ABI Platform
pyhcl-0.4.5-py3-none-any.whl Python 3 none any Details

Total release size: 111.9 kB

Release files / pyhcl-0.4.5.tar.gz

Download URL pyhcl-0.4.5.tar.gz
Size 61.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c47293a51ccdd25e18bb5c8c0ab0ffe355b37c87f8d6f9d3280dc41efd4740bc
BLAKE2b-256 checksum
How to use checksums
20b9ff5019eef43b613f49a4695253d8814f8a7012745ae570158e5b64967200
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / pyhcl-0.4.5-py3-none-any.whl

Download URL pyhcl-0.4.5-py3-none-any.whl
Size 50.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30ee337d330d1f90c9f5ed8f49c468f66c8e6e43192bdc7c6ece1420beb3070c
BLAKE2b-256 checksum
How to use checksums
a8b4b766fd33774b55cc15f240916219c4ce9c1bd7bef4acd997ad36ff3622d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.4.5 This release

2 release files

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.15

1 release file

0.3.14

1 release file

0.3.13

1 release file

0.3.12

1 release file

0.3.11

1 release file

0.3.10

1 release file

0.3.9

1 release file

0.3.8

1 release file

0.3.7

1 release file

0.3.6

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.15

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page