def match()

in qs_cfn_lint_rules/SentenceCase.py [0:0]


    def match(self, cfn):
        """Basic Matching"""
        matches = []
        title_message = 'Parameter {0} Description is not sentence case: {1}'
        spell_message = 'Parameter {0} contains spelling error(s): {1}'
        stop_message = 'Parameter {0} must end in a full stop "."'

        # Ignore templates that are not entry points
        if not cfn.template.get("Metadata", {}).get("QuickStartDocumentation"):
            return matches

        if self.id in cfn.template.get("Metadata", {}).get("QSLint", {}).get("Exclusions", []):
            return matches
        if "Parameters" not in cfn.template.keys():
            return matches
        else:
            custom_dict = self.get_custom_dict()
            spell = SpellChecker()
            if "Metadata" in cfn.template.keys():
                if "LintSpellExclude" in cfn.template["Metadata"].keys():
                    # add any proper nouns defined in template metadata
                    custom_dict = custom_dict.union(set(cfn.template["Metadata"]["LintSpellExclude"]))
            for x in cfn.template["Parameters"]:
                if "Description" in cfn.template["Parameters"][x].keys():
                    location = ["Parameters", x, "Description"]
                    description = cfn.template["Parameters"][x]["Description"]
                    stop_error = not (description.strip()[-1] == '.' or description.strip()[-2:] == '."')
                    description = strip_urls(description)
                    spell_errors, title_errors = self.get_errors(description, spell, custom_dict)
                    if stop_error:
                        matches.append(RuleMatch(location, stop_message.format(x)))
                    if title_errors:
                        matches.append(RuleMatch(location, title_message.format(x, title_errors)))
                    if spell_errors:
                        matches.append(RuleMatch(location, spell_message.format(x, spell_errors)))
            if "Metadata" not in cfn.template.keys():
                matches.append(RuleMatch(["Parameters"], "Template is missing Parameter labels and groups"))
            elif "AWS::CloudFormation::Interface" not in cfn.template["Metadata"].keys():
                matches.append(RuleMatch(["Metadata"], "Template is missing Parameter labels and groups"))
            elif "ParameterGroups" not in cfn.template["Metadata"]["AWS::CloudFormation::Interface"].keys():
                matches.append(RuleMatch(["Metadata", "AWS::CloudFormation::Interface"],
                                         "Template is missing Parameter groups"))
            elif "ParameterLabels" not in cfn.template["Metadata"]["AWS::CloudFormation::Interface"].keys():
                matches.append(RuleMatch(["Metadata", "AWS::CloudFormation::Interface"],
                                         "Template is missing Parameter labels"))
            else:
                count = 0
                for x in cfn.template["Metadata"]["AWS::CloudFormation::Interface"]["ParameterGroups"]:
                    title_message = 'Parameter Group name "{0}" is not sentence case: {1}'
                    spell_message = 'Parameter Group name "{0}" contains spelling error(s): {1}'
                    if "Label" not in x.keys():
                        matches.append(RuleMatch(["Metadata", "AWS::CloudFormation::Interface", "ParameterGroups", x],
                                                 "Template is missing Parameter groups"))
                    elif "default" not in x["Label"].keys():
                        matches.append(RuleMatch(["Metadata", "AWS::CloudFormation::Interface", "ParameterGroups", x],
                                                 "Template is missing Parameter groups"))
                    else:
                        location = ["Metadata", "AWS::CloudFormation::Interface", "ParameterGroups", count, "Label",
                                    "default"]
                        description = x["Label"]["default"]
                        spell_errors, title_errors = self.get_errors(description, spell, custom_dict)
                        if title_errors:
                            matches.append(RuleMatch(location, title_message.format(x["Label"]["default"], title_errors)))
                        if spell_errors:
                            matches.append(RuleMatch(location, spell_message.format(x["Label"]["default"], spell_errors)))
                    count += 1
                for x in cfn.template["Metadata"]["AWS::CloudFormation::Interface"]["ParameterLabels"]:
                    title_message = 'Parameter Label is not sentence case: {0}'
                    spell_message = 'Parameter Label contains spelling error(s): {0}'
                    if "default" not in cfn.template["Metadata"]["AWS::CloudFormation::Interface"]["ParameterLabels"][x].keys():
                        matches.append(RuleMatch(["Metadata", "AWS::CloudFormation::Interface", "ParameterLabels", x],
                                                 "Template is missing Parameter labels"))
                    else:
                        location = ["Metadata", "AWS::CloudFormation::Interface", "ParameterLabels", x, "default"]
                        description = cfn.template["Metadata"]["AWS::CloudFormation::Interface"]["ParameterLabels"][x]["default"]
                        spell_errors, title_errors = self.get_errors(description, spell, custom_dict)
                        if title_errors:
                            matches.append(RuleMatch(location, title_message.format(title_errors)))
                        if spell_errors:
                            matches.append(RuleMatch(location, spell_message.format(spell_errors)))
                    count += 1
        return matches