Skip to content

Commit 310b618

Browse files
authored
Remove snippet tags from snippet files (#68)
* Strip snippet- tags from snippet-files * Also remove SPDX headers * Gracefully handle first few lines in SPDX header check
1 parent 40b336a commit 310b618

File tree

7 files changed

+90
-4
lines changed

7 files changed

+90
-4
lines changed

aws_doc_sdk_examples_tools/doc_gen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def for_root(
195195
entities_config = yaml.safe_load(file)
196196
for entity, expanded in entities_config["expanded_override"].items():
197197
self.entities[entity] = expanded
198-
except Exception as e:
198+
except Exception:
199199
pass
200200

201201
metadata = root / ".doc_gen/metadata"

aws_doc_sdk_examples_tools/doc_gen_test.py

+11
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,14 @@ def test_doc_gen_encoder(sample_doc_gen: DocGen):
172172
# Verify examples (empty in this case)
173173
assert "examples" in decoded
174174
assert decoded["examples"] == {}
175+
176+
177+
def test_doc_gen_load_snippets():
178+
errors = MetadataErrors()
179+
doc_gen = DocGen(Path(), errors).for_root(
180+
Path(__file__).parent / "test_resources", incremental=False
181+
)
182+
doc_gen.process_metadata(doc_gen.root / "valid_metadata.yaml")
183+
doc_gen.collect_snippets()
184+
assert doc_gen.snippet_files == set(["snippet_file.txt"])
185+
assert doc_gen.snippets["snippet_file.txt"].code == "Line A\nLine C\n"

aws_doc_sdk_examples_tools/metadata_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def test_verify_load_successful():
533533
"php.snippet.tag.1",
534534
"php.snippet.tag.2",
535535
],
536-
snippet_files=[],
536+
snippet_files=["snippet_file.txt"],
537537
)
538538
],
539539
add_services={},

aws_doc_sdk_examples_tools/snippets.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
verify_no_secret_keys,
1616
ValidationConfig,
1717
)
18+
from . import validator_config
1819

1920
SNIPPET_START = "snippet-start:["
2021
SNIPPET_END = "snippet-end:["
@@ -213,10 +214,40 @@ def collect_snippet_files(
213214
file=snippet_file,
214215
line_start=0,
215216
line_end=len(code),
216-
code="".join(code),
217+
code="".join(
218+
strip_snippet_tags(strip_spdx_header(code))
219+
),
217220
)
218221

219222

223+
def strip_snippet_tags(lines: List[str]) -> List[str]:
224+
return [line for line in lines if not has_snippet_tag_or_spdx_header(line)]
225+
226+
227+
def strip_spdx_header(file: List[str]) -> List[str]:
228+
[a, b, c, *_] = file + [""] * 3
229+
has_copyright = (
230+
re.match(validator_config.SPDX_LEADER + validator_config.SPDX_COPYRIGHT, a)
231+
is not None
232+
)
233+
has_license = (
234+
re.match(validator_config.SPDX_LEADER + validator_config.SPDX_LICENSE, b)
235+
is not None
236+
)
237+
has_spacer = "" == c
238+
if has_copyright and has_license:
239+
if has_spacer:
240+
return file[3:]
241+
else:
242+
return file[2:]
243+
else:
244+
return file
245+
246+
247+
def has_snippet_tag_or_spdx_header(line: str) -> bool:
248+
return "snippet-start" in line or "snippet-end" in line
249+
250+
220251
@dataclass
221252
class MissingSnippet(MetadataError):
222253
tag: Optional[str] = None

aws_doc_sdk_examples_tools/snippets_test.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
import pytest
25
from pathlib import Path
36

@@ -65,3 +68,37 @@ def test_verify_snippet_start_end(file_contents: str, expected_error_count: int)
6568
_, errors = snippets.parse_snippets(file_contents.split("\n"), Path("test"), "")
6669
error_count = len(errors)
6770
assert error_count == expected_error_count
71+
72+
73+
def test_strip_snippet_tags():
74+
assert ["Line A", "Line C"] == snippets.strip_snippet_tags(
75+
[
76+
"Line A",
77+
"# snippet-start:[line b]",
78+
"Line C",
79+
"# snippet-end:[line d]",
80+
"line E # snippet-end",
81+
]
82+
)
83+
84+
85+
def test_strip_spdx_header():
86+
assert ["Line A", "Line B"] == snippets.strip_spdx_header(
87+
[
88+
"# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
89+
"# SPDX-License-Identifier: Apache-2.0",
90+
"Line A",
91+
"Line B",
92+
]
93+
)
94+
assert ["Line A", "Line B"] == snippets.strip_spdx_header(
95+
[
96+
"# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
97+
"# SPDX-License-Identifier: Apache-2.0",
98+
"",
99+
"Line A",
100+
"Line B",
101+
]
102+
)
103+
104+
assert [] == snippets.strip_spdx_header([])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Line A
2+
# snippet-start:[line b]
3+
Line C
4+
# snippet-end:[line d]
5+
line E # snippet-end

aws_doc_sdk_examples_tools/test_resources/valid_metadata.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ medical-imaging_TestExample:
3636
snippet_tags:
3737
- php.snippet.tag.1
3838
- php.snippet.tag.2
39+
snippet_files:
40+
- snippet_file.txt
3941
services:
40-
medical-imaging:
42+
medical-imaging:

0 commit comments

Comments
 (0)