Skip to content

Commit c16a894

Browse files
authored
Added python docstring check script (#2741)
* Added python docstring check script * Formatted python files * Fixed indentation for python check * Fixed indentation for testing job * Fixed indentation for most jobs * Fixed dependency for Python-Compliance job * Black formatting * Added scripts directory to format checks * Added scripts directory * Made builds depend on successful tests
1 parent 4a24515 commit c16a894

18 files changed

+3845
-2653
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
ignore = E402,E722,E203,F401,W503
3+
max-line-length = 80
Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Script to encourage documentation addition of changes incurred
1+
#!/usr/bin/env python3
2+
"""Script to encourage documentation addition of changes incurred.
23
34
Methodology:
45
@@ -8,30 +9,40 @@
89
910
This script is to help better document the functionality
1011
11-
NOTE:
12+
Other:
1213
1314
This script complies with our python3 coding and documentation standards
1415
and should be used as a reference guide. It complies with:
1516
16-
1) Pylint
17-
2) Pydocstyle
18-
3) Pycodestyle
19-
4) Flake8
17+
1) Pylint
18+
2) Pydocstyle
19+
3) Pycodestyle
20+
4) Flake8
2021
2122
Run these commands from the CLI to ensure the code is compliant for all
2223
your pull requests.
23-
"""
2424
25+
"""
2526
# Standard imports
2627
import argparse
2728
import os
2829
import sys
30+
import enum
2931

3032
import git
31-
import enum
3233

3334

3435
class DocumentationStatus(enum.Enum):
36+
"""Define the documentation status.
37+
38+
Args:
39+
None
40+
41+
Returns:
42+
None
43+
44+
"""
45+
3546
unknown = 0
3647
updated = 1
3748
not_updated = 2
@@ -52,44 +63,54 @@ def _arg_parser_resolver():
5263
parser = argparse.ArgumentParser()
5364
# getting merge branch name
5465
parser.add_argument(
55-
'--merge_branch_name', type=str, required=True,
56-
help='Name of the merging to branch')
66+
"--merge_branch_name",
67+
type=str,
68+
required=True,
69+
help="Name of the merging to branch",
70+
)
5771
# Github repository
5872
parser.add_argument(
59-
'--repository', type=str, required=True,
60-
help='Name of the GitHub repository in the format "<USERNAME>/<REPO_NAME>"')
73+
"--repository",
74+
type=str,
75+
required=True,
76+
help='''\
77+
Name of the GitHub repository in the format "<USERNAME>/<REPO_NAME>"''',
78+
)
6179
# getting root directory of repository
6280
parser.add_argument(
63-
'--directory', type=str, required=False,
81+
"--directory",
82+
type=str,
83+
required=False,
6484
default=os.getcwd(),
65-
help='The parent directory of files to analyze.')
85+
help="The parent directory of files to analyze.",
86+
)
6687
# Return parser
6788
result = parser.parse_args()
6889
return result
6990

7091

7192
def check_for_documentation(diff_item):
72-
"""Determine the documentation status
93+
"""Determine the documentation status.
7394
74-
Args:
75-
diff_item: Diff to check
95+
Args:
96+
diff_item: Diff to check
7697
77-
Returns:
78-
doc_status: DocumentationStatus
98+
Returns:
99+
doc_status: DocumentationStatus
79100
80101
"""
81102
# Extracting the changes made
82103
file_diffs = diff_item.diff.decode("utf-8")
83104
# Setting documentation status flag to unknown
84105
doc_status = DocumentationStatus.unknown
85106
# Splitting the changes for line by line iteration
86-
lines = file_diffs.split('\n')
107+
lines = file_diffs.split("\n")
87108
# Setting updated doc line count
88109
edited_doc_line_count = 0
89110
# Looping over differences
90111
for line in lines:
91112
# checking if the line was updated and contains documentation
92-
if line.strip() and line.startswith('+') and line.__contains__('///'):
113+
if line.strip() and line.startswith("+") and line.__contains__("///"):
93114
# updating the flag by one
94115
edited_doc_line_count += 1
95116
# Checking if no doc was changed
@@ -99,15 +120,15 @@ def check_for_documentation(diff_item):
99120

100121
# Reading complete file to check if not documentation exist
101122
# Reading the complete file
102-
file = diff_item.b_blob.data_stream.read().decode('utf-8')
123+
file = diff_item.b_blob.data_stream.read().decode("utf-8")
103124
# Splitting the line to check if documentation is present or not
104-
lines = file.split('\n')
125+
lines = file.split("\n")
105126
# Setting the documentation line count flag
106127
doc_lines = 0
107128
# Looping over the file lines
108129
for line in lines:
109130
# Checking if the line contains any documentation or not
110-
if line.strip() and line.__contains__('///'):
131+
if line.strip() and line.__contains__("///"):
111132
# updating the flag by 1
112133
doc_lines += 1
113134
# Checking if the documentation lines were present or not
@@ -128,6 +149,9 @@ def main():
128149
This function finds, and prints the files that exceed the CLI
129150
defined defaults.
130151
152+
Args:
153+
None
154+
131155
Returns:
132156
None
133157
@@ -137,12 +161,15 @@ def main():
137161
# Getting the git repo
138162
repo_feature = git.Repo(args.directory)
139163
(_, repository_directory) = args.repository.split("/")
140-
repo_merge = git.Repo.clone_from("https://github.com/{}.git".format(args.repository), "{}/{}".format(args.directory, repository_directory))
164+
repo_merge = git.Repo.clone_from(
165+
"https://github.com/{}.git".format(args.repository),
166+
"{}/{}".format(args.directory, repository_directory),
167+
)
141168

142169
# Do nothing if the branch has a "/" in it
143-
if '/' in args.merge_branch_name:
170+
if "/" in args.merge_branch_name:
144171
return
145-
172+
146173
# Getting latest commit on latest branch
147174
commit_dev = repo_merge.commit(args.merge_branch_name)
148175
# Getting latest commit on feature branch
@@ -152,35 +179,44 @@ def main():
152179
# Setting a flag to keep record of files and their documentation
153180
lookup = {}
154181
# Lopping over differences in modified files
155-
for diff_item in diff_index.iter_change_type('M'):
182+
for diff_item in diff_index.iter_change_type("M"):
156183
# Getting file path of difference
157184
file_path = diff_item.b_path
158185
# Checking if a file under codebase(lib) directory was modified
159-
if file_path.startswith('lib'):
186+
if file_path.startswith("lib"):
160187
# Getting file documentation status
161188
lookup[file_path] = check_for_documentation(diff_item)
162189
# Lopping over differences in added files
163-
for diff_item in diff_index.iter_change_type('A'):
190+
for diff_item in diff_index.iter_change_type("A"):
164191
# Getting file path of difference
165192
file_path = diff_item.b_path
166193
# Checking if a file under codebase(lib) directory was added
167-
if file_path.startswith('lib'):
194+
if file_path.startswith("lib"):
168195
# Getting file documentation status
169196
lookup[file_path] = check_for_documentation(diff_item)
170197
# Filtering files whose documentation status != updated
171-
filtered_lookup = {k: v for (k, v) in lookup.items() if DocumentationStatus.updated != v}
198+
filtered_lookup = {
199+
k: v for (k, v) in lookup.items() if DocumentationStatus.updated != v
200+
}
172201
# Checking if documentation was updated for all changed files
173202
if len(filtered_lookup) == 0:
174-
print('''🚀 {} Hurrah! documentation was updated in all modified/added files'''.format('\033[92m'))
203+
print(
204+
"""Hurrah! documentation was updated in all modified/added files"""
205+
)
175206
sys.exit(0)
176207
else:
177208
print(
178-
'''🔍 {}DOCUMENTATION NOT UPDATED: Files with missing or not updated DartDoc documentation found'''.format(
179-
'\033[91m'))
209+
"""\
210+
DOCUMENTATION NOT UPDATED: Files with missing or not updated \
211+
DartDoc documentation found"""
212+
)
180213
for failing_file in filtered_lookup:
181-
print('''>>> File name: {}\n\t{}\n'''.format(failing_file, filtered_lookup[failing_file]))
214+
print(
215+
f"""\
216+
>>> File name: {failing_file}\n\t{filtered_lookup[failing_file]}\n"""
217+
)
182218
sys.exit(1)
183219

