Skip to content

Commit 39f73e2

Browse files
mgr/smb: add a new BigString helper type for serializing yaml
Add a new BigString type that serves to help serialize resources to YAML, causing it to use the multi-line literal style. A BigString is a string so it should be transparent to other serializations, but note that it should only be used in "simplified" structures. Signed-off-by: John Mulligan <[email protected]>
1 parent 62347a1 commit 39f73e2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/pybind/mgr/smb/resources.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ def format_response(self) -> Tuple[int, str, str]:
9494
return -errno.EINVAL, data, "Invalid input"
9595

9696

97+
class BigString(str):
98+
"""A subclass of str that exists specifally to assit the YAML
99+
formatting of longer strings (SSL/TLS certs). Because the
100+
python YAML lib makes doing this automatically very awkward.
101+
"""
102+
103+
@staticmethod
104+
def yaml_representer(
105+
dumper: yaml.SafeDumper, data: 'BigString'
106+
) -> yaml.ScalarNode:
107+
_type = 'tag:yaml.org,2002:str'
108+
data = str(data)
109+
if '\n' in data or len(data) >= 80:
110+
return dumper.represent_scalar(_type, data, style='|')
111+
return dumper.represent_scalar(_type, data)
112+
113+
114+
# thanks yaml lib for your odd api.
115+
# Maybe this should be part of object_format.py? If this could be useful
116+
# elsewhere, perhaps lift this.
117+
yaml.SafeDumper.add_representer(BigString, BigString.yaml_representer)
118+
119+
97120
class _RBase:
98121
# mypy doesn't currently (well?) support class decorators adding methods
99122
# so we use a base class to add this method to all our resource classes.

0 commit comments

Comments
 (0)