| import logging |
| import os |
|
|
| logger = logging.getLogger("ONYX") |
|
|
|
|
| class CodeEngine: |
|
|
| def __init__(self): |
|
|
| self.supported_languages = [ |
| "python", |
| "kotlin", |
| "javascript" |
| ] |
|
|
| |
| self.projects_folder = "generated_apps" |
|
|
| |
| |
| |
|
|
| def generate_project(self, request): |
|
|
| request = request.lower() |
|
|
| |
| os.makedirs(self.projects_folder, exist_ok=True) |
|
|
| project_name = "generated_project" |
|
|
| if "api" in request or "fastapi" in request: |
| project_name = "api_project" |
|
|
| if "android" in request: |
| project_name = "android_project" |
|
|
| project_path = os.path.join(self.projects_folder, project_name) |
|
|
| os.makedirs(project_path, exist_ok=True) |
|
|
| code_data = self.generate_code(request) |
|
|
| file_path = os.path.join(project_path, "main.py") |
|
|
| with open(file_path, "w", encoding="utf-8") as f: |
| f.write(code_data["code"]) |
|
|
| |
| if code_data["type"] == "api": |
|
|
| req_path = os.path.join(project_path, "requirements.txt") |
|
|
| with open(req_path, "w", encoding="utf-8") as f: |
| f.write("fastapi\nuvicorn\n") |
|
|
| logger.info(f"Project generated: {project_path}") |
|
|
| return { |
| "path": project_path, |
| "type": code_data["type"], |
| "language": code_data["language"] |
| } |
|
|
| |
| |
| |
|
|
| def generate_code(self, request): |
|
|
| request = request.lower() |
|
|
| |
| |
| |
|
|
| if "api" in request or "fastapi" in request: |
|
|
| code = """ |
| from fastapi import FastAPI |
| |
| app = FastAPI() |
| |
| |
| @app.get("/") |
| def root(): |
| return {"message": "API running"} |
| """ |
|
|
| return { |
| "type": "api", |
| "language": "python", |
| "code": code |
| } |
|
|
| |
| |
| |
|
|
| if "python" in request or "script" in request: |
|
|
| code = """ |
| def main(): |
| |
| print("Application démarrée") |
| |
| |
| if __name__ == "__main__": |
| main() |
| """ |
|
|
| return { |
| "type": "script", |
| "language": "python", |
| "code": code |
| } |
|
|
| |
| |
| |
|
|
| if "android" in request: |
|
|
| code = """ |
| package com.example.app |
| |
| import android.os.Bundle |
| import androidx.appcompat.app.AppCompatActivity |
| |
| class MainActivity : AppCompatActivity() { |
| |
| override fun onCreate(savedInstanceState: Bundle?) { |
| super.onCreate(savedInstanceState) |
| |
| println("Application Android démarrée") |
| } |
| } |
| """ |
|
|
| return { |
| "type": "android", |
| "language": "kotlin", |
| "code": code |
| } |
|
|
| |
| |
| |
|
|
| code = """ |
| # Code généré par ONYX |
| |
| print("Hello from ONYX") |
| """ |
|
|
| return { |
| "type": "generic", |
| "language": "python", |
| "code": code |
| } |
|
|
| |
| |
| |
|
|
| def analyze_code(self, code): |
|
|
| lines = code.split("\n") |
|
|
| analysis = { |
| "lines": len(lines), |
| "contains_functions": "def " in code, |
| "contains_classes": "class " in code |
| } |
|
|
| return analysis |
|
|
| |
| |
| |
|
|
| def improve_code(self, code): |
|
|
| improved = code + "\n# Improved by ONYX" |
|
|
| return improved |