-
Notifications
You must be signed in to change notification settings - Fork 11
/
data_reader.py
170 lines (150 loc) · 4.82 KB
/
data_reader.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import json
from typing import Any, List
from typing import Tuple
from typing import Dict
JsonDict = Dict[str, Any]
class Instruction:
def __init__(
self,
inst_type: str,
text: str,
line_number: str,
line_column: str,
global_idx: str,
description: str,
relativ_pos: str,
):
self.type = inst_type
self.text = text
self.line_number = line_number
self.line_column = line_column
self.global_idx = global_idx
self.description = description
self.relativ_pos = relativ_pos
def GetDescription(self) -> str:
return self.description
class LinterReport:
def __init__(
self,
rule_id: str,
message: str,
evidence: str,
col_begin: str,
col_end: str,
line_begin: str,
line_end: str,
severity: str,
):
self.rule_id = rule_id
self.message = message
self.evidence = evidence
self.col_begin = col_begin
self.col_end = col_end
self.line_begin = line_begin
self.line_end = line_end
self.severity = severity
class DataPoint:
def __init__(
self,
source_code: str,
target_code: str,
warning_line: str,
linter_report: LinterReport,
instructions: List[Instruction],
source_file: str,
target_file: str,
repo: str,
source_filename: str,
target_filename: str,
source_changeid: str,
target_changeid: str,
):
self.source_code = source_code
self.target_code = target_code
self.warning_line = warning_line
self.linter_report = linter_report
self.instructions = instructions
self.source_file = source_file
self.target_file = target_file
self.repo = repo
self.source_filename = source_filename
self.target_filename = target_filename
self.source_changeid = source_changeid
self.target_changeid = target_changeid
def GetDescription(self) -> str:
desc = (
"WARNING\n"
+ self.linter_report.rule_id
+ " "
+ self.linter_report.message
+ " at line: "
+ str(self.linter_report.line_begin)
+ "\n"
)
desc += "WARNING LINE\n" + self.warning_line + "\n"
desc += "SOURCE PATCH\n" + self.source_code + "\nTARGET PATCH\n" + self.target_code + "\n"
desc += "INSTRUCTIONS\n"
for inst in self.instructions:
desc += inst.GetDescription() + "\n"
return desc
def GetT5Representation(self, include_warning: bool) -> Tuple[str, str]:
if include_warning:
inputs = (
"fix "
+ self.linter_report.rule_id
+ " "
+ self.linter_report.message
+ " "
+ self.warning_line
+ ":\n"
+ self.source_code
+ " </s>"
)
else:
inputs = "fix " + self.source_code + " </s>"
outputs = self.target_code + " </s>"
return inputs, outputs
def GetDataAsPython(data_json_path: str) -> List[DataPoint]:
with open(data_json_path, "r", errors="ignore") as f:
data_json = json.load(f)
# converts a data point in json format to a data point in python object
def FromJsonToPython(sample: JsonDict) -> DataPoint:
linter_report = LinterReport(
sample["linter_report"]["rule_id"],
sample["linter_report"]["message"],
sample["linter_report"]["evidence"],
sample["linter_report"]["col_begin"],
sample["linter_report"]["col_end"],
sample["linter_report"]["line_begin"],
sample["linter_report"]["line_end"],
sample["linter_report"]["severity"],
)
instructions = []
for inst in sample["instructions"]:
instruction = Instruction(
inst["type"],
inst["text"],
inst["line_number"],
inst["line_column"],
inst["global_idx"],
inst["description"],
inst["relativ_pos"],
)
instructions.append(instruction)
data_point = DataPoint(
sample["source_code"],
sample["target_code"],
sample["warning_line"],
linter_report,
instructions,
sample["source_file"],
sample["target_file"],
sample["repo"],
sample["source_filename"],
sample["target_filename"],
sample["source_changeid"],
sample["target_changeid"],
)
return data_point
data = [FromJsonToPython(sample) for sample in data_json]
return data