Simplify / reorganize i18n management for report and description

This commit is contained in:
Alexandre Aubin 2019-07-13 19:52:04 +02:00
parent 0ce4eb0a27
commit af23f53d82
4 changed files with 36 additions and 20 deletions

View file

@ -10,7 +10,6 @@ from yunohost.diagnosis import Diagnoser
class IPDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
description = "internet_connectivity"
cache_duration = 60
def validate_args(self, args):

View file

@ -9,10 +9,9 @@ from moulinette.utils.filesystem import read_file
from yunohost.diagnosis import Diagnoser
from yunohost.domain import domain_list, _build_dns_conf, _get_maindomain
class DNSDiagnoser(Diagnoser):
class DNSRecordsDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
description = "dns_configurations"
cache_duration = 3600*24
def validate_args(self, args):
@ -34,7 +33,7 @@ class DNSDiagnoser(Diagnoser):
main_domain = _get_maindomain()
for domain in self.args["domains"]:
self.logger_info("Diagnosing DNS conf for %s" % domain)
self.logger_debug("Diagnosing DNS conf for %s" % domain)
for report in self.check_domain(domain, domain==main_domain):
yield report
@ -92,5 +91,5 @@ class DNSDiagnoser(Diagnoser):
def main(args, env, loggers):
return DNSDiagnoser(args, env, loggers).diagnose()
return DNSRecordsDiagnoser(args, env, loggers).diagnose()

View file

@ -163,7 +163,8 @@
"diagnosis_dns_bad_conf": "Bad DNS configuration for {domain} : {category}.",
"diagnosis_dns_missing_record": "According to the recommended DNS configuration, you should add a DNS record with type {0}, name {1} and value {2}",
"diagnosis_dns_discrepancy": "According to the recommended DNS configuration, the value for the DNS record with type {0} and name {1} should be {2}, not {3}.",
"dns_configurations": "Domain name configuration (DNS)",
"diagnosis_description_ip": "Internet connectivity",
"diagnosis_description_dnsrecords": "DNS records",
"domain_cannot_remove_main": "Cannot remove main domain. Set a new main domain first",
"domain_cert_gen_failed": "Could not generate certificate",
"domain_created": "Domain created",
@ -245,7 +246,6 @@
"hook_name_unknown": "Unknown hook name '{name:s}'",
"installation_complete": "Installation complete",
"installation_failed": "Something went wrong with the installation",
"internet_connectivity": "Internet connectivity",
"ip6tables_unavailable": "You cannot play with ip6tables here. You are either in a container or your kernel does not support it",
"iptables_unavailable": "You cannot play with iptables here. You are either in a container or your kernel does not support it",
"log_corrupted_md_file": "The YAML metadata file associated with logs is damaged: '{md_file}\nError: {error}'",

View file

@ -64,18 +64,6 @@ def diagnosis_show(categories=[], full=False):
except Exception as e:
logger.error("Failed to fetch diagnosis result for category '%s' : %s" % (category, str(e))) # FIXME : i18n
# "Render" the strings with m18n.n
for report in all_reports:
report["description"] = m18n.n(report["description"])
for r in report["reports"]:
type_, message_key, message_args = r["report"]
r["report"] = (type_, m18n.n(message_key, **message_args))
if "details" in r:
r["details"] = [ m18n.n(key, *values) for key, values in r["details"] ]
return {"reports": all_reports}
def diagnosis_run(categories=[], force=False, args=None):
@ -130,6 +118,13 @@ class Diagnoser():
self.args.update(self.validate_args(self.args))
self.cache_file = Diagnoser.cache_file(self.id_)
descr_key = "diagnosis_description_" + self.id_
self.description = m18n.n(descr_key)
# If no description available, fallback to id
if self.description == descr_key:
self.description = report["id"]
def cached_time_ago(self):
if not os.path.exists(self.cache_file):
@ -145,12 +140,12 @@ class Diagnoser():
if not self.args.get("force", False) and self.cached_time_ago() < self.cache_duration:
self.logger_debug("Cache still valid : %s" % self.cache_file)
# FIXME uhoh that's not consistent with the other return later
return
self.logger_debug("Running diagnostic for %s" % self.id_)
new_report = { "id": self.id_,
"description": self.description,
"cached_for": self.cache_duration,
"reports": list(self.run())
}
@ -158,6 +153,7 @@ class Diagnoser():
# TODO / FIXME : should handle the case where we only did a partial diagnosis
self.logger_debug("Updating cache %s" % self.cache_file)
self.write_cache(new_report)
Diagnoser.i18n(new_report)
return 0, new_report
@ -170,8 +166,30 @@ class Diagnoser():
filename = Diagnoser.cache_file(id_)
report = read_json(filename)
report["timestamp"] = int(os.path.getmtime(filename))
Diagnoser.i18n(report)
return report
@staticmethod
def i18n(report):
# "Render" the strings with m18n.n
# N.B. : we do those m18n.n right now instead of saving the already-translated report
# because we can't be sure we'll redisplay the infos with the same locale as it
# was generated ... e.g. if the diagnosing happened inside a cron job with locale EN
# instead of FR used by the actual admin...
descr_key = "diagnosis_description_" + report["id"]
report["description"] = m18n.n(descr_key)
# If no description available, fallback to id
if report["description"] == descr_key:
report["description"] = report["id"]
for r in report["reports"]:
type_, message_key, message_args = r["report"]
r["report"] = (type_, m18n.n(message_key, **message_args))
if "details" in r:
r["details"] = [ m18n.n(key, *values) for key, values in r["details"] ]
def _list_diagnosis_categories():