Refactor action registration to use a decorator for user-friendly descriptions
This commit is contained in:
@@ -1,13 +1,6 @@
|
||||
# Registro popolato da tutti i file Toolkit presenti all'avvio.
|
||||
# Registro centrale popolato da tutti i file Toolkit all'avvio.
|
||||
ACTION_DESCRIPTIONS: dict[str, str] = {}
|
||||
|
||||
def register_friendly_actions(actions: dict[str, str]):
|
||||
"""
|
||||
Aggiunge le descrizioni di un Toolkit al registro globale.
|
||||
"""
|
||||
global ACTION_DESCRIPTIONS
|
||||
ACTION_DESCRIPTIONS.update(actions)
|
||||
|
||||
def get_user_friendly_action(tool_name: str) -> str:
|
||||
"""
|
||||
Restituisce un messaggio leggibile e descrittivo per l'utente
|
||||
@@ -15,3 +8,27 @@ def get_user_friendly_action(tool_name: str) -> str:
|
||||
"""
|
||||
# Usa il dizionario ACTION_DESCRIPTIONS importato
|
||||
return ACTION_DESCRIPTIONS.get(tool_name, f"⚙️ Eseguo l'operazione: {tool_name}...")
|
||||
|
||||
def friendly_action(description: str):
|
||||
"""
|
||||
Decoratore che registra automaticamente la descrizione "user-friendly"
|
||||
di un metodo nel registro globale.
|
||||
|
||||
Questo decoratore viene eseguito all'avvio dell'app (quando i file
|
||||
vengono importati) e popola il dizionario ACTION_DESCRIPTIONS.
|
||||
|
||||
Restituisce la funzione originale non modificata.
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
# Registra l'azione
|
||||
tool_name = func.__name__
|
||||
if tool_name in ACTION_DESCRIPTIONS:
|
||||
print(f"Attenzione: Azione '{tool_name}' registrata più volte.")
|
||||
|
||||
ACTION_DESCRIPTIONS[tool_name] = description
|
||||
|
||||
# Restituisce la funzione originale
|
||||
return func
|
||||
|
||||
return decorator
|
||||
Reference in New Issue
Block a user