Skip to content

Commit

Permalink
fix(snippets): trim leading whitespace when extracting snippets (#449)
Browse files Browse the repository at this point in the history
* fix(snippets): trim leading whitespace when extracting snippets

* docs: add docstring for _trim_leading_whitespace
  • Loading branch information
chingor13 authored Mar 19, 2020
1 parent c73d13f commit bcad3e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
34 changes: 33 additions & 1 deletion synthtool/gcp/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@
CLOSE_SNIPPET_REGEX = r".*\[END ([a-z0-9_]+)\].*$"


def _trim_leading_whitespace(lines: List[str]) -> List[str]:
"""Trims leading, plain spaces from the snippet content. Finds the minimum
number of leading spaces, ignoring empty lines, and removes that number of
spaces from each line.
Args:
lines (List[str]): Lines of content. These lines are newline terminated.
Returns:
List of trimmed lines.
"""

def number_of_leading_spaces(input: str) -> int:
return len(input) - len(input.lstrip(" "))

def is_empty_line(input: str) -> bool:
if re.match(r"^\s*$", input):
return True
return False

leading_spaces = [
number_of_leading_spaces(line) for line in lines if not is_empty_line(line)
]
max_leading_spaces = min(leading_spaces)
return [
"\n" if is_empty_line(line) else line[max_leading_spaces:] for line in lines
]


def all_snippets_from_file(sample_file: str) -> Dict[str, str]:
"""Reads in a sample file and parse out all contained snippets.
Expand Down Expand Up @@ -56,7 +85,10 @@ def all_snippets_from_file(sample_file: str) -> Dict[str, str]:
for snippet in open_snippets:
snippet_lines[snippet].append(line)

return {snippet: "".join(lines) for snippet, lines in snippet_lines.items()}
return {
snippet: "".join(_trim_leading_whitespace(lines))
for snippet, lines in snippet_lines.items()
}


def all_snippets(snippet_globs: List[str]) -> Dict[str, str]:
Expand Down
26 changes: 13 additions & 13 deletions tests/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ def test_load_snippets():
)
assert (
all_snippets["monitoring_install_with_bom"]
== """ <dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>3.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
== """<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
<artifactId>libraries-bom</artifactId>
<version>3.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</dependency>
</dependencies>
"""
)

Expand Down

0 comments on commit bcad3e0

Please sign in to comment.