Skip to content

Commit

Permalink
fix: improve issue generation script
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnu-deepsource committed Oct 31, 2023
1 parent 53d67a7 commit 7034f03
Showing 1 changed file with 20 additions and 77 deletions.
97 changes: 20 additions & 77 deletions analyzers/cfn-lint/utils/issue_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,20 @@
import os
from textwrap import dedent
from urllib.parse import unquote, urlparse
import argparse
from pathlib import Path
from typing import List, Dict, Optional, Union


def concat_binop(binop):
"""
Concatenate the values of binary operations in AST.
Args:
- binop (ast.BinOp): Binary operation node from AST
Returns:
- str: Concatenated value of binary operation
"""
def concat_binop(binop: ast.AST) -> str:
if isinstance(binop, ast.BinOp):
return concat_binop(binop.left) + concat_binop(binop.right)
elif isinstance(binop, ast.Constant):
return binop.value
return ""


def extract_class_attributes(node):
"""
Extract specific attributes from a class node in AST.
Args:
- node (ast.ClassDef): Class definition node in AST
Returns:
- dict: Extracted attributes from class
"""
def extract_class_attributes(node: ast.ClassDef) -> Dict[str, Union[str, List[str]]]:
class_data = {}
for item in node.body:
if isinstance(item, ast.Assign):
Expand All @@ -45,56 +30,26 @@ def extract_class_attributes(node):
return class_data


def extract_attributes_from_code(code):
"""
Extract specific attributes from a class in the provided code.
Args:
- code (str): Source code to extract data from
Returns:
- dict: Extracted attributes from class in code
"""
def extract_attributes_from_code(code: str) -> Dict[str, Union[str, List[str]]]:
class_data = {}
tree = ast.parse(code)

for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
class_data.update(extract_class_attributes(node))
return class_data


def extract_page_name(url):
"""
Extract and format the page name from a given URL.
Args:
- url (str): URL to extract page name from
Returns:
- str: Formatted page name
"""
def extract_page_name(url: str) -> Optional[str]:
parsed_url = urlparse(url)
path_segments = parsed_url.path.strip("/").split("/")
if path_segments:
last_segment = os.path.splitext(path_segments[-1])[0]
page_name = unquote(last_segment.replace("-", " ")).title()
page_name = page_name.replace("Cfn", "CloudFormation")
page_name = page_name.replace("Cloudformation", "CloudFormation")
return page_name
return page_name.replace("Cfn", "CloudFormation").replace("Cloudformation", "CloudFormation")
return None


def build_toml(issue):
"""
Build a TOML string from issue data.
Args:
- issue (dict): Issue data
Returns:
- str: Issue data in TOML format
"""
def build_toml(issue: Dict[str, Union[str, List[str]]]) -> str:
title = issue["shortdesc"]
description = issue["description"]
source_url = issue.get("source_url", "")
Expand All @@ -116,30 +71,14 @@ def build_toml(issue):
return dedent(content)


def write_to_file(issue):
"""
Write issue data to a TOML file.
Args:
- issue (dict): Issue data
"""
def write_to_file(issue: Dict[str, Union[str, List[str]]]) -> None:
file_name = f"./issues/CFLIN-{issue['id']}.toml"
with open(file_name, "w") as file:
file.write(build_toml(issue))


def extract_attributes_from_directory(directory):
"""
Extract class attributes from all Python files in a given directory.
Args:
- directory (str): Directory path to extract attributes from
Returns:
- list: List of dictionaries with extracted class attributes
"""
def extract_attributes_from_directory(directory: str) -> List[Dict[str, Union[str, List[str]]]]:
all_classes_data = []

for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".py"):
Expand All @@ -152,14 +91,18 @@ def extract_attributes_from_directory(directory):
all_classes_data.append(class_data)
except Exception as e:
print(f"Error parsing file {file}: {e}")

return all_classes_data


if __name__ == "__main__":
base_directory = "/Users/vishnu/Code/deepsource/"
rules_directory = "./cfn-lint/src/cfnlint/rules"
directory = os.path.join(base_directory, rules_directory)
attributes_list = extract_attributes_from_directory(directory)
parser = argparse.ArgumentParser(description="Extract attributes from Python files in a given directory.")
parser.add_argument("--root_directory", help="Root directory of the cfn-lint repository.")
args = parser.parse_args()

directory = Path(args.root_directory)
path = Path("cfn-lint/src/cfnlint/rules")
rules_directory = directory / path

attributes_list = extract_attributes_from_directory(rules_directory)
for attributes in attributes_list:
write_to_file(attributes)

0 comments on commit 7034f03

Please sign in to comment.