Skip to main content

HCL configuration parser for python

Project description

pyhcl

Build Status

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

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.

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

Authors

Dustin Spicuzza (dustin@virtualroadside.com)

Note: This project is not associated with Hashicorp

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

pyhcl-0.3.13.tar.gz (21.8 kB view details)

Uploaded Source

File details

Details for the file pyhcl-0.3.13.tar.gz.

File metadata

  • Download URL: pyhcl-0.3.13.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

File hashes

Hashes for pyhcl-0.3.13.tar.gz
Algorithm Hash digest
SHA256 ad4bf45f1f259b51baa6e6a3d7375341fcd1482c0442df10289d44ba349772c1
MD5 36fd997ea3db40d1e3457a489f11d08a
BLAKE2b-256 f8d54487b5a9aee56f0597428fbf29db7297255bed962fef30252e1db5cd2411

See more details on using hashes here.

Supported by

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