mock Moulinette.interface and use functools.wraps on @unit_operation

This commit is contained in:
axolotle 2023-01-13 16:17:05 +01:00
parent 710cdd878a
commit e7a2e386a7
2 changed files with 9 additions and 6 deletions

View file

@ -25,6 +25,7 @@ from glob import iglob
from importlib import import_module
from moulinette import m18n, Moulinette
from yunohost.interface import Interface, InterfaceKind
from yunohost.utils.error import YunohostError, YunohostValidationError
from moulinette.utils import log
from moulinette.utils.filesystem import read_yaml, cp
@ -409,7 +410,7 @@ def _hook_exec_bash(path, args, chdir, env, user, return_format, loggers):
env = {}
env["YNH_CWD"] = chdir
env["YNH_INTERFACE"] = Moulinette.interface.type
env["YNH_INTERFACE"] = Interface.kind.value
stdreturn = os.path.join(tempfile.mkdtemp(), "stdreturn")
with open(stdreturn, "w") as f:
@ -511,7 +512,7 @@ def hook_exec_with_script_debug_if_failure(*args, **kwargs):
error = error_message_if_script_failed
logger.error(error_message_if_failed(error))
failure_message_with_debug_instructions = operation_logger.error(error)
if Moulinette.interface.type != "api":
if Interface.kind is not InterfaceKind.API:
operation_logger.dump_script_log_extract_for_debugging()
# Script got manually interrupted ...
# N.B. : KeyboardInterrupt does not inherit from Exception

View file

@ -21,6 +21,7 @@ import re
import yaml
import glob
import psutil
import functools
from typing import List
from datetime import datetime, timedelta
@ -151,7 +152,7 @@ def log_list(limit=None, with_details=False, with_suboperations=False):
operations = list(reversed(sorted(operations, key=lambda o: o["name"])))
# Reverse the order of log when in cli, more comfortable to read (avoid
# unecessary scrolling)
is_api = Moulinette.interface.type == "api"
is_api = Interface.kind is InterfaceKind.API
if not is_api:
operations = list(reversed(operations))
@ -233,7 +234,7 @@ def log_show(
url = yunopaste(content)
logger.info(m18n.n("log_available_on_yunopaste", url=url))
if Moulinette.interface.type == "api":
if Interface.kind is InterfaceKind.API:
return {"url": url}
else:
return
@ -348,6 +349,7 @@ def is_unit_operation(
"""
def decorate(func):
@functools.wraps(func)
def func_wrapper(*args, **kwargs):
op_key = operation_key
if op_key is None:
@ -640,7 +642,7 @@ class OperationLogger:
"operation": self.operation,
"parent": self.parent,
"yunohost_version": get_ynh_package_version("yunohost")["version"],
"interface": Moulinette.interface.type,
"interface": Interface.kind.value,
}
if self.related_to is not None:
data["related_to"] = self.related_to
@ -699,7 +701,7 @@ class OperationLogger:
self.logger.removeHandler(self.file_handler)
self.file_handler.close()
is_api = Moulinette.interface.type == "api"
is_api = Interface.kind is InterfaceKind.API
desc = _get_description_from_name(self.name)
if error is None:
if is_api: