Skip to content

Commit 8153531

Browse files
authored
Merge pull request #32 from BruinGrowly/feat-config-file
style: Format code with black
2 parents 8e8040a + fa46e89 commit 8153531

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

harmonizer/ast_semantic_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ def get_intent_concepts(
117117
return [word for word in name_words if word in self.known_vocabulary]
118118
return list(concepts)
119119

120-
def get_execution_map(self, body: List[ast.AST]) -> Tuple[Dict[ast.AST, str], List[str]]:
120+
def get_execution_map(
121+
self, body: List[ast.AST]
122+
) -> Tuple[Dict[ast.AST, str], List[str]]:
121123
"""
122124
Parses the function's body to map each AST node to a semantic dimension
123125
and return the list of concepts found.

harmonizer/main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
# --- CONFIGURATION LOADING ---
3434

35+
3536
def load_configuration() -> Dict:
3637
"""
3738
Searches for and loads .harmonizer.yml from the current directory
@@ -65,6 +66,7 @@ def load_configuration() -> Dict:
6566

6667
# --- THE HARMONIZER APPLICATION ---
6768

69+
6870
class PythonCodeHarmonizer:
6971
"""
7072
Analyzes Python code for "Intent Harmony" using the DIVE-V2
@@ -86,7 +88,9 @@ def __init__(
8688
):
8789
self.config = config if config else {}
8890
self.engine = dive.DivineInvitationSemanticEngine(config=self.config)
89-
self.parser = AST_Semantic_Parser(vocabulary=self.engine.vocabulary.all_keywords)
91+
self.parser = AST_Semantic_Parser(
92+
vocabulary=self.engine.vocabulary.all_keywords
93+
)
9094
self.map_generator = SemanticMapGenerator()
9195
self.disharmony_threshold = disharmony_threshold
9296
self.quiet = quiet
@@ -297,14 +301,20 @@ def parse_cli_arguments() -> argparse.Namespace:
297301
formatter_class=argparse.RawDescriptionHelpFormatter,
298302
)
299303
parser.add_argument("files", nargs="+", help="Python file(s) to analyze")
300-
parser.add_argument("--format", choices=["text", "json"], default="text", help="Output format")
301-
parser.add_argument("--threshold", type=float, default=0.5, help="Disharmony threshold")
304+
parser.add_argument(
305+
"--format", choices=["text", "json"], default="text", help="Output format"
306+
)
307+
parser.add_argument(
308+
"--threshold", type=float, default=0.5, help="Disharmony threshold"
309+
)
302310
parser.add_argument(
303311
"--suggest-refactor",
304312
action="store_true",
305313
help="Suggest a refactoring for disharmonious functions.",
306314
)
307-
parser.add_argument("--version", action="version", version="Python Code Harmonizer v1.4")
315+
parser.add_argument(
316+
"--version", action="version", version="Python Code Harmonizer v1.4"
317+
)
308318
return parser.parse_args()
309319

310320

@@ -350,7 +360,9 @@ def execute_analysis(
350360
exit_code = harmonizer.get_highest_severity_code(report)
351361
highest_exit_code = max(highest_exit_code, exit_code)
352362
if output_format == "text":
353-
formatted = harmonizer.format_report(report, suggest_refactor=suggest_refactor)
363+
formatted = harmonizer.format_report(
364+
report, suggest_refactor=suggest_refactor
365+
)
354366
harmonizer.output_report(formatted)
355367
return all_reports, highest_exit_code
356368

@@ -377,5 +389,6 @@ def run_cli():
377389

378390
sys.exit(highest_exit_code)
379391

392+
380393
if __name__ == "__main__":
381394
run_cli()

harmonizer/refactorer.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class Refactorer:
2020
concrete refactoring strategies.
2121
"""
2222

23-
def __init__(self, function_node: ast.FunctionDef, execution_map: Dict[ast.AST, str]):
23+
def __init__(
24+
self, function_node: ast.FunctionDef, execution_map: Dict[ast.AST, str]
25+
):
2426
self.function_node = function_node
2527
self.execution_map = execution_map
2628

@@ -43,7 +45,10 @@ def suggest_dimensional_split(self) -> str:
4345
ast.Expr(
4446
value=ast.Call(
4547
func=ast.Name(id=new_func_name, ctx=ast.Load()),
46-
args=[ast.Name(id=arg.arg, ctx=ast.Load()) for arg in self.function_node.args.args],
48+
args=[
49+
ast.Name(id=arg.arg, ctx=ast.Load())
50+
for arg in self.function_node.args.args
51+
],
4752
keywords=[],
4853
)
4954
)
@@ -67,10 +72,7 @@ def suggest_dimensional_split(self) -> str:
6772
ast.fix_missing_locations(new_module)
6873
final_code = ast.unparse(new_module)
6974

70-
return (
71-
"# --- Suggested Refactoring: Dimensional Split ---\n\n"
72-
+ final_code
73-
)
75+
return "# --- Suggested Refactoring: Dimensional Split ---\n\n" + final_code
7476

7577
def _group_nodes_by_dimension(self) -> Dict[str, List[ast.AST]]:
7678
"""Groups the function's body nodes by their semantic dimension."""
@@ -79,7 +81,9 @@ def _group_nodes_by_dimension(self) -> Dict[str, List[ast.AST]]:
7981
groups[dimension].append(node)
8082
return groups
8183

82-
def _create_new_function(self, name: str, body_nodes: List[ast.AST]) -> ast.FunctionDef:
84+
def _create_new_function(
85+
self, name: str, body_nodes: List[ast.AST]
86+
) -> ast.FunctionDef:
8387
"""Creates a new function definition from a list of body nodes."""
8488
return ast.FunctionDef(
8589
name=name,

tests/test_refactorer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ def test_dimensional_split_refactoring(parser):
4343
assert len(suggestion_ast.body) == 4 # 3 new functions + 1 rewritten original
4444

4545
# Find the generated functions in the new module
46-
generated_funcs = {node.name: node for node in suggestion_ast.body if isinstance(node, ast.FunctionDef)}
46+
generated_funcs = {
47+
node.name: node
48+
for node in suggestion_ast.body
49+
if isinstance(node, ast.FunctionDef)
50+
}
4751

4852
# Check for the presence of all expected functions
4953
assert "_validate_and_delete_user_justice" in generated_funcs

0 commit comments

Comments
 (0)