|
| 1 | +class Range: |
| 2 | + """An object representing where a finding was discovered in content.""" |
| 3 | + def __init__(self, start: int, end: int): |
| 4 | + """Instantiate a Range object. |
| 5 | + :param start: The start of the range. |
| 6 | + :type start: int |
| 7 | + :param end: The end of the range. |
| 8 | + :type end: int |
| 9 | + """ |
| 10 | + self.start = start |
| 11 | + self.end = end |
| 12 | + |
| 13 | + |
| 14 | +class Finding: |
| 15 | + """An object representing an occurrence of a configured detector (i.e. finding) in the provided data.""" |
| 16 | + def __init__(self, |
| 17 | + finding: str, |
| 18 | + redacted_finding: str, |
| 19 | + before_context: str, |
| 20 | + after_context: str, |
| 21 | + detector_name: str, |
| 22 | + detector_uuid: str, |
| 23 | + confidence: str, |
| 24 | + byte_range: Range, |
| 25 | + codepoint_range: Range, |
| 26 | + matched_detection_rule_uuids: list[str], |
| 27 | + matched_detection_rules: list[str]): |
| 28 | + """Instantiate a Finding object. |
| 29 | + :param finding: The data that triggered a detector match. |
| 30 | +
|
| 31 | + :type finding: str |
| 32 | + :param redacted_finding: The redacted finding if redaction was configured, None otherwise. |
| 33 | + :type redacted_finding: str |
| 34 | + :param before_context: The data that immediately preceded the finding if configured, None otherwise. |
| 35 | + :type before_context: str |
| 36 | + :param after_context: The data that immediately succeeded the finding if configured, None otherwise. |
| 37 | + :type after_context: str |
| 38 | + :param detector_name: The the name of the detector, if configured, None otherwise. |
| 39 | + :type detector_name: str |
| 40 | + :param detector_uuid: The ID that uniquely identifies this detector. |
| 41 | + :type detector_uuid: str |
| 42 | + :param confidence: The confidence that the data contained in Finding is an instance of the matched detector. |
| 43 | + :type confidence: str |
| 44 | + :param byte_range: The byte range in which a finding was detected within the item. |
| 45 | + :type byte_range: Range |
| 46 | + :param codepoint_range: The codepoint range in which a finding was detected within the item. This differs |
| 47 | + from byte range since a codepoint may contain multiple bytes. |
| 48 | + :type codepoint_range: Range |
| 49 | + :param matched_detection_rule_uuids: The list of detection rule UUIDs that contained a detector that triggered a |
| 50 | + match. |
| 51 | + :type matched_detection_rule_uuids: list[str] |
| 52 | + :param matched_detection_rules: The list of inline detection rules that contained a detector that triggered a |
| 53 | + match. |
| 54 | + :type matched_detection_rules: list[str] |
| 55 | + """ |
| 56 | + self.finding = finding |
| 57 | + self.redacted_finding = redacted_finding |
| 58 | + self.before_context = before_context |
| 59 | + self.after_context = after_context |
| 60 | + self.detector_name = detector_name |
| 61 | + self.detector_uuid = detector_uuid |
| 62 | + self.confidence = confidence |
| 63 | + self.byte_range = byte_range |
| 64 | + self.codepoint_range = codepoint_range |
| 65 | + self.matched_detection_rule_uuids = matched_detection_rule_uuids |
| 66 | + self.matched_detection_rules = matched_detection_rules |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def from_dict(cls, resp: dict) -> "Finding": |
| 70 | + return cls( |
| 71 | + resp["finding"], |
| 72 | + resp.get("redactedFinding"), |
| 73 | + resp.get("beforeContext"), |
| 74 | + resp.get("afterContext"), |
| 75 | + resp["detector"].get("name"), |
| 76 | + resp["detector"].get("uuid"), |
| 77 | + resp["confidence"], |
| 78 | + Range(resp["location"]["byteRange"]["start"], resp["location"]["byteRange"]["end"]), |
| 79 | + Range(resp["location"]["codepointRange"]["start"], resp["location"]["codepointRange"]["end"]), |
| 80 | + resp["matchedDetectionRuleUUIDs"], |
| 81 | + resp["matchedDetectionRules"] |
| 82 | + ) |
0 commit comments