summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vallee Delisle <dvd@redhat.com>2021-05-11 21:49:04 -0400
committerDavid Vallee Delisle <dvd@redhat.com>2021-06-04 17:26:57 +0000
commit0950f82ec59e1b4a2af66b5c5940a651a5cb574f (patch)
tree522d38ead70d2af4fec857ba93dfbdac75e94a7a
parentdca54e4c1cf3c4d2674d146678fe08c021977358 (diff)
downloadoslo-config-stable/victoria.tar.gz
config-generator yaml format doesn't work with i18n fieldsvictoria-em8.3.4stable/victoria
This is because there's no yaml representer for i18n Messages object. This patch aims to add this representer and allow the generation of configurations using oslo.i18n strings. One example of this is cinder. Closes-bug: #1928582 Change-Id: I70ab87c9bed093cad883b6301b8a09753fc470d9 (cherry picked from commit e5fc313ecf47813364a2111b987ced823c0d64e3) (cherry picked from commit de1dbeed235d2df7cf1528aef6ad4a70ddf8559f)
-rw-r--r--oslo_config/generator.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/oslo_config/generator.py b/oslo_config/generator.py
index f422587..348cac2 100644
--- a/oslo_config/generator.py
+++ b/oslo_config/generator.py
@@ -41,6 +41,7 @@ except ImportError:
import yaml
from oslo_config import cfg
+from oslo_i18n import _message
import stevedore.named # noqa
LOG = logging.getLogger(__name__)
@@ -685,6 +686,18 @@ def _generate_machine_readable_data(groups, conf):
return output_data
+def i18n_representer(dumper, data):
+ """oslo_i18n yaml representer
+
+ Returns a translated to the default locale string for yaml.safe_dump
+
+ :param dumper: a SafeDumper instance passed by yaml.safe_dump
+ :param data: a oslo_i18n._message.Message instance
+ """
+ serializedData = str(data.translation())
+ return dumper.represent_str(serializedData)
+
+
def _output_machine_readable(groups, output_file, conf):
"""Write a machine readable sample config file
@@ -698,6 +711,7 @@ def _output_machine_readable(groups, output_file, conf):
"""
output_data = _generate_machine_readable_data(groups, conf)
if conf.format_ == 'yaml':
+ yaml.SafeDumper.add_representer(_message.Message, i18n_representer)
output_file.write(yaml.safe_dump(output_data,
default_flow_style=False))
else: