Propagate changes from moulinette : get rid of msettings, and prompt/display are also wrapped in a 'Moulinette' object

This commit is contained in:
Alexandre Aubin 2021-07-09 21:49:12 +02:00
parent 5e2478d309
commit a2009d6a9a
9 changed files with 41 additions and 40 deletions

View file

@ -36,7 +36,7 @@ import urllib.parse
import tempfile import tempfile
from collections import OrderedDict from collections import OrderedDict
from moulinette import prompt, m18n, msettings from moulinette import Moulinette, m18n
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.network import download_json from moulinette.utils.network import download_json
@ -643,7 +643,7 @@ def app_upgrade(app=[], url=None, file=None, force=False):
m18n.n("app_upgrade_failed", app=app_instance_name, error=error) m18n.n("app_upgrade_failed", app=app_instance_name, error=error)
) )
failure_message_with_debug_instructions = operation_logger.error(error) failure_message_with_debug_instructions = operation_logger.error(error)
if msettings.get("interface") != "api": if Moulinette.interface.type != "api":
dump_app_log_extract_for_debugging(operation_logger) dump_app_log_extract_for_debugging(operation_logger)
# Script got manually interrupted ... N.B. : KeyboardInterrupt does not inherit from Exception # Script got manually interrupted ... N.B. : KeyboardInterrupt does not inherit from Exception
except (KeyboardInterrupt, EOFError): except (KeyboardInterrupt, EOFError):
@ -821,11 +821,11 @@ def app_install(
def confirm_install(confirm): def confirm_install(confirm):
# Ignore if there's nothing for confirm (good quality app), if --force is used # Ignore if there's nothing for confirm (good quality app), if --force is used
# or if request on the API (confirm already implemented on the API side) # or if request on the API (confirm already implemented on the API side)
if confirm is None or force or msettings.get("interface") == "api": if confirm is None or force or Moulinette.interface.type == "api":
return return
if confirm in ["danger", "thirdparty"]: if confirm in ["danger", "thirdparty"]:
answer = prompt( answer = Moulinette.prompt(
m18n.n("confirm_app_install_" + confirm, answers="Yes, I understand"), m18n.n("confirm_app_install_" + confirm, answers="Yes, I understand"),
color="red", color="red",
) )
@ -833,7 +833,7 @@ def app_install(
raise YunohostError("aborting") raise YunohostError("aborting")
else: else:
answer = prompt( answer = Moulinette.prompt(
m18n.n("confirm_app_install_" + confirm, answers="Y/N"), color="yellow" m18n.n("confirm_app_install_" + confirm, answers="Y/N"), color="yellow"
) )
if answer.upper() != "Y": if answer.upper() != "Y":
@ -1005,7 +1005,7 @@ def app_install(
error = m18n.n("app_install_script_failed") error = m18n.n("app_install_script_failed")
logger.error(m18n.n("app_install_failed", app=app_id, error=error)) logger.error(m18n.n("app_install_failed", app=app_id, error=error))
failure_message_with_debug_instructions = operation_logger.error(error) failure_message_with_debug_instructions = operation_logger.error(error)
if msettings.get("interface") != "api": if Moulinette.interface.type != "api":
dump_app_log_extract_for_debugging(operation_logger) dump_app_log_extract_for_debugging(operation_logger)
# Script got manually interrupted ... N.B. : KeyboardInterrupt does not inherit from Exception # Script got manually interrupted ... N.B. : KeyboardInterrupt does not inherit from Exception
except (KeyboardInterrupt, EOFError): except (KeyboardInterrupt, EOFError):
@ -2729,7 +2729,7 @@ class YunoHostArgumentFormatParser(object):
) )
try: try:
question.value = prompt( question.value = Moulinette.prompt(
text_for_user_input_in_cli, self.hide_user_input_in_prompt text_for_user_input_in_cli, self.hide_user_input_in_prompt
) )
except NotImplementedError: except NotImplementedError:

View file

@ -38,7 +38,7 @@ from collections import OrderedDict
from functools import reduce from functools import reduce
from packaging import version from packaging import version
from moulinette import prompt, m18n, msettings from moulinette import Moulinette, m18n
from moulinette.utils import filesystem from moulinette.utils import filesystem
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import read_file, mkdir, write_to_yaml, read_yaml from moulinette.utils.filesystem import read_file, mkdir, write_to_yaml, read_yaml
@ -1508,7 +1508,7 @@ class RestoreManager:
m18n.n("app_restore_failed", app=app_instance_name, error=error) m18n.n("app_restore_failed", app=app_instance_name, error=error)
) )
failure_message_with_debug_instructions = operation_logger.error(error) failure_message_with_debug_instructions = operation_logger.error(error)
if msettings.get("interface") != "api": if Moulinette.interface.type != "api":
dump_app_log_extract_for_debugging(operation_logger) dump_app_log_extract_for_debugging(operation_logger)
# Script got manually interrupted ... N.B. : KeyboardInterrupt does not inherit from Exception # Script got manually interrupted ... N.B. : KeyboardInterrupt does not inherit from Exception
except (KeyboardInterrupt, EOFError): except (KeyboardInterrupt, EOFError):
@ -1839,7 +1839,7 @@ class BackupMethod(object):
# Ask confirmation for copying # Ask confirmation for copying
if size > MB_ALLOWED_TO_ORGANIZE: if size > MB_ALLOWED_TO_ORGANIZE:
try: try:
i = prompt( i = Moulinette.prompt(
m18n.n( m18n.n(
"backup_ask_for_copying_if_needed", "backup_ask_for_copying_if_needed",
answers="y/N", answers="y/N",
@ -2343,7 +2343,7 @@ def backup_restore(name, system=[], apps=[], force=False):
if not force: if not force:
try: try:
# Ask confirmation for restoring # Ask confirmation for restoring
i = prompt( i = Moulinette.prompt(
m18n.n("restore_confirm_yunohost_installed", answers="y/N") m18n.n("restore_confirm_yunohost_installed", answers="y/N")
) )
except NotImplemented: except NotImplemented:
@ -2417,7 +2417,7 @@ def backup_list(with_info=False, human_readable=False):
def backup_download(name): def backup_download(name):
if msettings.get("interface") != "api": if Moulinette.interface.type != "api":
logger.error( logger.error(
"This option is only meant for the API/webadmin and doesn't make sense for the command line." "This option is only meant for the API/webadmin and doesn't make sense for the command line."
) )

View file

@ -28,7 +28,7 @@ import re
import os import os
import time import time
from moulinette import m18n, msettings from moulinette import m18n, Moulinette
from moulinette.utils import log from moulinette.utils import log
from moulinette.utils.filesystem import ( from moulinette.utils.filesystem import (
read_json, read_json,
@ -138,7 +138,7 @@ def diagnosis_show(
url = yunopaste(content) url = yunopaste(content)
logger.info(m18n.n("log_available_on_yunopaste", url=url)) logger.info(m18n.n("log_available_on_yunopaste", url=url))
if msettings.get("interface") == "api": if Moulinette.interface.type == "api":
return {"url": url} return {"url": url}
else: else:
return return
@ -219,7 +219,7 @@ def diagnosis_run(
if email: if email:
_email_diagnosis_issues() _email_diagnosis_issues()
if issues and msettings.get("interface") == "cli": if issues and Moulinette.interface.type == "cli":
logger.warning(m18n.n("diagnosis_display_tip")) logger.warning(m18n.n("diagnosis_display_tip"))
@ -595,7 +595,7 @@ class Diagnoser:
info[1].update(meta_data) info[1].update(meta_data)
s = m18n.n(info[0], **(info[1])) s = m18n.n(info[0], **(info[1]))
# In cli, we remove the html tags # In cli, we remove the html tags
if msettings.get("interface") != "api" or force_remove_html_tags: if Moulinette.interface.type != "api" or force_remove_html_tags:
s = s.replace("<cmd>", "'").replace("</cmd>", "'") s = s.replace("<cmd>", "'").replace("</cmd>", "'")
s = html_tags.sub("", s.replace("<br>", "\n")) s = html_tags.sub("", s.replace("<br>", "\n"))
else: else:

View file

@ -26,7 +26,7 @@
import os import os
import re import re
from moulinette import m18n, msettings, prompt from moulinette import m18n, Moulinette
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
@ -236,8 +236,8 @@ def domain_remove(operation_logger, domain, remove_apps=False, force=False):
if apps_on_that_domain: if apps_on_that_domain:
if remove_apps: if remove_apps:
if msettings.get("interface") == "cli" and not force: if Moulinette.interface.type == "cli" and not force:
answer = prompt( answer = Moulinette.prompt(
m18n.n( m18n.n(
"domain_remove_confirm_apps_removal", "domain_remove_confirm_apps_removal",
apps="\n".join([x[1] for x in apps_on_that_domain]), apps="\n".join([x[1] for x in apps_on_that_domain]),
@ -343,7 +343,7 @@ def domain_dns_conf(domain, ttl=None):
for record in record_list: for record in record_list:
result += "\n{name} {ttl} IN {type} {value}".format(**record) result += "\n{name} {ttl} IN {type} {value}".format(**record)
if msettings.get("interface") == "cli": if Moulinette.interface.type == "cli":
logger.info(m18n.n("domain_dns_conf_is_just_a_recommendation")) logger.info(m18n.n("domain_dns_conf_is_just_a_recommendation"))
return result return result

View file

@ -31,7 +31,7 @@ import mimetypes
from glob import iglob from glob import iglob
from importlib import import_module from importlib import import_module
from moulinette import m18n, msettings from moulinette import m18n, Moulinette
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
from moulinette.utils import log from moulinette.utils import log
from moulinette.utils.filesystem import read_json from moulinette.utils.filesystem import read_json
@ -409,7 +409,7 @@ def _hook_exec_bash(path, args, chdir, env, return_format, loggers):
env = {} env = {}
env["YNH_CWD"] = chdir env["YNH_CWD"] = chdir
env["YNH_INTERFACE"] = msettings.get("interface") env["YNH_INTERFACE"] = Moulinette.interface.type
stdreturn = os.path.join(tempfile.mkdtemp(), "stdreturn") stdreturn = os.path.join(tempfile.mkdtemp(), "stdreturn")
with open(stdreturn, "w") as f: with open(stdreturn, "w") as f:

View file

@ -33,7 +33,7 @@ import psutil
from datetime import datetime, timedelta from datetime import datetime, timedelta
from logging import FileHandler, getLogger, Formatter from logging import FileHandler, getLogger, Formatter
from moulinette import m18n, msettings from moulinette import m18n, Moulinette
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
from yunohost.utils.packages import get_ynh_package_version from yunohost.utils.packages import get_ynh_package_version
@ -44,7 +44,6 @@ CATEGORIES_PATH = "/var/log/yunohost/categories/"
OPERATIONS_PATH = "/var/log/yunohost/categories/operation/" OPERATIONS_PATH = "/var/log/yunohost/categories/operation/"
METADATA_FILE_EXT = ".yml" METADATA_FILE_EXT = ".yml"
LOG_FILE_EXT = ".log" LOG_FILE_EXT = ".log"
RELATED_CATEGORIES = ["app", "domain", "group", "service", "user"]
logger = getActionLogger("yunohost.log") logger = getActionLogger("yunohost.log")
@ -125,7 +124,7 @@ def log_list(limit=None, with_details=False, with_suboperations=False):
operations = list(reversed(sorted(operations, key=lambda o: o["name"]))) operations = list(reversed(sorted(operations, key=lambda o: o["name"])))
# Reverse the order of log when in cli, more comfortable to read (avoid # Reverse the order of log when in cli, more comfortable to read (avoid
# unecessary scrolling) # unecessary scrolling)
is_api = msettings.get("interface") == "api" is_api = Moulinette.interface.type == "api"
if not is_api: if not is_api:
operations = list(reversed(operations)) operations = list(reversed(operations))
@ -214,7 +213,7 @@ def log_show(
url = yunopaste(content) url = yunopaste(content)
logger.info(m18n.n("log_available_on_yunopaste", url=url)) logger.info(m18n.n("log_available_on_yunopaste", url=url))
if msettings.get("interface") == "api": if Moulinette.interface.type == "api":
return {"url": url} return {"url": url}
else: else:
return return
@ -609,7 +608,7 @@ class OperationLogger(object):
"operation": self.operation, "operation": self.operation,
"parent": self.parent, "parent": self.parent,
"yunohost_version": get_ynh_package_version("yunohost")["version"], "yunohost_version": get_ynh_package_version("yunohost")["version"],
"interface": msettings.get("interface"), "interface": Moulinette.interface.type,
} }
if self.related_to is not None: if self.related_to is not None:
data["related_to"] = self.related_to data["related_to"] = self.related_to
@ -663,7 +662,7 @@ class OperationLogger(object):
self.logger.removeHandler(self.file_handler) self.logger.removeHandler(self.file_handler)
self.file_handler.close() self.file_handler.close()
is_api = msettings.get("interface") == "api" is_api = Moulinette.interface.type == "api"
desc = _get_description_from_name(self.name) desc = _get_description_from_name(self.name)
if error is None: if error is None:
if is_api: if is_api:

View file

@ -3,7 +3,7 @@ import pytest
import sys import sys
import moulinette import moulinette
from moulinette import m18n, msettings from moulinette import m18n, Moulinette
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from contextlib import contextmanager from contextlib import contextmanager
@ -81,4 +81,6 @@ def pytest_cmdline_main(config):
import yunohost import yunohost
yunohost.init(debug=config.option.yunodebug) yunohost.init(debug=config.option.yunodebug)
msettings["interface"] = "test" class DummyInterface():
type = "test"
Moulinette._interface = DummyInterface()

View file

@ -30,7 +30,7 @@ import time
from importlib import import_module from importlib import import_module
from packaging import version from packaging import version
from moulinette import prompt, m18n from moulinette import Moulinette, m18n
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.process import check_output, call_async_output from moulinette.utils.process import check_output, call_async_output
from moulinette.utils.filesystem import read_yaml, write_to_yaml from moulinette.utils.filesystem import read_yaml, write_to_yaml
@ -692,7 +692,7 @@ def tools_shutdown(operation_logger, force=False):
if not shutdown: if not shutdown:
try: try:
# Ask confirmation for server shutdown # Ask confirmation for server shutdown
i = prompt(m18n.n("server_shutdown_confirm", answers="y/N")) i = Moulinette.prompt(m18n.n("server_shutdown_confirm", answers="y/N"))
except NotImplemented: except NotImplemented:
pass pass
else: else:
@ -711,7 +711,7 @@ def tools_reboot(operation_logger, force=False):
if not reboot: if not reboot:
try: try:
# Ask confirmation for restoring # Ask confirmation for restoring
i = prompt(m18n.n("server_reboot_confirm", answers="y/N")) i = Moulinette.prompt(m18n.n("server_reboot_confirm", answers="y/N"))
except NotImplemented: except NotImplemented:
pass pass
else: else:

View file

@ -33,7 +33,7 @@ import string
import subprocess import subprocess
import copy import copy
from moulinette import prompt, display, msettings, m18n from moulinette import Moulinette, m18n
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.process import check_output from moulinette.utils.process import check_output
@ -117,18 +117,18 @@ def user_create(
# Validate domain used for email address/xmpp account # Validate domain used for email address/xmpp account
if domain is None: if domain is None:
if msettings.get("interface") == "api": if Moulinette.interface.type == "api":
raise YunohostValidationError( raise YunohostValidationError(
"Invalid usage, you should specify a domain argument" "Invalid usage, you should specify a domain argument"
) )
else: else:
# On affiche les differents domaines possibles # On affiche les differents domaines possibles
display(m18n.n("domains_available")) Moulinette.display(m18n.n("domains_available"))
for domain in domain_list()["domains"]: for domain in domain_list()["domains"]:
display("- {}".format(domain)) Moulinette.display("- {}".format(domain))
maindomain = _get_maindomain() maindomain = _get_maindomain()
domain = prompt( domain = Moulinette.prompt(
m18n.n("ask_user_domain") + " (default: %s)" % maindomain m18n.n("ask_user_domain") + " (default: %s)" % maindomain
) )
if not domain: if not domain:
@ -379,8 +379,8 @@ def user_update(
# when in the cli interface if the option to change the password is called # when in the cli interface if the option to change the password is called
# without a specified value, change_password will be set to the const 0. # without a specified value, change_password will be set to the const 0.
# In this case we prompt for the new password. # In this case we prompt for the new password.
if msettings.get("interface") == "cli" and not change_password: if Moulinette.interface.type == "cli" and not change_password:
change_password = prompt(m18n.n("ask_password"), True, True) change_password = Moulinette.prompt(m18n.n("ask_password"), True, True)
# Ensure sufficiently complex password # Ensure sufficiently complex password
assert_password_is_strong_enough("user", change_password) assert_password_is_strong_enough("user", change_password)