Skip to content

Commit 78cd1bc

Browse files
Update code to python3.7. Dropped support of python3.6.
1 parent 618bdb7 commit 78cd1bc

File tree

8 files changed

+2361
-72
lines changed

8 files changed

+2361
-72
lines changed

hcl2/parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def create_parser_file():
3535
The below code copies some of the standalone parser generator code in a way that we can use
3636
"""
3737
lark_file = os.path.join(dirname(__file__), 'hcl2.lark')
38-
with open(lark_file, 'r') as lark_file, open(PARSER_FILE, 'w') as parser_file:
38+
with open(lark_file, 'r', encoding="utf-8") as lark_file,\
39+
open(PARSER_FILE, 'w', encoding="utf-8") as parser_file:
3940
lark_inst = Lark(lark_file.read(), parser="lalr", lexer="standard")
4041

4142
data, memo = lark_inst.memo_serialize([TerminalDef, Rule])

hcl2/transformer.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,31 @@ def expr_term(self, args: List) -> Any:
3939

4040
def index_expr_term(self, args: List) -> str:
4141
args = self.strip_new_line_tokens(args)
42-
return "%s%s" % (str(args[0]), str(args[1]))
42+
return f"{args[0]}{args[1]}"
4343

4444
def index(self, args: List) -> str:
4545
args = self.strip_new_line_tokens(args)
46-
return "[%s]" % (str(args[0]))
46+
return f"[{args[0]}]"
4747

4848
def get_attr_expr_term(self, args: List) -> str:
49-
return "%s%s" % (str(args[0]), str(args[1]))
49+
return f"{args[0]}{args[1]}"
5050

5151
def get_attr(self, args: List) -> str:
52-
return ".%s" % args[0]
52+
return f".{args[0]}"
5353

5454
def attr_splat_expr_term(self, args: List) -> str:
55-
return "%s%s" % (args[0], args[1])
55+
return f"{args[0]}{args[1]}"
5656

5757
def attr_splat(self, args: List) -> str:
58-
return ".*%s" % ("".join(args))
58+
args_str = "".join(str(arg) for arg in args)
59+
return f".*{args_str}"
5960

6061
def full_splat_expr_term(self, args: List) -> str:
61-
return "%s%s" % (str(args[0]), str(args[1]))
62+
return f"{args[0]}{args[1]}"
6263

6364
def full_splat(self, args: List) -> str:
64-
return "[*]%s" % ("".join(args))
65+
args_str = "".join(str(arg) for arg in args)
66+
return f"[*]{args_str}"
6567

6668
def tuple(self, args: List) -> List:
6769
return [self.to_string_dollar(arg) for arg in self.strip_new_line_tokens(args)]
@@ -88,7 +90,7 @@ def function_call(self, args: List) -> str:
8890
args_str = ''
8991
if len(args) > 1:
9092
args_str = ", ".join([str(arg) for arg in args[1]])
91-
return "%s(%s)" % (str(args[0]), args_str)
93+
return f"{args[0]}({args_str})"
9294

9395
def arguments(self, args: List) -> List:
9496
return args
@@ -127,7 +129,7 @@ def attribute(self, args: List) -> Attribute:
127129

128130
def conditional(self, args: List) -> str:
129131
args = self.strip_new_line_tokens(args)
130-
return "%s ? %s : %s" % (args[0], args[1], args[2])
132+
return f"{args[0]} ? {args[1]} : {args[2]}"
131133

132134
def binary_op(self, args: List) -> str:
133135
return " ".join([str(arg) for arg in args])
@@ -162,7 +164,7 @@ def body(self, args: List) -> Dict[str, List]:
162164
for arg in args:
163165
if isinstance(arg, Attribute):
164166
if arg.key in result:
165-
raise RuntimeError("{} already defined".format(arg.key))
167+
raise RuntimeError(f"{arg.key} already defined")
166168
result[arg.key] = arg.value
167169
attributes.add(arg.key)
168170
else:
@@ -171,7 +173,7 @@ def body(self, args: List) -> Dict[str, List]:
171173
key = str(key)
172174
if key in result:
173175
if key in attributes:
174-
raise RuntimeError("{} already defined".format(key))
176+
raise RuntimeError(f"{key} already defined")
175177
result[key].append(value)
176178
else:
177179
result[key] = [value]
@@ -188,8 +190,8 @@ def binary_operator(self, args: List) -> str:
188190
def heredoc_template(self, args: List) -> str:
189191
match = HEREDOC_PATTERN.match(str(args[0]))
190192
if not match:
191-
raise RuntimeError("Invalid Heredoc token: %s" % args[0])
192-
return '"%s"' % match.group(2)
193+
raise RuntimeError(f"Invalid Heredoc token: {args[0]}")
194+
return f"\"{match.group(2)}\""
193195

194196
def heredoc_template_trim(self, args: List) -> str:
195197
# See https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions
@@ -198,7 +200,7 @@ def heredoc_template_trim(self, args: List) -> str:
198200
# and then remove that number of spaces from each line
199201
match = HEREDOC_TRIM_PATTERN.match(str(args[0]))
200202
if not match:
201-
raise RuntimeError("Invalid Heredoc token: %s" % args[0])
203+
raise RuntimeError(f"Invalid Heredoc token: {args[0]}")
202204

203205
text = match.group(2)
204206
lines = text.split('\n')
@@ -220,7 +222,7 @@ def new_line_or_comment(self, args: List) -> Discard:
220222
def for_tuple_expr(self, args: List) -> str:
221223
args = self.strip_new_line_tokens(args)
222224
for_expr = " ".join([str(arg) for arg in args[1:-1]])
223-
return '[%s]' % for_expr
225+
return f"[{for_expr}]"
224226

225227
def for_intro(self, args: List) -> str:
226228
args = self.strip_new_line_tokens(args)
@@ -233,7 +235,10 @@ def for_cond(self, args: List) -> str:
233235
def for_object_expr(self, args: List) -> str:
234236
args = self.strip_new_line_tokens(args)
235237
for_expr = " ".join([str(arg) for arg in args[1:-1]])
236-
return '{%s}' % for_expr
238+
# doubled curly braces stands for inlining the braces
239+
# and the third pair of braces is for the interpolation
240+
# e.g. f"{2 + 2} {{2 + 2}}" == "4 {2 + 2}"
241+
return f"{{{for_expr}}}"
237242

238243
def strip_new_line_tokens(self, args: List) -> List:
239244
"""
@@ -247,7 +252,7 @@ def to_string_dollar(self, value: Any) -> Any:
247252
if isinstance(value, str):
248253
if value.startswith('"') and value.endswith('"'):
249254
return str(value)[1:-1]
250-
return '${%s}' % value
255+
return f"${{{value}}}"
251256
return value
252257

253258
def strip_quotes(self, value: Any) -> Any:

mypy.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[mypy]
22
ignore_missing_imports = True
3+
exclude = (?x)(
4+
^build
5+
)

0 commit comments

Comments
 (0)