(cls, subject, *args, **kwargs)
| 217 | class PropertyValueTable(formatters.Formatter): |
| 218 | @classmethod |
| 219 | def format(cls, subject, *args, **kwargs): |
| 220 | attributes = kwargs.get("attributes", None) |
| 221 | attribute_display_order = kwargs.get( |
| 222 | "attribute_display_order", DEFAULT_ATTRIBUTE_DISPLAY_ORDER |
| 223 | ) |
| 224 | attribute_transform_functions = kwargs.get("attribute_transform_functions", {}) |
| 225 | |
| 226 | if not attributes or "all" in attributes: |
| 227 | attributes = sorted( |
| 228 | [attr for attr in subject.__dict__ if not attr.startswith("_")] |
| 229 | ) |
| 230 | |
| 231 | for attr in attribute_display_order[::-1]: |
| 232 | if attr in attributes: |
| 233 | attributes.remove(attr) |
| 234 | attributes = [attr] + attributes |
| 235 | table = PrettyTable() |
| 236 | table.field_names = ["Property", "Value"] |
| 237 | table.max_width["Property"] = 20 |
| 238 | table.max_width["Value"] = 60 |
| 239 | table.padding_width = 1 |
| 240 | table.align = "l" |
| 241 | table.valign = "t" |
| 242 | |
| 243 | for attribute in attributes: |
| 244 | if "." in attribute: |
| 245 | field_names = attribute.split(".") |
| 246 | value = cls._get_attribute_value(subject, field_names.pop(0)) |
| 247 | for name in field_names: |
| 248 | value = cls._get_attribute_value(value, name) |
| 249 | if type(value) is str: |
| 250 | break |
| 251 | else: |
| 252 | value = cls._get_attribute_value(subject, attribute) |
| 253 | |
| 254 | transform_function = attribute_transform_functions.get( |
| 255 | attribute, lambda value: value |
| 256 | ) |
| 257 | value = transform_function(value=value) |
| 258 | |
| 259 | if type(value) is dict or type(value) is list: |
| 260 | value = json.dumps(value, indent=4) |
| 261 | |
| 262 | value = strutil.strip_carriage_returns(strutil.unescape(value)) |
| 263 | table.add_row([attribute, value]) |
| 264 | return table |
| 265 | |
| 266 | @staticmethod |
| 267 | def _get_attribute_value(subject, attribute): |
nothing calls this directly
no test coverage detected