Skip to content

Commit a4574ba

Browse files
committed
Add comments
1 parent 16821e2 commit a4574ba

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

scripts/auto-pr-helper.py

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,41 @@
33
import random
44
import string
55

6-
CHARS = string.ascii_letters + string.digits
7-
ID_LENGTH = 12
86

97
def generate_id(prefix):
10-
"""Generate a random ID with the given prefix."""
8+
"""
9+
Generate a random ID with the given prefix.
10+
"""
11+
12+
CHARS = string.ascii_letters + string.digits
13+
ID_LENGTH = 12
1114
random_part = "".join(random.choice(CHARS) for _ in range(ID_LENGTH))
1215
return f"{prefix}_{random_part}"
1316

1417
def extract_form_fields(issue_body: str) -> dict:
18+
"""
19+
This function parses issues json into a dict of important fields
20+
"""
1521
# Mapping from issue body headers to dict keys
22+
# Changing issues fields name to snake_case (eg. 'Guideline Title' => 'guideline_title')
1623
header_map = {
17-
"Chapter": "Chapter",
18-
"Guideline Title": "Guideline Title",
19-
"Category": "Category",
20-
"Status": "Status",
21-
"Release Begin": "Release Begin",
22-
"Release End": "Release End",
23-
"FLS Paragraph ID": "Fls Paragraph Id",
24-
"Decidability": "Decidability",
25-
"Scope": "Scope",
26-
"Tags": "Tags",
27-
"Amplification": "Amplification",
28-
"Exception(s)": "Exception(S)",
29-
"Rationale": "Rationale",
30-
"Non-Compliant Example - Prose": "Non Compliant Example Prose",
31-
"Non-Compliant Example - Code": "Non Compliant Example Code",
32-
"Compliant Example - Prose": "Compliant Example Prose",
33-
"Compliant Example - Code": "Compliant Example Code",
24+
"Chapter": "chapter",
25+
"Guideline Title": "guideline_title",
26+
"Category": "category",
27+
"Status": "status",
28+
"Release Begin": "release_begin",
29+
"Release End": "release_end",
30+
"FLS Paragraph ID": "fls_id",
31+
"Decidability": "decidability",
32+
"Scope": "scope",
33+
"Tags": "tags",
34+
"Amplification": "amplification",
35+
"Exception(s)": "exceptions",
36+
"Rationale": "rationale",
37+
"Non-Compliant Example - Prose": "non_compliant_ex_prose",
38+
"Non-Compliant Example - Code": "non_compliant_ex",
39+
"Compliant Example - Prose": "compliant_example_prose",
40+
"Compliant Example - Code": "compliant_example",
3441
}
3542

3643
fields = {v: "" for v in header_map.values()}
@@ -41,12 +48,14 @@ def extract_form_fields(issue_body: str) -> dict:
4148

4249
lines.append("### END") # Sentinel to process last field
4350

51+
# Look for '###' in every line, ### represent a sections/field in a guideline
4452
for line in lines:
4553
header_match = re.match(r'^### (.+)$', line.strip())
4654
if header_match:
4755
# Save previous field value if any
4856
if current_key is not None:
4957
value = "\n".join(current_value_lines).strip()
58+
# `_No response_` represents an empty field
5059
if value == "_No response_":
5160
value = ""
5261
if current_key in fields:
@@ -61,6 +70,10 @@ def extract_form_fields(issue_body: str) -> dict:
6170
return fields
6271

6372
def save_guideline_file(content: str, chapter: str):
73+
"""
74+
Appends a guideline str to a chapter
75+
76+
"""
6477
content = "\n" + content + "\n"
6578
# os.makedirs(f"src/coding-guidelines/{chapter}", exist_ok=True)
6679
filename = f"src/coding-guidelines/{chapter.lower()}.rst"
@@ -75,8 +88,14 @@ def save_guideline_file(content: str, chapter: str):
7588
print(f"Saved guideline to {filename}")
7689

7790
def guideline_template(fields: dict) -> str:
91+
"""
92+
This function turns a dictionary that contains the guideline fields
93+
into a proper .rst guideline format
94+
"""
95+
7896

7997
# taken from generate-guideline-templates.py
98+
# to generate random ids
8099
guideline_id = generate_id("gui")
81100
rationale_id = generate_id("rat")
82101
non_compliant_example_id = generate_id("non_compl_ex")
@@ -86,43 +105,43 @@ def get(key):
86105
return fields.get(key, "").strip()
87106

88107

89-
guideline_text = f""".. guideline:: {get('Guideline Title')}
108+
guideline_text = f""".. guideline:: {get('guideline_title')}
90109
:id: {guideline_id}
91-
:category: {get('Category').lower()}
92-
:status: {get('Status').lower()}
93-
:release: {get('Release Begin').lower()}
94-
:fls: {get('Fls Paragraph Id').lower()}
95-
:decidability: {get('Decidability').lower()}
96-
:scope: {get('Scope').lower()}
97-
:tags: {",".join(get('Tags').split(" "))}
110+
:category: {get('category').lower()}
111+
:status: {get('status').lower()}
112+
:release: {get('release_begin').lower()}-{get('release_end')}
113+
:fls: {get('fls_id').lower()}
114+
:decidability: {get('decidability').lower()}
115+
:scope: {get('scope').lower()}
116+
:tags: {",".join(get('tags').split(" "))}
98117
99-
{get('Amplification')}
118+
{get('amplification')}
100119
101120
.. rationale::
102121
:id: {rationale_id}
103-
:status: {get('Status').lower()}
122+
:status: {get('status').lower()}
104123
105-
{get('Rationale')}
124+
{get('rationale')}
106125
107126
.. non_compliant_example::
108127
:id: {non_compliant_example_id}
109-
:status: {get('Status').lower()}
128+
:status: {get('status').lower()}
110129
111-
{get('Non Compliant Example Prose')}
130+
{get('non_compliant_ex_prose')}
112131
113132
.. code-block:: rust
114133
115-
{get('Non Compliant Example Code')}
134+
{get('non_compliant_example')}
116135
117136
.. compliant_example::
118137
:id: {compliant_example_id}
119-
:status: {get('Status').lower()}
138+
:status: {get('status').lower()}
120139
121-
{get('Compliant Example Prose')}
140+
{get('compliant_example_prose')}
122141
123142
.. code-block:: rust
124143
125-
{get('Compliant Example Code')}
144+
{get('compliant_example')}
126145
"""
127146

128147
return guideline_text
@@ -139,6 +158,7 @@ def get(key):
139158

140159
## locally test with `cat scripts/test_issue_sample.json | python3 scripts/auto-pr-helper.py`
141160

161+
# Read json from stdin
142162
issue_json = sys.stdin.read()
143163
issue = json.loads(issue_json)
144164

@@ -147,7 +167,7 @@ def get(key):
147167
issue_body = issue['body']
148168

149169
fields = extract_form_fields(issue_body)
150-
chapter = fields["Chapter"]
170+
chapter = fields["chapter"]
151171

152172

153173
content = guideline_template(fields)

0 commit comments

Comments
 (0)