python-hcl2 5.0.0__tar.gz → 5.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.github/workflows/pr_check.yml +1 -1
  2. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/CHANGELOG.md +38 -0
  3. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/PKG-INFO +10 -1
  4. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/README.md +8 -0
  5. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/version.py +2 -2
  6. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/pyproject.toml +1 -0
  7. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/PKG-INFO +10 -1
  8. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/SOURCES.txt +1 -0
  9. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/tox.ini +2 -1
  10. python-hcl2-5.1.1/tree-to-hcl2-reconstruction.md +130 -0
  11. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.codacy.yml +0 -0
  12. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.coveragerc +0 -0
  13. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.github/CODEOWNERS +0 -0
  14. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.github/workflows/codeql-analysis.yml +0 -0
  15. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.github/workflows/publish.yml +0 -0
  16. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.gitignore +0 -0
  17. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.pre-commit-config.yaml +0 -0
  18. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/.yamllint.yml +0 -0
  19. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/LICENSE +0 -0
  20. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/MANIFEST.in +0 -0
  21. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/bin/terraform_test +0 -0
  22. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/__init__.py +0 -0
  23. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/__main__.py +0 -0
  24. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/api.py +0 -0
  25. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/hcl2.lark +0 -0
  26. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/parser.py +0 -0
  27. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/py.typed +0 -0
  28. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/reconstructor.py +0 -0
  29. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/hcl2/transformer.py +0 -0
  30. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/mypy.ini +0 -0
  31. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/pylintrc +0 -0
  32. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/dependency_links.txt +0 -0
  33. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/entry_points.txt +0 -0
  34. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/not-zip-safe +0 -0
  35. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/requires.txt +0 -0
  36. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/python_hcl2.egg-info/top_level.txt +0 -0
  37. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/reports/.gitignore +0 -0
  38. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/requirements.txt +0 -0
  39. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/setup.cfg +0 -0
  40. {python-hcl2-5.0.0 → python-hcl2-5.1.1}/test-requirements.txt +0 -0
@@ -13,7 +13,7 @@ jobs:
13
13
  runs-on: ubuntu-22.04
14
14
  strategy:
15
15
  matrix:
16
- python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
16
+ python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12', '3.13']
17
17
  steps:
18
18
  - uses: actions/checkout@master
19
19
  - name: Set up Python ${{ matrix.python-version }}
