Skip to content

Commit b03560f

Browse files
author
Alan Christie
committed
style: Switch to black
1 parent 8bb0f38 commit b03560f

File tree

6 files changed

+135
-115
lines changed

6 files changed

+135
-115
lines changed

decoder/decode_jinja2_3_0.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,26 @@
77
from jinja2.meta import find_undeclared_variables
88

99

10-
def decode(template_text: str,
11-
variable_map: Optional[Dict[str, str]],
12-
subject: str) -> Tuple[str, bool]:
13-
"""Decodes text expected to conform to Jinja2 (v3.0.x)
14-
"""
10+
def decode(
11+
template_text: str, variable_map: Optional[Dict[str, str]], subject: str
12+
) -> Tuple[str, bool]:
13+
"""Decodes text expected to conform to Jinja2 (v3.0.x)"""
1514
assert template_text
1615
assert subject
1716

1817
# Make a template from the text
19-
env: jinja2.Environment = jinja2.\
20-
Environment(undefined=jinja2.DebugUndefined)
18+
env: jinja2.Environment = jinja2.Environment(undefined=jinja2.DebugUndefined)
2119
try:
2220
template: jinja2.Template = env.from_string(template_text)
2321
except TemplateSyntaxError as ex:
24-
msg: str = f'TemplateSyntaxError with {subject}: {ex}'
22+
msg: str = f"TemplateSyntaxError with {subject}: {ex}"
2523
return msg, False
2624

2725
# Render (this works even if there are variables in the rendered text
2826
# The rendered text, when stripped of whitespace, must not be empty.
2927
rendered_text = template.render(variable_map).strip()
3028
if len(rendered_text) == 0:
31-
msg = f'Rendered text for {subject} is blank'
29+
msg = f"Rendered text for {subject} is blank"
3230
return msg, False
3331

3432
# Check if rendering was done correctly.
@@ -40,9 +38,9 @@ def decode(template_text: str,
4038
undefined: Set[str] = find_undeclared_variables(abstract_syntax_tree)
4139
if undefined:
4240
# We
43-
msg = f'Undefined template variables for {subject}:'
41+
msg = f"Undefined template variables for {subject}:"
4442
for variable in undefined:
45-
msg += f' {variable},'
43+
msg += f" {variable},"
4644
# Return the message, stripping the last comma from it
4745
return msg[:-1], False
4846

decoder/decoder.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,34 @@
1717

1818
# The (built-in) schemas...
1919
# from the same directory as us.
20-
_JD_SCHEMA_FILE: str = os.path.join(os.path.dirname(__file__),
21-
'job-definition-schema.yaml')
22-
_MANIFEST_SCHEMA_FILE: str = os.path.join(os.path.dirname(__file__),
23-
'manifest-schema.yaml')
20+
_JD_SCHEMA_FILE: str = os.path.join(
21+
os.path.dirname(__file__), "job-definition-schema.yaml"
22+
)
23+
_MANIFEST_SCHEMA_FILE: str = os.path.join(
24+
os.path.dirname(__file__), "manifest-schema.yaml"
25+
)
2426

2527
# Load the JD schema YAML file now.
2628
# This must work as the file is installed along with this module.
2729
assert os.path.isfile(_JD_SCHEMA_FILE)
28-
with open(_JD_SCHEMA_FILE, 'r', encoding='utf8') as schema_file:
29-
_JOB_DEFINITION_SCHEMA: Dict[str, Any] =\
30-
yaml.load(schema_file, Loader=yaml.FullLoader)
30+
with open(_JD_SCHEMA_FILE, "r", encoding="utf8") as schema_file:
31+
_JOB_DEFINITION_SCHEMA: Dict[str, Any] = yaml.load(
32+
schema_file, Loader=yaml.FullLoader
33+
)
3134
assert _JOB_DEFINITION_SCHEMA
3235

3336
# Load the Manifest schema YAML file now.
3437
# This must work as the file is installed along with this module.
3538
assert os.path.isfile(_MANIFEST_SCHEMA_FILE)
36-
with open(_MANIFEST_SCHEMA_FILE, 'r', encoding='utf8') as schema_file:
37-
_MANIFEST_SCHEMA: Dict[str, Any] =\
38-
yaml.load(schema_file, Loader=yaml.FullLoader)
39+
with open(_MANIFEST_SCHEMA_FILE, "r", encoding="utf8") as schema_file:
40+
_MANIFEST_SCHEMA: Dict[str, Any] = yaml.load(schema_file, Loader=yaml.FullLoader)
3941
assert _MANIFEST_SCHEMA
4042

4143

4244
class TextEncoding(enum.Enum):
43-
"""A general text encoding format, used initially for Job text fields.
44-
"""
45-
JINJA2_3_0 = 1 # Encoding that complies with Jinja2 v3.0.x
45+
"""A general text encoding format, used initially for Job text fields."""
46+
47+
JINJA2_3_0 = 1 # Encoding that complies with Jinja2 v3.0.x
4648

4749

4850
def validate_manifest_schema(manifest: Dict[str, Any]) -> Optional[str]:
@@ -79,10 +81,12 @@ def validate_job_schema(job_definition: Dict[str, Any]) -> Optional[str]:
7981
return None
8082

8183

82-
def decode(template_text: str,
83-
variable_map: Optional[Dict[str, str]],
84-
subject: str,
85-
template_engine: TextEncoding) -> Tuple[str, bool]:
84+
def decode(
85+
template_text: str,
86+
variable_map: Optional[Dict[str, str]],
87+
subject: str,
88+
template_engine: TextEncoding,
89+
) -> Tuple[str, bool]:
8690
"""Given some text and a 'variable map' (a dictionary of keys and values)
8791
this returns the decoded text (using the named engine) as a string
8892
and a boolean set to True. On failure the boolean is False and the returned
@@ -101,8 +105,8 @@ def decode(template_text: str,
101105
if variable_map is None:
102106
return template_text, True
103107

104-
if template_engine.name.lower() == 'jinja2_3_0':
108+
if template_engine.name.lower() == "jinja2_3_0":
105109
return decode_jinja2_3_0.decode(template_text, variable_map, subject)
106110

107111
# Unsupported engine if we get here!
108-
return f'Unsupported template engine: {template_engine}', False
112+
return f"Unsupported template engine: {template_engine}", False

tests/test_decode_jinja2_3_0.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Tests for the decoder package.
22
import pytest
3+
34
pytestmark = pytest.mark.unit
45

56
from typing import Dict
@@ -9,12 +10,12 @@
910

1011
def test_basic_decode():
1112
# Arrange
12-
text: str = 'foo={{ foo }}, bar={{ bar }}, baz={{ baz }}'
13-
field_map: Dict = {'foo': 1, 'bar': 2, 'baz': 3}
14-
expected_text: str = 'foo=1, bar=2, baz=3'
13+
text: str = "foo={{ foo }}, bar={{ bar }}, baz={{ baz }}"
14+
field_map: Dict = {"foo": 1, "bar": 2, "baz": 3}
15+
expected_text: str = "foo=1, bar=2, baz=3"
1516

1617
# Act
17-
rendered, success = decode_jinja2_3_0.decode(text, field_map, 'text')
18+
rendered, success = decode_jinja2_3_0.decode(text, field_map, "text")
1819

1920
# Assert
2021
assert success
@@ -23,31 +24,33 @@ def test_basic_decode():
2324

2425
def test_decode_with_missing_variables():
2526
# Arrange
26-
text: str = 'foo={{ foo }}, bar={{ bar }}, baz={{ baz }}'
27-
field_map: Dict = {'foo': 1}
28-
expected_text_prefix: str = 'Undefined template variables for text:'
27+
text: str = "foo={{ foo }}, bar={{ bar }}, baz={{ baz }}"
28+
field_map: Dict = {"foo": 1}
29+
expected_text_prefix: str = "Undefined template variables for text:"
2930

3031
# Act
31-
rendered, success = decode_jinja2_3_0.decode(text, field_map, 'text')
32+
rendered, success = decode_jinja2_3_0.decode(text, field_map, "text")
3233

3334
# Assert
3435
assert not success
3536
# The order of missing variables is not deterministic.
3637
# They change - so we just need to make suer they're there.
3738
assert rendered.startswith(expected_text_prefix)
38-
assert 'bar' in rendered
39-
assert 'baz' in rendered
39+
assert "bar" in rendered
40+
assert "baz" in rendered
4041

4142

4243
def test_decode_with_template_error():
4344
# Arrange
44-
text: str = 'foo={{ foo doo }}, bar={{ bar }}, baz={{ baz }}'
45-
field_map: Dict = {'foo': 1}
46-
expected_text: str = "TemplateSyntaxError with text:" \
47-
" expected token 'end of print statement', got 'doo'"
45+
text: str = "foo={{ foo doo }}, bar={{ bar }}, baz={{ baz }}"
46+
field_map: Dict = {"foo": 1}
47+
expected_text: str = (
48+
"TemplateSyntaxError with text:"
49+
" expected token 'end of print statement', got 'doo'"
50+
)
4851

4952
# Act
50-
rendered, success = decode_jinja2_3_0.decode(text, field_map, 'text')
53+
rendered, success = decode_jinja2_3_0.decode(text, field_map, "text")
5154

5255
# Assert
5356
assert not success

tests/test_decoder.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
from typing import Dict
33

44
import pytest
5+
56
pytestmark = pytest.mark.unit
67

78
from decoder import decoder
89

910

1011
def test_jinja2_3_0_decode():
1112
# Arrange
12-
text: str = 'foo={{ foo }}, bar={{ bar }}, baz={{ baz }}'
13-
field_map: Dict = {'foo': 1, 'bar': 2, 'baz': 3}
14-
expected_text: str = 'foo=1, bar=2, baz=3'
13+
text: str = "foo={{ foo }}, bar={{ bar }}, baz={{ baz }}"
14+
field_map: Dict = {"foo": 1, "bar": 2, "baz": 3}
15+
expected_text: str = "foo=1, bar=2, baz=3"
1516

1617
# Act
17-
rendered, success = decoder.decode(text, field_map, 'text',
18-
decoder.TextEncoding.JINJA2_3_0)
18+
rendered, success = decoder.decode(
19+
text, field_map, "text", decoder.TextEncoding.JINJA2_3_0
20+
)
1921

2022
# Assert
2123
assert success
@@ -24,26 +26,28 @@ def test_jinja2_3_0_decode():
2426

2527
def test_jinja2_3_0_decode_with_missing_variables():
2628
# Arrange
27-
text: str = 'foo={{ foo }}, bar={{ bar }}, baz={{ baz }}'
28-
field_map: Dict = {'foo': 1, 'bar': 2}
29+
text: str = "foo={{ foo }}, bar={{ bar }}, baz={{ baz }}"
30+
field_map: Dict = {"foo": 1, "bar": 2}
2931

3032
# Act
31-
rendered, success = decoder.decode(text, field_map, 'text',
32-
decoder.TextEncoding.JINJA2_3_0)
33+
rendered, success = decoder.decode(
34+
text, field_map, "text", decoder.TextEncoding.JINJA2_3_0
35+
)
3336

3437
# Assert
3538
assert not success
36-
assert rendered == 'Undefined template variables for text: baz'
39+
assert rendered == "Undefined template variables for text: baz"
3740

3841

3942
def test_jinja2_3_0_decode_without_variables():
4043
# Arrange
41-
text: str = 'foo=1, bar=2, baz=3'
42-
expected_text: str = 'foo=1, bar=2, baz=3'
44+
text: str = "foo=1, bar=2, baz=3"
45+
expected_text: str = "foo=1, bar=2, baz=3"
4346

4447
# Act
45-
rendered, success = decoder.decode(text, None, 'text',
46-
decoder.TextEncoding.JINJA2_3_0)
48+
rendered, success = decoder.decode(
49+
text, None, "text", decoder.TextEncoding.JINJA2_3_0
50+
)
4751

4852
# Assert
4953
assert success

0 commit comments

Comments
 (0)