python-hcl2 | Amplify builds innovative and compelling digital | Learning library
kandi X-RAY | python-hcl2 Summary
kandi X-RAY | python-hcl2 Summary
Amplify builds innovative and compelling digital educational products that empower teachers and students across the country. We have a long history as the leading innovator in K-12 education - and have been described as the best tech company in education and the best education company in tech. While others try to shrink the learning experience into the technology, we use technology to expand what is possible in real classrooms with real students and teachers. Learn more at
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Return a dict of key value pairs
- Convert value to string
- Strip quotes from value
- Parse the given arguments into a dictionary
- Strip newline tokens
- Load data from file
- Parse text into a dictionary
- Return binary term
- Build a conditional expression
- Parse arguments
- Generate a string for introspection
- Generate a string for condition
- Returns the index of the given arguments
- Build an index expression
- Generates a FORMAT expression for a given object
- Constructs a FOR_Tuple expression
- Return a list of pip requirements
- Returns the long description
- Creates Attribute object
- Build a dictionary from the given arguments
- Build the function call
- Get the python version string
- Return the expression term
- Create the lark parser file
python-hcl2 Key Features
python-hcl2 Examples and Code Snippets
Community Discussions
Trending Discussions on python-hcl2
QUESTION
I am trying to edit Terraform configuration files with Python. I am parsing Terraform files (.tf) using python hcl2 library which returns a python dictionary. I want to add new key/value pairs or change some values in the dictionary. Directly writing to the file is not a good practice since the returned python dictionary is not in Hashicorp Configuration Language format. Also there can be multiple configuration files like variables.tf etc. which are linked together. Should I implement my own serializer which converts python dictionary to terraform configuration file or is there an easier way to do it?
...ANSWER
Answered 2021-Jan-13 at 00:07The python-hcl2
library implements a parser for the HCL syntax, but it doesn't have a serializer, and its API is designed to drop all of the HCL specifics and retain only a basic Python data structure, so it doesn't seem to retain enough information to surgically modify the input without losing details such as comments, ordering of attributes, etc.
At the time I'm writing this, the only HCL implementation that explicitly supports updating existing configuration files in-place is the Go package hclwrite
. It allows callers to load in arbitrary HCL source, surgically modify parts of it, and then re-serialize that updated version with only minor whitespace normalization to the unchanged parts of the input.
In principle it would be possible to port hclwrite
to Python, or to implement a serializer from a dictionary like python-hcl2
returns if you are not concerned with preserving unchanged input, but both of these seem like a significant project.
If you do decide to do it, one part that warrants careful attention is serialization of strings into HCL syntax, because the required escaping isn't exactly the same as any other language. You might wish to refer to the escapeQuotedStringLit
function from hclwrite
to see all of the cases to handle, so you can potentially implement compatible logic in Python.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install python-hcl2
You can use python-hcl2 like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page