@@ -5,6 +5,44 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## \[5.1.1\] - 2024-10-15
9
+
10
+ ### Added
11
+
12
+ - fix `tree-to-hcl2-reconstruction.md` URL in README.md ([#175](https://github.com/amplify-education/python-hcl2/pull/175))
13
+
14
+ ## \[5.1.0\] - 2024-10-15
15
+
16
+ ### Added
17
+
18
+ - support python 3.13 ([#170](https://github.com/amplify-education/python-hcl2/pull/170))
19
+ - add section about Tree->HCL2 reconstruction to the README.md ([#174](https://github.com/amplify-education/python-hcl2/pull/174))
20
+
21
+ ## \[5.0.0\] - 2024-10-07
22
+
23
+ ### Added
24
+
25
+ - Support full reconstruction of HCL from parse tree. Thanks, @weaversam8 ([#169](https://github.com/amplify-education/python-hcl2/pull/169))
26
+
27
+ ## \[4.3.5\] - 2024-08-06
28
+
29
+ ### Added
30
+
31
+ - additional test coverage ([#165](https://github.com/amplify-education/python-hcl2/pull/165))
32
+ - fix: Add support for attributes named "in". Thanks, @elisiariocouto ([#164](https://github.com/amplify-education/python-hcl2/pull/164))
33
+ - fix: add "for" attribute identifier. Thanks, @zhcli ([#167](https://github.com/amplify-education/python-hcl2/pull/167))
34
+ - allow `if` and `for_each` keywords to be used as identifiers ([#168](https://github.com/amplify-education/python-hcl2/pull/168))
35
+
36
+ ### Added
37
+
38
+ ## \[4.3.4\] - 2024-06-12
39
+
40
+ ### Added
41
+
42
+ - fix codacy badge ([#157](https://github.com/amplify-education/python-hcl2/pull/157))
43
+ - Fix MANIFEST.in and/or Python dependency filename(s) ([#161](https://github.com/amplify-education/python-hcl2/pull/161))
44
+ - adds support for provider functions. Thanks, @lkwg82 ([#162](https://github.com/amplify-education/python-hcl2/pull/162))
45
+
8
46
  ## \[4.3.3\] - 2024-03-27
9
47
 
10
48
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-hcl2
3
- Version: 5.0.0
3
+ Version: 5.1.1
4
4
  Summary: A parser for HCL2
5
5
  Author-email: Amplify Education <github@amplify.com>
6
6
  License: MIT
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3.9
18
18
  Classifier: Programming Language :: Python :: 3.10
19
19
  Classifier: Programming Language :: Python :: 3.11
20
20
  Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
21
22
  Requires-Python: >=3.7.0
22
23
  Description-Content-Type: text/markdown
23
24
  License-File: LICENSE
@@ -66,6 +67,14 @@ with open('foo.tf', 'r') as file:
66
67
  dict = hcl2.load(file)
67
68
  ```
68
69
 
70
+ ### Parse Tree to HCL2 reconstruction
71
+
72
+ With version 5.0.0 the possibility of HCL2 reconstruction from Lark Parse Tree was introduced.
73
+
74
+ Example of manipulating Lark Parse Tree and reconstructing it back into valid HCL2 can be found in [tree-to-hcl2-reconstruction.md](https://github.com/amplify-education/python-hcl2/blob/main/tree-to-hcl2-reconstruction.md) file.
75
+
76
+ More details about reconstruction implementation can be found in this [PR](https://github.com/amplify-education/python-hcl2/pull/169).
77
+
69
78
  ## Building From Source
70
79
 
71
80
  For development, `tox>=4.0.9` is recommended.
@@ -42,6 +42,14 @@ with open('foo.tf', 'r') as file:
42
42
  dict = hcl2.load(file)
43
43
  ```
44
44
 
45
+ ### Parse Tree to HCL2 reconstruction
46
+
47
+ With version 5.0.0 the possibility of HCL2 reconstruction from Lark Parse Tree was introduced.
48
+
49
+ Example of manipulating Lark Parse Tree and reconstructing it back into valid HCL2 can be found in [tree-to-hcl2-reconstruction.md](https://github.com/amplify-education/python-hcl2/blob/main/tree-to-hcl2-reconstruction.md) file.
50
+
51
+ More details about reconstruction implementation can be found in this [PR](https://github.com/amplify-education/python-hcl2/pull/169).
52
+
45
53
  ## Building From Source
46
54
 
47
55
  For development, `tox>=4.0.9` is recommended.
@@ -1,4 +1,4 @@
1
1
  # file generated by setuptools_scm
2
2
  # don't change, don't track in version control
3
- __version__ = version = '5.0.0'
4
- __version_tuple__ = version_tuple = (5, 0, 0)
3
+ __version__ = version = '5.1.1'
4
+ __version_tuple__ = version_tuple = (5, 1, 1)
@@ -22,6 +22,7 @@ classifiers = [
22
22
  "Programming Language :: Python :: 3.10",
23
23
  "Programming Language :: Python :: 3.11",
24
24
  "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
25
26
  ]
26
27
  requires-python = ">=3.7.0"
27
28
  dependencies = ["lark>=1,<2"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-hcl2
3
- Version: 5.0.0
3
+ Version: 5.1.1
4
4
  Summary: A parser for HCL2
5
5
  Author-email: Amplify Education <github@amplify.com>
6
6
  License: MIT
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3.9
18
18
  Classifier: Programming Language :: Python :: 3.10
19
19
  Classifier: Programming Language :: Python :: 3.11
20
20
  Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
21
22
  Requires-Python: >=3.7.0
22
23
  Description-Content-Type: text/markdown
23
24
  License-File: LICENSE
@@ -66,6 +67,14 @@ with open('foo.tf', 'r') as file:
66
67
  dict = hcl2.load(file)
67
68
  ```
68
69
 
70
+ ### Parse Tree to HCL2 reconstruction
71
+
72
+ With version 5.0.0 the possibility of HCL2 reconstruction from Lark Parse Tree was introduced.
73
+
74
+ Example of manipulating Lark Parse Tree and reconstructing it back into valid HCL2 can be found in [tree-to-hcl2-reconstruction.md](https://github.com/amplify-education/python-hcl2/blob/main/tree-to-hcl2-reconstruction.md) file.
75
+
76
+ More details about reconstruction implementation can be found in this [PR](https://github.com/amplify-education/python-hcl2/pull/169).
77
+
69
78
  ## Building From Source
70
79
 
71
80
  For development, `tox>=4.0.9` is recommended.
@@ -13,6 +13,7 @@ pyproject.toml
13
13
  requirements.txt
14
14
  test-requirements.txt
15
15
  tox.ini
16
+ tree-to-hcl2-reconstruction.md
16
17
  .github/CODEOWNERS
17
18
  .github/workflows/codeql-analysis.yml
18
19
  .github/workflows/pr_check.yml
@@ -1,5 +1,5 @@
1
1
  [tox]
2
- envlist={py37}-unit,{py38}-unit,{py39}-unit,{py310}-unit,{py311}-unit,{py312}-unit
2
+ envlist={py37}-unit,{py38}-unit,{py39}-unit,{py310}-unit,{py311}-unit,{py312}-unit,{py313}-unit
3
3
  skipsdist=true
4
4
 
5
5
  [gh-actions]
@@ -10,6 +10,7 @@ python =
10
10
  3.10: py310-unit
11
11
  3.11: py311-unit
12
12
  3.12: py312-unit
13
+ 3.13: py313-unit
13
14
 
14
15
  [testenv]
15
16
  commands =
@@ -0,0 +1,130 @@
1
+ Given `example.tf` file with following content
2
+
3
+ ```terraform
4
+ resource "aws_s3_bucket" "bucket" {
5
+ bucket = "bucket_id"
6
+ force_destroy = true
7
+ }
8
+ ```
9
+
10
+ below code will add a `tags` object to the S3 bucket definition. The code can also be used to print out readable representation of **any** Parse Tree (any valid HCL2 file), which can be useful when working on your own logic for arbitrary Parse Tree manipulation.
11
+
12
+ ```python
13
+ from copy import deepcopy
14
+ from lark import Token, Tree
15
+ import hcl2
16
+
17
+
18
+ def build_tags_tree(base_indent: int = 0) -> Tree:
19
+ # build Tree representing following HCL2 structure
20
+ # tags = {
21
+ # Name = "My bucket"
22
+ # Environment = "Dev"
23
+ # }
24
+ return Tree('attribute', [
25
+ Tree('identifier', [
26
+ Token('NAME', 'tags')
27
+ ]),
28
+ Token('EQ', '='),
29
+ Tree('expr_term', [
30
+ Tree('object', [
31
+ Tree('new_line_or_comment', [
32
+ Token('NL_OR_COMMENT', '\n' + ' ' * (base_indent + 1)),
33
+ ]),
34
+ Tree('object_elem', [
35
+ Tree('identifier', [
36
+ Token('NAME', 'Name')
37
+ ]),
38
+ Token('EQ', '='),
39
+ Tree('expr_term', [
40
+ Token('STRING_LIT', '"My bucket"')
41
+ ])
42
+ ]),
43
+ Tree('new_line_and_or_comma', [
44
+ Tree('new_line_or_comment', [
45
+ Token('NL_OR_COMMENT', '\n' + ' ' * (base_indent + 1)),
46
+ ]),
47
+ ]),
48
+ Tree('object_elem', [
49
+ Tree('identifier', [
50
+ Token('NAME', 'Environment')
51
+ ]),
52
+ Token('EQ', '='),
53
+ Tree('expr_term', [
54
+ Token('STRING_LIT', '"Dev"')
55
+ ])
56
+ ]),
57
+ Tree('new_line_and_or_comma', [
58
+ Tree('new_line_or_comment', [
59
+ Token('NL_OR_COMMENT', '\n' + ' ' * base_indent),
60
+ ]),
61
+ ]),
62
+ ]),
63
+ ])
64
+ ])
65
+
66
+
67
+ def is_bucket_block(tree: Tree) -> bool:
68
+ # check whether given Tree represents `resource "aws_s3_bucket" "bucket"`
69
+ try:
70
+ return tree.data == 'block' and tree.children[2].value == '"bucket"'
71
+ except IndexError:
72
+ return False
73
+
74
+
75
+ def insert_tags(tree: Tree, indent: int = 0) -> Tree:
76
+ # Insert tags tree and adjust surrounding whitespaces to match indentation
77
+ new_children = [*tree.children.copy(), build_tags_tree(indent)]
78
+ # add indentation before tags tree
79
+ new_children[len(tree.children) - 1] = Tree('new_line_or_comment', [
80
+ Token('NL_OR_COMMENT', '\n ')
81
+ ])
82
+ # move closing bracket to the new line
83
+ new_children.append(
84
+ Tree('new_line_or_comment', [
85
+ Token('NL_OR_COMMENT', '\n')
86
+ ])
87
+ )
88
+ return Tree(tree.data, new_children)
89
+
90
+
91
+ def process_token(node: Token, indent=0):
92
+ # Print details of this token and return its copy
93
+ print(f'[{indent}] (token)\t|', ' ' * indent, node.type, node.value)
94
+ return deepcopy(node)
95
+
96
+
97
+ def process_tree(node: Tree, depth=0) -> Tree:
98
+ # Recursively iterate over tree's children
99
+ # the depth parameter represents recursion depth,
100
+ # it's used to deduce indentation for printing tree and for adjusting whitespace after adding tags
101
+ new_children = []
102
+ print(f'[{depth}] (tree)\t|', ' ' * depth, node.data)
103
+ for child in node.children:
104
+ if isinstance(child, Tree):
105
+ if is_bucket_block(child):
106
+ block_children = child.children.copy()
107
+ # this child is the Tree representing block's actual body
108
+ block_children[3] = insert_tags(block_children[3], depth)
109
+ # replace original Tree with new one including the modified body
110
+ child = Tree(child.data, block_children)
111
+
112
+ new_children.append(process_tree(child, depth + 1))
113
+
114
+ else:
115
+ new_children.append(process_token(child, depth + 1))
116
+
117
+ return Tree(node.data, new_children)
118
+
119
+
120
+ def main():
121
+ tree = hcl2.parse(open('example.tf'))
122
+ new_tree = process_tree(tree)
123
+ reconstructed = hcl2.writes(new_tree)
124
+ open('example_reconstructed.tf', 'w').write(reconstructed)
125
+
126
+
127
+ if __name__ == "__main__":
128
+ main()
129
+
130
+ ```
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes