-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutil.py
More file actions
153 lines (119 loc) · 4.58 KB
/
util.py
File metadata and controls
153 lines (119 loc) · 4.58 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
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
from typing import Any, Dict
def parse_task_description(task_description_text: str) -> Dict[str, Any]:
result = {}
lines = task_description_text.strip().split("\n")
is_yaml_format = False
if lines and lines[0].strip().startswith("task_description:"):
is_yaml_format = True
if is_yaml_format:
in_task_description = False
task_content_lines = []
remaining_lines = []
for i, line in enumerate(lines):
stripped = line.strip()
if stripped.startswith("task_description:"):
in_task_description = True
continue
elif in_task_description:
if line.startswith(" ") or not stripped:
task_content_lines.append(line[2:] if line.startswith(" ") else "")
else:
remaining_lines = lines[i:]
break
task_content = "\n".join(task_content_lines).strip()
task_lines = task_content.split("\n")
objective_lines = []
in_objective = False
for task_line in task_lines:
task_line = task_line.strip()
if not task_line:
continue
if ":" in task_line and not in_objective:
key, value = task_line.split(":", 1)
key = key.strip().lower().replace(" ", "_")
value = value.strip()
if key == "objective" or key == "instructions":
in_objective = True
if value:
objective_lines.append(value)
else:
result[key] = value
elif in_objective:
objective_lines.append(task_line)
if objective_lines:
result["instructions"] = "\n".join(objective_lines).strip()
for line in remaining_lines:
line = line.strip()
if not line:
continue
if ":" in line:
key, value = line.split(":", 1)
key = key.strip().lower().replace(" ", "_")
value = value.strip()
if key == "parser_name" and value.startswith("<"):
result[key] = value.strip("<>")
else:
result[key] = value
result.setdefault("author_name", "System")
result.setdefault("author_email", "system@example.com")
result.setdefault("difficulty", "medium")
result.setdefault("category", "Backend")
result.setdefault("tags", ["mern"])
result.setdefault("parser_name", "jest")
return result
for line in lines:
line = line.strip()
if not line:
continue
if ":" in line:
key, value = line.split(":", 1)
key = key.strip().lower().replace(" ", "_")
value = value.strip()
if key == "tags" and value.startswith("<"):
result[key] = [tag.strip("<>") for tag in value.split()]
elif key == "parser_name" and value.startswith("<"):
result[key] = value.strip("<>")
else:
result[key] = value
instructions_lines = []
in_instructions = False
for line in lines:
line = line.strip()
if line.startswith("Instructions:"):
in_instructions = True
instructions_lines.append(line.split(":", 1)[1].strip())
elif (
in_instructions
and line
and not line.startswith(
("author_", "difficulty:", "category:", "tags:", "parser_name:")
)
):
instructions_lines.append(line)
if instructions_lines:
result["instructions"] = "\n".join(instructions_lines).strip()
return result
def test_task_description_parser():
sample_content = """task_description |
Task: add is_odd
Task ID: 001
Instructions: add is_odd and have main.py print out whether or not the random
number is odd.
author_name: Author Name
author_email: <you@example.com>
difficulty: easy
category: Feature
tags: <python>
parser_name: <pytest>"""
parsed = parse_task_description(sample_content)
print("Parsed task description:")
print(f"Task: {parsed.get('task')}")
print(f"Task ID: {parsed.get('task_id')}")
print(f"Instructions: {parsed.get('instructions')}")
print(f"Author: {parsed.get('author_name')}")
print(f"Difficulty: {parsed.get('difficulty')}")
print(f"Tags: {parsed.get('tags')}")
print(f"Parser: {parsed.get('parser_name')}")
return parsed
if __name__ == "__main__":
test_task_description_parser()