core, frontends: fix invalid translatable strings.

Some f-strings where used in translatable text, this has been fixed by using explicit
`format()` call (using a script based on `tokenize`).

As tokenize messes with spaces, a reformating tool (`black`) has been applied to some
files afterwards.
This commit is contained in:
Goffi 2021-06-14 18:35:12 +02:00
commit 7adbf3ead8

View file

@ -28,35 +28,47 @@ __commands__ = ["Info"]
class Disco(base.CommandBase):
def __init__(self, host):
extra_outputs = {'default': self.default_output}
extra_outputs = {"default": self.default_output}
super(Disco, self).__init__(
host, 'disco', use_output='complex', extra_outputs=extra_outputs,
help=_('service discovery'))
host,
"disco",
use_output="complex",
extra_outputs=extra_outputs,
help=_("service discovery"),
)
def add_parser_options(self):
self.parser.add_argument("jid", help=_("entity to discover"))
self.parser.add_argument(
"-t", "--type", type=str, choices=('infos', 'items', 'both'), default='both',
help=_("type of data to discover"))
self.parser.add_argument("-n", "--node", default='', help=_("node to use"))
"-t",
"--type",
type=str,
choices=("infos", "items", "both"),
default="both",
help=_("type of data to discover"),
)
self.parser.add_argument("-n", "--node", default="", help=_("node to use"))
self.parser.add_argument(
"-C", "--no-cache", dest='use_cache', action="store_false",
help=_("ignore cache"))
"-C",
"--no-cache",
dest="use_cache",
action="store_false",
help=_("ignore cache"),
)
def default_output(self, data):
features = data.get('features', [])
identities = data.get('identities', [])
extensions = data.get('extensions', {})
items = data.get('items', [])
features = data.get("features", [])
identities = data.get("identities", [])
extensions = data.get("extensions", {})
items = data.get("items", [])
identities_table = common.Table(self.host,
identities,
headers=(_('category'),
_('type'),
_('name')),
use_buffer=True)
identities_table = common.Table(
self.host,
identities,
headers=(_("category"), _("type"), _("name")),
use_buffer=True,
)
extensions_tpl = []
extensions_types = list(extensions.keys())
@ -69,30 +81,35 @@ class Disco(base.CommandBase):
data_keys = list(data.keys())
data_keys.sort()
for key in data_keys:
field_lines.append(A.color('\t', C.A_SUBHEADER, key, A.RESET, ': ',
data[key]))
field_lines.append(
A.color("\t", C.A_SUBHEADER, key, A.RESET, ": ", data[key])
)
if len(values) == 1:
field_lines.append(A.color('\t', C.A_SUBHEADER, "value", A.RESET,
': ', values[0] or (A.BOLD + "UNSET")))
field_lines.append(
A.color(
"\t",
C.A_SUBHEADER,
"value",
A.RESET,
": ",
values[0] or (A.BOLD + "UNSET"),
)
)
elif len(values) > 1:
field_lines.append(A.color('\t', C.A_SUBHEADER, "values", A.RESET,
': '))
field_lines.append(
A.color("\t", C.A_SUBHEADER, "values", A.RESET, ": ")
)
for value in values:
field_lines.append(A.color('\t - ', A.BOLD, value))
fields.append('\n'.join(field_lines))
extensions_tpl.append('{type_}\n{fields}'.format(
type_=type_,
fields='\n\n'.join(fields)))
field_lines.append(A.color("\t - ", A.BOLD, value))
fields.append("\n".join(field_lines))
extensions_tpl.append(
"{type_}\n{fields}".format(type_=type_, fields="\n\n".join(fields))
)
items_table = common.Table(
self.host,
items,
headers=(
_('entity'),
_('node'),
_('name')),
use_buffer=True)
self.host, items, headers=(_("entity"), _("node"), _("name")), use_buffer=True
)
template = []
if features:
@ -104,16 +121,18 @@ class Disco(base.CommandBase):
if items:
template.append(A.color(C.A_HEADER, _("Items")) + "\n\n{items}")
print("\n\n".join(template).format(
features = '\n'.join(features),
identities = identities_table.display().string,
extensions = '\n'.join(extensions_tpl),
items = items_table.display().string,
))
print(
"\n\n".join(template).format(
features="\n".join(features),
identities=identities_table.display().string,
extensions="\n".join(extensions_tpl),
items=items_table.display().string,
)
)
async def start(self):
infos_requested = self.args.type in ('infos', 'both')
items_requested = self.args.type in ('items', 'both')
infos_requested = self.args.type in ("infos", "both")
items_requested = self.args.type in ("items", "both")
jids = await self.host.check_jids([self.args.jid])
jid = jids[0]
@ -126,13 +145,13 @@ class Disco(base.CommandBase):
jid,
node=self.args.node,
use_cache=self.args.use_cache,
profile_key=self.host.profile
profile_key=self.host.profile,
)
except Exception as e:
self.disp(_(f"error while doing discovery: {e}"), error=True)
self.disp(_("error while doing discovery: {e}").format(e=e), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
# items
# items
if not items_requested:
items = None
else:
@ -141,35 +160,34 @@ class Disco(base.CommandBase):
jid,
node=self.args.node,
use_cache=self.args.use_cache,
profile_key=self.host.profile
profile_key=self.host.profile,
)
except Exception as e:
self.disp(_(f"error while doing discovery: {e}"), error=True)
self.disp(_("error while doing discovery: {e}").format(e=e), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
# output
# output
data = {}
if infos_requested:
features, identities, extensions = infos
features.sort()
identities.sort(key=lambda identity: identity[2])
data.update({
'features': features,
'identities': identities,
'extensions': extensions})
data.update(
{"features": features, "identities": identities, "extensions": extensions}
)
if items_requested:
items.sort(key=lambda item: item[2])
data['items'] = items
data["items"] = items
await self.output(data)
self.host.quit()
class Version(base.CommandBase):
class Version(base.CommandBase):
def __init__(self, host):
super(Version, self).__init__(host, 'version', help=_('software version'))
super(Version, self).__init__(host, "version", help=_("software version"))
def add_parser_options(self):
self.parser.add_argument("jid", type=str, help=_("Entity to request"))
@ -180,45 +198,49 @@ class Version(base.CommandBase):
try:
data = await self.host.bridge.getSoftwareVersion(jid, self.host.profile)
except Exception as e:
self.disp(_(f"error while trying to get version: {e}"), error=True)
self.disp(_("error while trying to get version: {e}").format(e=e), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
infos = []
name, version, os = data
if name:
infos.append(_(f"Software name: {name}"))
infos.append(_("Software name: {name}").format(name=name))
if version:
infos.append(_(f"Software version: {version}"))
infos.append(_("Software version: {version}").format(version=version))
if os:
infos.append(_(f"Operating System: {os}"))
infos.append(_("Operating System: {os}").format(os=os))
print("\n".join(infos))
self.host.quit()
class Session(base.CommandBase):
def __init__(self, host):
extra_outputs = {'default': self.default_output}
extra_outputs = {"default": self.default_output}
super(Session, self).__init__(
host, 'session', use_output='dict', extra_outputs=extra_outputs,
help=_('running session'))
host,
"session",
use_output="dict",
extra_outputs=extra_outputs,
help=_("running session"),
)
def add_parser_options(self):
pass
async def default_output(self, data):
started = data['started']
data['started'] = '{short} (UTC, {relative})'.format(
started = data["started"]
data["started"] = "{short} (UTC, {relative})".format(
short=date_utils.date_fmt(started),
relative=date_utils.date_fmt(started, 'relative'))
await self.host.output(C.OUTPUT_DICT, 'simple', {}, data)
relative=date_utils.date_fmt(started, "relative"),
)
await self.host.output(C.OUTPUT_DICT, "simple", {}, data)
async def start(self):
try:
data = await self.host.bridge.sessionInfosGet(self.host.profile)
except Exception as e:
self.disp(_(f'Error getting session infos: {e}'), error=True)
self.disp(_("Error getting session infos: {e}").format(e=e), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
await self.output(data)
@ -226,22 +248,23 @@ class Session(base.CommandBase):
class Devices(base.CommandBase):
def __init__(self, host):
super(Devices, self).__init__(
host, 'devices', use_output=C.OUTPUT_LIST_DICT,
help=_('devices of an entity'))
host, "devices", use_output=C.OUTPUT_LIST_DICT, help=_("devices of an entity")
)
def add_parser_options(self):
self.parser.add_argument(
"jid", type=str, nargs='?', default='', help=_("Entity to request"))
"jid", type=str, nargs="?", default="", help=_("Entity to request")
)
async def start(self):
try:
data = await self.host.bridge.devicesInfosGet(
self.args.jid, self.host.profile)
self.args.jid, self.host.profile
)
except Exception as e:
self.disp(_(f'Error getting devices infos: {e}'), error=True)
self.disp(_("Error getting devices infos: {e}").format(e=e), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
data = data_format.deserialise(data, type_check=list)
@ -254,5 +277,8 @@ class Info(base.CommandBase):
def __init__(self, host):
super(Info, self).__init__(
host, 'info', use_profile=False,
help=_('Get various pieces of information on entities'))
host,
"info",
use_profile=False,
help=_("Get various pieces of information on entities"),
)