-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check the entire code base for compliance with DOC String standard (#226
) * Update DOC String Complaince * Fix coderabbit suggestions * Update needs in compliance job * Testing without compliance job * Update some issues * Fix some test issues * Fix some test issues * Fix test issues * Fix package issue * Intentional removing doc string * Update check_docstrings.py * Fix excluded dir * Remove raises * Fix black format * Fix some issues * Excluded tests * Fix None parsing * format black * Fix some issues * Add Args and returns for some files * Add Args and returns to some files * Fix linting issue * Fix multiline DOC issue * Fix linting issue * Fix some files * Fix linting * Fix another file * Fix linting * Fix lint issue * Fix interfaces DOC issue * Fix missing descriptions issue * Fix linting issue * Fix some files * Fix linting * Fix some issues * Fix linting issue * Fix some issues * Fix linting issue * Fix device file * Fix linting issue * Reverted back device file * Fix linting issue * Add exlcude for device and snmp_manager file * Fix some issues
- Loading branch information
1 parent
05c4b46
commit e4b8cba
Showing
32 changed files
with
925 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import os | ||
import re | ||
import sys | ||
from docstring_parser import parse | ||
|
||
|
||
def validate_docstring(file_path): | ||
""" | ||
Validate docstrings in a file for compliance with the Google style guide. | ||
Args: | ||
file_path (str): Path to the Python file to validate. | ||
Returns: | ||
list: List of violations found in the file, with details about the issue and corrective action. | ||
""" | ||
print(f"Validating file: {file_path}") | ||
violations = [] | ||
|
||
try: | ||
with open(file_path, "r") as f: | ||
lines = f.readlines() | ||
except Exception as e: | ||
print(f"Failed to read file {file_path}: {e}") | ||
return violations | ||
|
||
for i, line in enumerate(lines): | ||
if re.match(r"^\s*def ", line): | ||
func_name = line.strip() | ||
print(f"Found function definition at line {i + 1}: {func_name}") | ||
|
||
docstring_start = i + 1 | ||
while ( | ||
docstring_start < len(lines) | ||
and lines[docstring_start].strip() == "" | ||
): | ||
docstring_start += 1 | ||
|
||
if docstring_start < len(lines) and lines[ | ||
docstring_start | ||
].strip().startswith('"""'): | ||
print(f"Docstring starts at line {docstring_start + 1}") | ||
docstring_end = docstring_start + 1 | ||
while docstring_end < len(lines) and not lines[ | ||
docstring_end | ||
].strip().endswith('"""'): | ||
docstring_end += 1 | ||
|
||
if docstring_end < len(lines): | ||
docstring = "\n".join( | ||
lines[docstring_start : docstring_end + 1] | ||
) | ||
print( | ||
f"Extracted docstring from lines {docstring_start + 1} to {docstring_end + 1}" | ||
) | ||
|
||
try: | ||
parsed = parse(docstring) | ||
print("Parsed docstring successfully") | ||
|
||
# Check for Args section | ||
if "Args:" not in docstring: | ||
print("Missing 'Args' section") | ||
violations.append( | ||
{ | ||
"line": i + 1, | ||
"function": func_name, | ||
"issue": "Missing 'Args' section", | ||
"action": "Add an 'Args' section listing the arguments this function accepts.", | ||
} | ||
) | ||
|
||
# Check for Returns section | ||
if "Returns:" not in docstring: | ||
print("Missing 'Returns' section") | ||
violations.append( | ||
{ | ||
"line": i + 1, | ||
"function": func_name, | ||
"issue": "Missing 'Returns' section", | ||
"action": "Add a 'Returns' section describing the return value.", | ||
} | ||
) | ||
|
||
except Exception as e: | ||
print(f"Error parsing docstring: {e}") | ||
violations.append( | ||
{ | ||
"line": i + 1, | ||
"function": func_name, | ||
"issue": "Docstring parsing error", | ||
"action": f"Ensure the docstring is properly formatted: {e}", | ||
} | ||
) | ||
else: | ||
print("Docstring does not close properly") | ||
violations.append( | ||
{ | ||
"line": i + 1, | ||
"function": func_name, | ||
"issue": "Unclosed docstring", | ||
"action": "Ensure the docstring is properly closed with triple quotes.", | ||
} | ||
) | ||
else: | ||
print("Missing docstring for function") | ||
violations.append( | ||
{ | ||
"line": i + 1, | ||
"function": func_name, | ||
"issue": "Missing docstring", | ||
"action": "Add a Google-style docstring to describe this function.", | ||
} | ||
) | ||
|
||
print(f"Found {len(violations)} violations in file: {file_path}") | ||
return violations | ||
|
||
|
||
def check_directory(directory, exclude_dirs=None): | ||
""" | ||
Check all Python files in a directory for docstring compliance, excluding specified directories. | ||
Args: | ||
directory (str): Directory to scan. | ||
exclude_dirs (list): List of directories to exclude. | ||
Returns: | ||
dict: Dictionary of file violations. | ||
""" | ||
if exclude_dirs is None: | ||
exclude_dirs = [] | ||
|
||
all_violations = {} | ||
for root, dirs, files in os.walk(directory): | ||
# Skip excluded directories | ||
dirs[:] = [ | ||
d for d in dirs if os.path.join(root, d) not in exclude_dirs | ||
] | ||
|
||
for file in files: | ||
if file.endswith(".py"): | ||
file_path = os.path.join(root, file) | ||
print(f"Processing file: {file_path}") | ||
violations = validate_docstring(file_path) | ||
if violations: | ||
all_violations[file_path] = violations | ||
|
||
print(f"Completed scanning directory: {directory}") | ||
return all_violations | ||
|
||
|
||
def main(): | ||
""" | ||
Args: | ||
None | ||
Returns: | ||
None | ||
""" | ||
directory = sys.argv[1] if len(sys.argv) > 1 else "." | ||
# Define excluded directories (e.g., virtual environment or library folders) | ||
exclude_dirs = [ | ||
os.path.join(directory, "venv"), | ||
os.path.join(directory, "lib"), | ||
os.path.join(directory, "venv/lib/python3.11/site-packages"), | ||
os.path.join(directory, "tests"), | ||
os.path.join(directory, "switchmap/poller/snmp"), | ||
os.path.join(directory, "switchmap/server/db/ingest"), | ||
os.path.join(directory, "switchmap/server/db/ingest/update"), | ||
] | ||
violations = check_directory(directory, exclude_dirs=exclude_dirs) | ||
if violations: | ||
print("Docstring violations found:") | ||
for file, issues in violations.items(): | ||
for issue in issues: | ||
print( | ||
f"{file}:{issue['line']}: {issue['function']}: {issue['issue']}" | ||
) | ||
print(f" Corrective Action: {issue['action']}") | ||
sys.exit(1) | ||
else: | ||
print("All docstrings are compliant.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ graphql_server==3.0.0b5 | |
mock | ||
pytest | ||
|
||
# Docstring Parser | ||
docstring_parser | ||
|
||
# Other | ||
more-itertools | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.