forked from pixee/codemodder-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.py
More file actions
65 lines (49 loc) · 1.73 KB
/
result.py
File metadata and controls
65 lines (49 loc) · 1.73 KB
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
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from .utils.abc_dataclass import ABCDataclass
@dataclass
class LineInfo:
line: int
column: int
snippet: str | None
@dataclass
class Location(ABCDataclass):
file: Path
start: LineInfo
end: LineInfo
def match(self, pos):
start_column = self.start.column
end_column = self.end.column
return (
pos.start.line == self.start.line
and (pos.start.column in (start_column - 1, start_column))
and pos.end.line == self.end.line
and (pos.end.column in (end_column - 1, end_column))
)
@dataclass
class Result(ABCDataclass):
rule_id: str
locations: list[Location]
class ResultSet(dict[str, dict[Path, list[Result]]]):
def add_result(self, result: Result):
for loc in result.locations:
self.setdefault(result.rule_id, {}).setdefault(loc.file, []).append(result)
def results_for_rule_and_file(self, rule_id: str, file: Path) -> list[Result]:
return self.get(rule_id, {}).get(file, [])
def files_for_rule(self, rule_id: str) -> list[Path]:
return list(self.get(rule_id, {}).keys())
def all_rule_ids(self) -> list[str]:
return list(self.keys())
def __or__(self, other):
result = ResultSet(super().__or__(other))
for k in self.keys() | other.keys():
result[k] = list_dict_or(self[k], other[k])
return result
def list_dict_or(
dictionary: dict[Any, list[Any]], other: dict[Any, list[Any]]
) -> dict[Path, list[Any]]:
result_dict = other | dictionary
for k in other.keys() | dictionary.keys():
result_dict[k] = dictionary[k] + other[k]
return result_dict