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-25 13:39:02 +0000
commit149d726d2c2006cfce72c17dc366647cfea27861 (patch)
treecc1f044abda1cdb70bc581ca526f49a435938b47
parente8ab0c22801038d4763a6a04e454ec36cdf76f7e (diff)
downloadoslo-config-stable/train.tar.gz
config-generator yaml format doesn't work with i18n fieldsstable/train
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) (cherry picked from commit 0950f82ec59e1b4a2af66b5c5940a651a5cb574f) (cherry picked from commit 11f8a430d34ed880a02eaa59092e8c9873f37c6c)
-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 475b244..920270c 100644
--- a/oslo_config/generator.py
+++ b/oslo_config/generator.py
@@ -37,6 +37,7 @@ import yaml
from oslo_config import cfg
+from oslo_i18n import _message
import stevedore.named # noqa
LOG = logging.getLogger(__name__)
@@ -684,6 +685,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
@@ -697,6 +710,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: