Giter Site home page Giter Site logo

pyhcl's Introduction

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

Note: This project is not associated with Hashicorp

pyhcl's People

Contributors

bspaans avatar ermakov-oleg avatar jammycakes avatar janboll avatar jonesetc avatar placydo avatar scottbelden avatar virtuald avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.