68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from django.core.management.base import BaseCommand
|
|
from django.apps import apps
|
|
import inspect
|
|
import importlib
|
|
import yaml
|
|
import os
|
|
from django.conf import settings
|
|
|
|
class Command(BaseCommand):
|
|
help = "Generate YAML support knowledge base from Django views and models"
|
|
|
|
def handle(self, *args, **kwargs):
|
|
output_file = "haikal_kb.yaml"
|
|
kb = {
|
|
"metadata": {
|
|
"system_name": "Haikal",
|
|
"version": "1.0",
|
|
"generated_from": "Django",
|
|
},
|
|
"features": {},
|
|
"glossary": {}
|
|
}
|
|
|
|
def extract_doc(item):
|
|
doc = inspect.getdoc(item)
|
|
return doc.strip() if doc else ""
|
|
|
|
def get_all_views_modules():
|
|
view_modules = []
|
|
for app in settings.INSTALLED_APPS:
|
|
try:
|
|
mod = importlib.import_module(f"{app}.views")
|
|
view_modules.append((app, mod))
|
|
except ImportError:
|
|
continue
|
|
return view_modules
|
|
|
|
def get_all_model_classes():
|
|
all_models = []
|
|
for model in apps.get_models():
|
|
all_models.append((model._meta.app_label, model.__name__, extract_doc(model)))
|
|
return all_models
|
|
|
|
# Extract views
|
|
for app, mod in get_all_views_modules():
|
|
for name, obj in inspect.getmembers(mod, inspect.isfunction):
|
|
doc = extract_doc(obj)
|
|
if doc:
|
|
kb["features"][name] = {
|
|
"description": doc,
|
|
"source": f"{app}.views.{name}",
|
|
"type": "view_function"
|
|
}
|
|
|
|
# Extract models
|
|
for app, name, doc in get_all_model_classes():
|
|
if doc:
|
|
kb["features"][name] = {
|
|
"description": doc,
|
|
"source": f"{app}.models.{name}",
|
|
"type": "model_class"
|
|
}
|
|
|
|
with open(output_file, "w", encoding="utf-8") as f:
|
|
yaml.dump(kb, f, allow_unicode=True, sort_keys=False)
|
|
|
|
self.stdout.write(self.style.SUCCESS(f"✅ YAML knowledge base saved to {output_file}"))
|