File: README.rst

package info (click to toggle)
python-pyhcl 0.4.4-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: python: 3,833; makefile: 25; sh: 6
file content (116 lines) | stat: -rw-r--r-- 3,081 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pyhcl
=====

|Build Status|

Implements a parser for `HCL (HashiCorp Configuration
Language) <https://github.com/hashicorp/hcl>`__ 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.

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 <https://pypi.python.org/pypi/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

.. |Build Status| image:: https://travis-ci.org/virtuald/pyhcl.svg?branch=master
   :target: https://travis-ci.org/virtuald/pyhcl