Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle standardize START/END_EXCLUDE for snippets #624

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions synthtool/gcp/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

OPEN_SNIPPET_REGEX = r".*\[START ([a-z0-9_]+)\].*$"
CLOSE_SNIPPET_REGEX = r".*\[END ([a-z0-9_]+)\].*$"
OPEN_EXCLUDE_REGEX = r".*\[START_EXCLUDE\].*$"
CLOSE_EXCLUDE_REGEX = r".*\[END_EXCLUDE\].*$"


def _trim_leading_whitespace(lines: List[str]) -> List[str]:
Expand Down Expand Up @@ -65,23 +67,36 @@ def all_snippets_from_file(sample_file: str) -> Dict[str, str]:
snippet_lines = {} # type: Dict[str, List[str]]
open_snippets = set()
with open(sample_file) as f:
excluding = False
# Iterate over each line:
# - If the line matches an opening snippet tag, add that snippet tag to
# the set of open tags.
# - If the line matches a closing snippet tag, remove that snippet tag
# from the set of open tags.
# - Otherwise, add the line to each of the open snippets
# - If the line matches an opening exclude tag, record that we excluding
# content.
# - If the line matches a closing exclude tag, record that we are capturing
# content again.
# - Otherwise, if we are not excluding content, add the line to each of the
# open snippets
#
# This allows us to handle parsing nested or interleaved snippets.
# This allows us to handle parsing nested or interleaved snippets and ignore
# blocks of code in the snippets
for line in f:
open_match = re.match(pattern=OPEN_SNIPPET_REGEX, string=line)
close_match = re.match(pattern=CLOSE_SNIPPET_REGEX, string=line)
open_exclude_match = re.match(pattern=OPEN_EXCLUDE_REGEX, string=line)
close_exclude_match = re.match(pattern=CLOSE_EXCLUDE_REGEX, string=line)
if open_match:
open_snippets.add(open_match[1])
snippet_lines[open_match[1]] = []
elif close_match:
open_snippets.discard(close_match[1])
else:
elif open_exclude_match:
excluding = True
elif close_exclude_match:
excluding = False
elif not excluding:
for snippet in open_snippets:
snippet_lines[snippet].append(line)

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</dependency>
<!-- [START_EXCLUDE] -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- [END_EXCLUDE] -->
</dependencies>
<!-- [END monitoring_install_with_bom] -->
</project>