File size: 4,024 Bytes
e0bfae3 d89bf53 e0bfae3 d89bf53 e0bfae3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | import logging
import os
logger = logging.getLogger("ONYX")
class CodeEngine:
def __init__(self):
self.supported_languages = [
"python",
"kotlin",
"javascript"
]
# dossier où ONYX génère les projets
self.projects_folder = "generated_apps"
# =========================
# GENERATE PROJECT
# =========================
def generate_project(self, request):
request = request.lower()
# créer le dossier principal si nécessaire
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"])
# requirements pour API
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"]
}
# =========================
# GENERATE CODE
# =========================
def generate_code(self, request):
request = request.lower()
# =========================
# FASTAPI TEMPLATE
# =========================
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
}
# =========================
# PYTHON SCRIPT TEMPLATE
# =========================
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
}
# =========================
# ANDROID TEMPLATE
# =========================
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
}
# =========================
# DEFAULT
# =========================
code = """
# Code généré par ONYX
print("Hello from ONYX")
"""
return {
"type": "generic",
"language": "python",
"code": code
}
# =========================
# ANALYZE 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
# =========================
# IMPROVE CODE
# =========================
def improve_code(self, code):
improved = code + "\n# Improved by ONYX"
return improved |