184220

185-
if __name__ == '__main__':
221+
if __name__ == "__main__":
186222
main()

.github/workflows/talawa_mobile_md_mdx_format_adjuster.py renamed to .github/workflows/archive/talawa_mobile_md_mdx_format_adjuster.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/usr/bin/env python3
2-
# -*- coding: UTF-8 -*-
3-
"""
4-
Script to adjust Dart documentation for MDX compatibility in Docusaurus.
2+
"""Script to adjust Dart documentation for MDX compatibility in Docusaurus.
53
6-
This script scans Dart-generated Markdown files and modifies special characters,
7-
code blocks, and Dart-specific symbols to comply with the MDX syntax used in
8-
Docusaurus v3. It ensures compatibility with the markdown processor by making
4+
This script scans Dart-generated Markdown files and modifies special characters,
5+
code blocks, and Dart-specific symbols to comply with the MDX syntax used in
6+
Docusaurus v3. It ensures compatibility with the markdown processor by making
97
adjustments like escaping certain characters and modifying code blocks.
108
119
This script complies with:
@@ -18,9 +16,11 @@
1816
import argparse
1917
import re
2018

19+
2120
def escape_mdx_characters(text):
22-
"""
23-
Escape special characters (<, >, {, }) in Dart docs to make them MDX compatible.
21+
"""Escape special characters.
22+
23+
Includes (<, >, {, }) in Dart docs to make them MDX compatible.
2424
2525
Args:
2626
text (str): The text content to be processed.
@@ -33,19 +33,19 @@ def escape_mdx_characters(text):
3333
"<": r"(?<!\\)<",
3434
">": r"(?<!\\)>",
3535
"{": r"(?<!\\){",
36-
"}": r"(?<!\\)}"
36+
"}": r"(?<!\\)}",
3737
}
38-
38+
3939
for char, pattern in patterns.items():
4040
text = re.sub(pattern, f"\\{char}", text)
4141

4242
return text
4343

44+
4445
def adjust_dart_code_blocks(text):
45-
"""
46-
Modify Dart code blocks to ensure they are correctly formatted in MDX.
46+
"""Modify Dart code blocks to ensure they are correctly formatted in MDX.
4747
48-
This function replaces Dart code block annotations like `///` or `//`
48+
This function replaces Dart code block annotations like `///` or `//`
4949
and adjusts code block syntax.
5050
5151
Args:
@@ -62,17 +62,20 @@ def adjust_dart_code_blocks(text):
6262

6363
return text
6464

65+
6566
def process_file(filepath):
66-
"""
67-
Process a single Dart Markdown file for MDX compatibility.
67+
"""Process a single Dart Markdown file for MDX compatibility.
68+
69+
Writes the processed content back to the file if any changes occur.
6870
6971
Args:
7072
filepath (str): The path to the Markdown file to be processed.
7173
7274
Returns:
73-
None: Writes the processed content back to the file if any changes occur.
75+
None
76+
7477
"""
75-
with open(filepath, 'r', encoding='utf-8') as file:
78+
with open(filepath, "r", encoding="utf-8") as file:
7679
content = file.read()
7780

7881
# Escape MDX special characters
@@ -81,14 +84,14 @@ def process_file(filepath):
8184
content = adjust_dart_code_blocks(content)
8285

8386
# Write back to the file only if changes were made
84-
with open(filepath, 'w', encoding='utf-8') as file:
87+
with open(filepath, "w", encoding="utf-8") as file:
8588
file.write(content)
8689

90+
8791
def main():
88-
"""
89-
Main function to process all Dart Markdown files in a specified directory.
92+
"""Process all Dart Markdown files in a specified directory.
9093
91-
Scans for Markdown files and processes them for MDX compatibility,
94+
Scans for Markdown files and processes them for MDX compatibility,
9295
especially focusing on Dart-specific docs.
9396
9497
Args:
@@ -97,12 +100,14 @@ def main():
97100
Returns:
98101
None
99102
"""
100-
parser = argparse.ArgumentParser(description="Adjust Dart Markdown files for MDX compatibility.")
103+
parser = argparse.ArgumentParser(
104+
description="Adjust Dart Markdown files for MDX compatibility."
105+
)
101106
parser.add_argument(
102107
"--directory",
103108
type=str,
104109
required=True,
105-
help="Directory containing Markdown files to process."
110+
help="Directory containing Markdown files to process.",
106111
)
107112

108113
args = parser.parse_args()
@@ -113,5 +118,6 @@ def main():
113118
if file.lower().endswith(".md"):
114119
process_file(os.path.join(root, file))
115120

121+
116122
if __name__ == "__main__":
117123
main()

0 commit comments

Comments
 (0)