• Stars
    star
    334
  • Rank 126,264 (Top 3 %)
  • Language
    Python
  • License
    Mozilla Public Li...
  • Created about 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HCL is a configuration language. pyhcl is a python parser for it.

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.

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: #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 ([email protected])

Note: This project is not associated with Hashicorp

More Repositories

1

docker-registry-cache

A squid-based HTTP cache you can point at a private docker registry (v2 only)
Shell
94
star
2

vagrant-rekey-ssh

Obsolete after Vagrant 1.7. Vagrant plugin which plugs default gaping holes in a vagrant box
Ruby
55
star
3

r-star-tree

A relatively simple implementation of the R* Tree data structure for C++
C++
47
star
4

go-ordered-json

A fork of the golang encoding/json package to support ordered JSON decoding/encoding
Go
31
star
5

pygi-composite-templates

An implementation of GTK3 composite templates for PyGI
Python
18
star
6

greenado

Greenlet-based pseudo-synchronous coroutines for tornado
Python
16
star
7

mbr-lovenote

Writes a custom message to your master boot record, inspired by XKCD
C++
14
star
8

python-tesseract-sip

Python SIP wrapper for libtesseract (Apache license)
Python
12
star
9

go-paniclog

A cross-platform mechanism to write golang panic output to a file
Go
10
star
10

pydsltool

A ruby-inspired python library to enable making simple domain specific languages
Python
9
star
11

header2whatever

Generate arbitrary files from C/C++ header files
Python
6
star
12

frc-gcc-vm

A vagrant-based VM that installs the GCC 4.8 toolchain for the FRC cRio
Shell
5
star
13

ews-proxy

EWS Proxy
Go
4
star
14

nsq-docker

Automated docker builds for NSQ
Shell
4
star
15

webdma

Allows changing of program variables via a portable embedded webserver
Shell
4
star
16

magicbot-java

RobotPy's magicbot framework implemented in Java
Java
3
star
17

npp_tabs

A notepad++ plugin to make using spaces for indentation just as easy as using tabs!
C++
3
star
18

pybind11gen

Semi-automated bindings generator for pybind11
Python
3
star
19

frc-imageprocessing-workshop-2016

FRC Image processing workshop, 2016 edition
Jupyter Notebook
2
star
20

halsim-websocket

Websocket extension for HALSIM
C++
1
star
21

pygi-treeview-dnd

A library that allows using the high level drag and drop API in PyGObject
Python
1
star
22

frc-robotpy-workshop

Slides + Code for RobotPy workshop
Python
1
star
23

go-oncectx

A context-aware version of sync.Once
Go
1
star
24

pygi-gio-coroutines

Make asynchronous I/O using PyGObject+Gio easier!
Python
1
star
25

robotpy-mindsensors

MindSensors CANLight project built for RobotPy
C++
1
star