File size: 4,865 Bytes
b40c49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

import hashlib
import sys
import uuid
from dataclasses import InitVar, dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from cfnlint.rules import CloudFormationLintRule, RuleMatch


@dataclass(frozen=True, eq=True)
class Match:
    """
    Represents a match found by a CloudFormationLintRule.

    Attributes:
        linenumber (int): The line number where the match was found.
        columnnumber (int): The column number where the match was found.
        linenumberend (int): The ending line number of the match.
        columnnumberend (int): The ending column number of the match.
        filename (str): The name of the file where the match was found.
        rule (CloudFormationLintRule): The rule that found the match.
        message (str): The message associated with the match.
        id (str): A unique identifier for the match.
        parent_id (str): The ID of the parent match,
            if this match is a child of another.

    Methods:
        create(message: str, filename: str, rule: CloudFormationLintRule,
            linenumber: int = None, columnnumber: int = None,
            linenumberend: int = None, columnnumberend: int = None,
            rulematch_obj: RuleMatch = None, parent_id: str = None) -> Match:
            Factory method to create a new Match instance.

    """

    message: str = field(init=True)
    rule: CloudFormationLintRule = field(init=True)
    filename: str | None = field(init=True, default=None)
    linenumber: int = field(init=True, default=1)
    columnnumber: int = field(init=True, default=1)
    linenumberend: int = field(init=True, default=1)
    columnnumberend: int = field(init=True, default=1)
    id: str = field(init=False)
    parent_id: str | None = field(init=True, default=None)
    rulematch_obj: InitVar[RuleMatch | None] = None

    def __post_init__(self, rulematch_obj):
        if sys.version_info.major == 3 and sys.version_info.minor > 8:
            hex_string = hashlib.md5(
                f"{self}".encode("UTF-8"), usedforsecurity=False
            ).hexdigest()
        else:
            hex_string = hashlib.md5(f"{self}".encode("UTF-8")).hexdigest()
        super().__setattr__("id", str(uuid.UUID(hex=hex_string)))

        if rulematch_obj:
            for k, v in vars(rulematch_obj).items():
                if not hasattr(self, k):
                    super().__setattr__(k, v)

    def __repr__(self):
        # use the Posix path to keep things consistent across platforms
        file_str = Path(self.filename).as_posix() + ":" if self.filename else ""
        return f"[{self.rule}] ({self.message}) matched {file_str}{self.linenumber}"

    @classmethod
    def create(
        cls,
        message: str,
        filename: str,
        rule: CloudFormationLintRule,
        linenumber: int | None = None,
        columnnumber: int | None = None,
        linenumberend: int | None = None,
        columnnumberend: int | None = None,
        rulematch_obj: RuleMatch | None = None,
        parent_id: str | None = None,
    ) -> "Match":
        """
        Factory method to create a new Match instance.

        Args:
            message (str): The message associated with the match.
            filename (str): The name of the file where the match was found.
            rule (CloudFormationLintRule): The rule that found the match.
            linenumber (int, optional): The line number where the match
                was found.
            columnnumber (int, optional): The column number where the
                match was found.
            linenumberend (int, optional): The ending line number of
                the match.
            columnnumberend (int, optional): The ending column number of
                the match.
            rulematch_obj (RuleMatch, optional): The RuleMatch object that
                generated this Match.
            parent_id (str, optional): The ID of the parent match, if this
                match is a child of another.

        Returns:
            Match: A new Match instance.
        """

        if columnnumber is None:
            columnnumber = 1
        if columnnumberend is None:
            columnnumberend = columnnumber + 1
        if linenumber is None:
            linenumber = 1
        if linenumberend is None:
            linenumberend = linenumber

        return cls(
            linenumber=linenumber,
            columnnumber=columnnumber,
            linenumberend=linenumberend,
            columnnumberend=columnnumberend,
            filename=filename,
            rule=rule,
            message=message,
            rulematch_obj=rulematch_obj,
            parent_id=parent_id,
        )