from typing import (
Any,
Dict,
+ Iterable,
Optional,
TYPE_CHECKING,
)
DEFAULT_JSON_INDENT: int = 2
-class Format(enum.Enum):
+class Format(str, enum.Enum):
plain = "plain"
json = "json"
json_pretty = "json-pretty"
... # 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
"""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.
# 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