Skip to content

Commit e40c7a4

Browse files
committed
Allows bare AWS within code-style XML tags.
1 parent 1e506e1 commit e40c7a4

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

aws_doc_sdk_examples_tools/metadata_validator.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import datetime
1313
import os
1414
import re
15+
import xml.etree.ElementTree as xml_tree
1516
import yaml
1617
from dataclasses import dataclass, field
1718
from pathlib import Path
@@ -122,8 +123,7 @@ def _is_valid(self, value: str):
122123
return True
123124
valid = True
124125
if self.check_aws:
125-
# All occurrences of AWS must be entities or within a word.
126-
valid = len(re.findall("(?<![&0-9a-zA-Z])AWS(?![;0-9a-zA-Z])", value)) == 0
126+
valid = self._validate_aws_entity_usage(value)
127127
if not valid:
128128
self.last_err = 'valid string: it contains a non-entity usage of "AWS"'
129129
if valid and self.upper_start:
@@ -156,6 +156,23 @@ def _is_valid(self, value: str):
156156
valid = super()._is_valid(value)
157157
return valid
158158

159+
@staticmethod
160+
def _validate_aws_entity_usage(value: str) -> bool:
161+
"""
162+
All occurrences of AWS must be entities or within a word or within a programlisting or code or noloc block.
163+
164+
Count all bare AWS occurrences within accepted XML tags.
165+
Count all bare AWS occurrences overall.
166+
If these counts differ, there's an invalid usage.
167+
"""
168+
xtree = xml_tree.fromstring(f"<fake>{value.replace('&', '&amp;')}</fake>")
169+
blocks = xtree.findall("programlisting") + xtree.findall("code") + xtree.findall("noloc")
170+
aws_in_blocks = 0
171+
for element in blocks:
172+
aws_in_blocks += len(re.findall("(?<![&0-9a-zA-Z])AWS(?![;0-9a-zA-Z])", element.text))
173+
aws_everywhere = len(re.findall("(?<![&0-9a-zA-Z])AWS(?![;0-9a-zA-Z])", value))
174+
return aws_everywhere == aws_in_blocks
175+
159176

160177
@dataclass
161178
class ValidateYamaleError(MetadataParseError):

0 commit comments

Comments
 (0)