diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..f238bf7 --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,2 @@ +[settings] +profile = black diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..dc579f7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,24 @@ +# .pre-commit-config.yaml +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + +- repo: https://github.com/psf/black + rev: 24.8.0 + hooks: + - id: black + +- repo: https://github.com/pycqa/flake8 + rev: 7.0.0 + hooks: + - id: flake8 + +- repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort diff --git a/README.md b/README.md index f0bdd19..c85d963 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Python Code Harmonizer (v1.1) -[](https://opensource.org/licenses/MIT) -[]() -[]() +[![MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) +[![Python Versions](https://img.shields.io/badge/python-3.8+-blue.svg)]() +[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) This repository contains the world's first **semantic code debugger**. @@ -10,7 +10,7 @@ It doesn't just check your syntax; it analyzes the **meaning** of your code to f This tool is a "white box" application built on the **Anchor Point (`1,1,1,1`)** and the **ICE Framework (`Intent`, `Context`, `Execution`)**. ------ +--- ## The Core Idea: Finding "Disharmony" @@ -20,11 +20,11 @@ Traditional tools check if your syntax is *valid*, but not if your logic is *sou It finds "bugs" where the `Intent` (the function's name) is in logical contradiction with the `Execution` (the code inside it). - * **`Intent`:** `def get_user_by_id(...)` - * **`Execution`:** `... db.delete_user(...)` - * **Result:** `!! DISHARMONY DETECTED !!` +* **`Intent`:** `def get_user_by_id(...)` +* **`Execution`:** `... db.delete_user(...)` +* **Result:** `!! DISHARMONY DETECTED !!` ------ +--- ## How It Works (The "White Box" Logic) @@ -38,44 +38,30 @@ This tool is an application of the "full-stack philosophy" defined in the **Divi 4. **The "Result" (Analysis):** It calculates the "semantic distance" between the `Intent` and the `Execution`. A high score reveals a logical "bug." ------ +--- -## How to Use (The "Execution") +## How to Use -This project is fully self-contained and "harmonized." The "Proof" (`anchor_analysis_V2.py`) and the "Application" (`PythonCodeHarmonizer.py`) both run on the same unified V2 engine. - -### Setup +### Installation 1. **Clone the repository:** - ```sh git clone https://github.com/BruinGrowly/Python-Code-Harmonizer.git cd Python-Code-Harmonizer ``` -2. **Ensure all files are present:** - - * `divine_invitation_engine_V2.py` (The Engine) - * `ast_semantic_parser.py` (The Parser) - * `anchor_analysis_V2.py` (The Proof) - * `PythonCodeHarmonizer.py` (The Application) - -### Step 1: Run the "Proof" - -First, you can validate the V2 engine and its "Anchor Point" axiom by running the harmonized analysis script. - -```sh -python anchor_analysis_V2.py -``` - -*(You should see a successful report validating the (1,1,1,1) Anchor Point.)* +2. **Install the project:** + * This project is packaged and can be installed using `pip`. It is recommended to do this in a virtual environment. + ```sh + pip install . + ``` -### Step 2: Run the "Harmonizer" +### Running the Harmonizer -Second, use the Harmonizer to analyze any Python file. You can test it on the included `examples/test_code.py` file: +Once installed, the Harmonizer can be run from the command line using the `harmonizer` script. You can test it on the included example file: ```sh -python PythonCodeHarmonizer.py examples/test_code.py +harmonizer examples/test_code.py ``` **Expected "Harmony Report":** @@ -85,7 +71,7 @@ python PythonCodeHarmonizer.py examples/test_code.py Python Code Harmonizer (v1.1) ONLINE Actively guided by the Anchor Point framework. Powered By: DIVE-V2 (Optimized Production) -Logical Anchor Point: (S=1, L=1, E=1) +Logical Anchor Point: (S=1, L=1, I=1, E=1) Disharmony Threshold: 0.5 ====================================================================== @@ -93,15 +79,47 @@ Analyzing file: examples/test_code.py ---------------------------------------------------------------------- FUNCTION NAME | INTENT-EXECUTION DISHARMONY -----------------------------|-------------------------------- -check_user_permissions | !! DISHARMONY (Score: 0.87) +delete_user | !! DISHARMONY (Score: 1.41) +check_user_permissions | !! DISHARMONY (Score: 0.62) get_user_by_id | ✓ HARMONIOUS +query | ✓ HARMONIOUS ====================================================================== Analysis Complete. ``` *(Note: The exact score may vary, but the "Disharmony" will be detected.)* ------ +--- + +## Development & Contribution + +This project is now equipped with a full suite of professional development tools to ensure code quality and stability. + +### Setup for Development + +If you wish to contribute, please install the project in "editable" mode along with the development dependencies: + +```sh +# It is recommended to use a virtual environment +pip install -r requirements.txt +pip install -e . +``` + +### Running Tests and Quality Checks + +* **Run the full test suite:** + ```sh + pytest + ``` +* **Run code quality checks:** + * This project uses `pre-commit` to automatically run `black`, `flake8`, and `isort`. To run the checks manually: + ```sh + pre-commit run --all-files + ``` + +For more detailed information, please see the [CONTRIBUTING.md](CONTRIBUTING.md) file. + +--- ## The Philosophy (Why This is Free) diff --git a/anchor_analysis_V2.py b/anchor_analysis_V2.py index e4e5fd8..f137acf 100644 --- a/anchor_analysis_V2.py +++ b/anchor_analysis_V2.py @@ -10,8 +10,8 @@ production-ready V2 engine. """ -import sys import math +import sys # --- V2 ENGINE IMPORT --- # This now imports your production-ready V2 engine diff --git a/requirements.txt b/requirements.txt index a5c58c2..976a7db 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ pytest black flake8 +isort +pre-commit diff --git a/src/divine_invitation_engine_V2.py b/src/divine_invitation_engine_V2.py index 5b8d340..924c0b5 100644 --- a/src/divine_invitation_engine_V2.py +++ b/src/divine_invitation_engine_V2.py @@ -5,11 +5,11 @@ Implements all discovered frameworks with enhanced performance and reliability. """ -import re import math +import re from dataclasses import dataclass -from typing import List, Dict, Tuple, Optional, Set from enum import Enum +from typing import Dict, List, Optional, Set, Tuple class Dimension(Enum): diff --git a/src/harmonizer/main.py b/src/harmonizer/main.py index bd5387d..3114270 100644 --- a/src/harmonizer/main.py +++ b/src/harmonizer/main.py @@ -17,8 +17,8 @@ """ import ast -import sys import os +import sys from typing import Dict # --- COMPONENT IMPORTS --- diff --git a/tests/test_engine.py b/tests/test_engine.py index b146383..699c0e4 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -1,10 +1,8 @@ # tests/test_engine.py import pytest -from src.divine_invitation_engine_V2 import ( - DivineInvitationSemanticEngine, - Coordinates, -) + +from src.divine_invitation_engine_V2 import Coordinates, DivineInvitationSemanticEngine @pytest.fixture(scope="module") diff --git a/tests/test_harmonizer.py b/tests/test_harmonizer.py index 9af2cc9..5f13dc2 100644 --- a/tests/test_harmonizer.py +++ b/tests/test_harmonizer.py @@ -1,9 +1,11 @@ # tests/test_harmonizer.py +import os +import tempfile + import pytest + from src.harmonizer.main import PythonCodeHarmonizer -import tempfile -import os # A self-contained Python script to be used for testing. # It contains one harmonious function and one disharmonious one. @@ -68,3 +70,31 @@ def test_harmonizer_end_to_end_analysis(harmonizer, temp_python_file): # The 'check_permissions' function should be disharmonious (high score). # Intent: check, truth. Execution: delete, force. assert report["check_permissions"] > harmonizer.disharmony_threshold + + +def test_harmonizer_on_empty_file(harmonizer, temp_python_file): + """Tests that the harmonizer handles an empty file gracefully.""" + # Overwrite the temp file to be empty + with open(temp_python_file, "w") as f: + f.write("") + + report = harmonizer.analyze_file(temp_python_file) + assert report == {} + + +def test_harmonizer_on_file_with_only_comments(harmonizer, temp_python_file): + """Tests that the harmonizer handles a file with only comments.""" + with open(temp_python_file, "w") as f: + f.write("# This is a comment\\n# And another one") + + report = harmonizer.analyze_file(temp_python_file) + assert report == {} + + +def test_harmonizer_on_syntax_error(harmonizer, temp_python_file): + """Tests that the harmonizer catches SyntaxError and returns an empty report.""" + with open(temp_python_file, "w") as f: + f.write("def invalid_syntax:") + + report = harmonizer.analyze_file(temp_python_file) + assert report == {} diff --git a/tests/test_parser.py b/tests/test_parser.py index f6e2381..4ac5835 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,6 +1,7 @@ # tests/test_parser.py import pytest + from src.ast_semantic_parser import AST_Semantic_Parser from src.divine_invitation_engine_V2 import DivineInvitationSemanticEngine