]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr: add CommonFormatter type and valid_formats method
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 9 Apr 2022 19:13:41 +0000 (15:13 -0400)
committerAdam King <adking@redhat.com>
Sat, 21 May 2022 23:21:11 +0000 (19:21 -0400)
A type that has a valid_formats method, and thus meets the
CommonFormatter protocol, supports distinguishing between formats
that are known but unsupported for a given API vs. unknown (possibly a typo).

To make working with the format names easier this also makes the Format
enum inherit from str.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit 77573637cd78e50d1336680a23fd1044d626f855)

src/pybind/mgr/object_format.py
src/pybind/mgr/tests/test_object_format.py

index 2dc0ecdabac654bca21eb80f7e155f827c8ec893..0d2dbb439593e1c063933a8326adee00ce6d4216 100644 (file)
@@ -8,6 +8,7 @@ import sys
 from typing import (
     Any,
     Dict,
+    Iterable,
     Optional,
     TYPE_CHECKING,
 )
@@ -30,7 +31,7 @@ else:
 DEFAULT_JSON_INDENT: int = 2
 
 
-class Format(enum.Enum):
+class Format(str, enum.Enum):
     plain = "plain"
     json = "json"
     json_pretty = "json-pretty"
@@ -89,6 +90,16 @@ class ReturnValueProvider(Protocol):
         ...  # pragma: no cover
 
 
+class CommonFormatter(Protocol):
+    """A protocol that indicates the type is a formatter for multiple
+    possible formats.
+    """
+
+    def valid_formats(self) -> Iterable[str]:
+        """Return the names of known valid formats."""
+        ...  # pragma: no cover
+
+
 # The _is_name_of_protocol_type functions below are here because the production
 # builds of the ceph manager are lower than python 3.8 and do not have
 # typing_extensions available in the resulting images. This means that
@@ -182,6 +193,12 @@ class ObjectFormatAdapter:
         """Return a YAML formatted string representing the input object."""
         return yaml.safe_dump(self._fetch_yaml_data())
 
+    format_json_pretty = format_json
+
+    def valid_formats(self) -> Iterable[str]:
+        """Return valid format names."""
+        return set(str(v) for v in Format.__members__)
+
 
 class ReturnValueAdapter:
     """A return-value adapter for an object.
index 5a1a4b5a227ca82e3bd8cb51f28aec7676383418..6587955d49cbeca235ded12712aff8776434ab59 100644 (file)
@@ -128,3 +128,12 @@ def test_return_value(obj: Any, ret: int):
     # a ReturnValueAdapter instance meets the ReturnValueProvider protocol.
     assert object_format._is_return_value_provider(rva)
     assert rva.mgr_return_value() == ret
+
+
+def test_valid_formats():
+    ofa = object_format.ObjectFormatAdapter({"fred": "wilma"})
+    vf = ofa.valid_formats()
+    assert "json" in vf
+    assert "yaml" in vf
+    assert "xml" in vf
+    assert "plain" in vf