diff --git a/locale/rougail_output_ansible.pot b/locale/rougail_output_ansible.pot index 39e0760..405467e 100644 --- a/locale/rougail_output_ansible.pot +++ b/locale/rougail_output_ansible.pot @@ -5,7 +5,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" -"POT-Creation-Date: 2025-04-29 23:02+0200\n" +"POT-Creation-Date: 2025-05-03 22:46+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/rougail/output_ansible/__init__.py b/src/rougail/output_ansible/__init__.py index ca5c9da..2fd138b 100644 --- a/src/rougail/output_ansible/__init__.py +++ b/src/rougail/output_ansible/__init__.py @@ -55,20 +55,26 @@ class RougailOutputAnsible(RougailOutputJson): def manage_errors(self) -> bool: if not super().manage_errors(): if not self.support_namespace: - self.errors.append(_('no namespace configured')) - hosts_config = self.config.option('hosts') + self.errors.append(_("no namespace configured")) + hosts_config = self.config.option("hosts") try: if hosts_config.group_type() != groups.namespace: hosts_config = None except AttributeError: hosts_config = None if not hosts_config: - self.errors.append(_('cannot find host namespace "{0}"').format(self.host_namespace)) + self.errors.append( + _('cannot find host namespace "{0}"').format(self.host_namespace) + ) else: try: - hosts_config.option('hostnames').name() + hosts_config.option("hostnames").name() except AttributeError: - self.errors.append(_('malformated host namespace "{0}", should have the "hostnames" key').format(self.host_namespace)) + self.errors.append( + _( + 'malformated host namespace "{0}", should have the "hostnames" key' + ).format(self.host_namespace) + ) return super().manage_errors() return True @@ -85,53 +91,67 @@ class RougailOutputAnsible(RougailOutputJson): def json_to_ansible(self): ret = {"_meta": {"hostvars": {}}, "all": {"children": ["ungrouped"]}} - if '_warnings' in self.dico: - ret["_meta"]["hostvars"]["localhost"] = {'_warnings': self.dico.pop('_warnings')} + if "_warnings" in self.dico: + ret["_meta"]["hostvars"]["localhost"] = { + "_warnings": self.dico.pop("_warnings") + } ret["ungrouped"] = {"hosts": ["localhost"]} - if '_errors' in self.dico: - ret["_meta"]["hostvars"].setdefault("localhost", {})['_errors'] = self.dico.pop('_errors') + if "_errors" in self.dico: + ret["_meta"]["hostvars"].setdefault("localhost", {})["_errors"] = ( + self.dico.pop("_errors") + ) if "ungrouped" not in ret: ret["ungrouped"] = {"hosts": ["localhost"]} if self.host_namespace in self.dico: hosts = self.dico.pop(self.host_namespace) if "hostnames" not in hosts: - ret["_meta"]["hostvars"].setdefault("localhost", {}).setdefault('_errors', []).append(_('cannot find "hostnames" in "{0}" namespace').format(self.host_namespace)) + ret["_meta"]["hostvars"].setdefault("localhost", {}).setdefault( + "_errors", [] + ).append( + _('cannot find "hostnames" in "{0}" namespace').format( + self.host_namespace + ) + ) if "ungrouped" not in ret: ret["ungrouped"] = {"hosts": ["localhost"]} hostnames = {} else: - hostnames = hosts['hostnames'] + hostnames = hosts["hostnames"] ret_hosts = {} for name, hosts in hostnames.items(): - if 'hosts' in hosts: - if 'prefix_name' in hosts: - host_name = hosts['prefix_name'] + if "hosts" in hosts: + if "prefix_name" in hosts: + host_name = hosts["prefix_name"] add_index = True elif len(hosts["hosts"]) == 1: host_name = hosts["hosts"][0] add_index = False else: raise Exception("cannot find prefix_name") - for idx, host in enumerate(hosts['hosts']): + for idx, host in enumerate(hosts["hosts"]): index = str(idx + 1) if idx < 9: - index = '0' + index - if 'prefix_name' in hosts: - host_name = hosts['prefix_name'] + index + index = "0" + index + if "prefix_name" in hosts: + host_name = hosts["prefix_name"] + index ret_hosts.setdefault(name, {})[host_name] = host - ret.setdefault(name, {}).setdefault('hosts', []).append(host_name) + ret.setdefault(name, {}).setdefault("hosts", []).append( + host_name + ) else: ret["all"]["children"].append(name) ret[name] = hosts for hosts in ret_hosts.values(): for host, domain_name in hosts.items(): - ret['_meta']['hostvars'][host] = {'ansible_host': domain_name} + ret["_meta"]["hostvars"][host] = {"ansible_host": domain_name} if self.namespace_is_hostname: host_namespace = normalize_family(host) if host_namespace in self.dico: - ret['_meta']['hostvars'][host].update(self.dico[host_namespace]) + ret["_meta"]["hostvars"][host].update( + self.dico[host_namespace] + ) else: - ret['_meta']['hostvars'][host].update(self.dico) + ret["_meta"]["hostvars"][host].update(self.dico) self.dico = ret diff --git a/src/rougail/output_ansible/config.py b/src/rougail/output_ansible/config.py index 4b0eb89..704d45f 100644 --- a/src/rougail/output_ansible/config.py +++ b/src/rougail/output_ansible/config.py @@ -24,7 +24,8 @@ def get_rougail_config( *, backward_compatibility=True, ) -> dict: - options = [f""" + options = [ + f""" list: description: {_('parameter added only to be compatible with Ansible')} default: false @@ -44,7 +45,8 @@ json: disabled {{% endif %}} -""", f""" +""", + f""" ansible: exists: true redefine: true @@ -56,7 +58,8 @@ ansible: disabled {{% endif %}} -""", f""" +""", + f""" ansible: description: {_('Configuration of output Ansible')} exists: false @@ -65,7 +68,8 @@ ansible: when_not: ansible propertyerror: false -""", f""" +""", + f""" ansible: host_namespace: @@ -75,7 +79,8 @@ ansible: namespace_is_hostname: description: {_('Only variables in host namespace is available by a host')} default: false -"""] +""", + ] return { "name": "ansible", "process": "output", diff --git a/src/rougail/output_ansible/i18n.py b/src/rougail/output_ansible/i18n.py index b514e77..22b6021 100644 --- a/src/rougail/output_ansible/i18n.py +++ b/src/rougail/output_ansible/i18n.py @@ -19,6 +19,8 @@ along with this program. If not, see . from gettext import translation from pathlib import Path -t = translation("rougail_output_ansible", str(Path(__file__).parent / "locale"), fallback=True) +t = translation( + "rougail_output_ansible", str(Path(__file__).parent / "locale"), fallback=True +) _ = t.gettext