| 1 |
# Copyright 2017 Huawei, Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
# |
| 15 |
|
| 16 |
"""Formattable column for specify content type""" |
| 17 |
|
| 18 |
import typing as ty |
| 19 |
|
| 20 |
from cliff import columns |
| 21 |
|
| 22 |
from osc_lib import utils |
| 23 |
|
| 24 |
|
| 25 |
class DictColumn(columns.FormattableColumn[dict[str, ty.Any]]): |
| 26 |
"""Format column for dict content""" |
| 27 |
|
| 28 |
def human_readable(self) -> str: |
| 29 |
return utils.format_dict(self._value) |
| 30 |
|
| 31 |
def machine_readable(self) -> dict[str, ty.Any]: |
| 32 |
return dict(self._value or {}) |
| 33 |
|
| 34 |
|
| 35 |
class DictListColumn(columns.FormattableColumn[dict[str, list[ty.Any]]]): |
| 36 |
"""Format column for dict, key is string, value is list""" |
| 37 |
|
| 38 |
def human_readable(self) -> str: |
| 39 |
return utils.format_dict_of_list(self._value) or '' |
| 40 |
|
| 41 |
def machine_readable(self) -> dict[str, list[ty.Any]]: |
| 42 |
return dict(self._value or {}) |
| 43 |
|
| 44 |
|
| 45 |
class ListColumn(columns.FormattableColumn[list[ty.Any]]): |
| 46 |
"""Format column for list content""" |
| 47 |
|
| 48 |
def human_readable(self) -> str: |
| 49 |
return utils.format_list(self._value) or '' |
| 50 |
|
| 51 |
def machine_readable(self) -> list[ty.Any]: |
| 52 |
return [x for x in self._value or []] |
| 53 |
|
| 54 |
|
| 55 |
class ListDictColumn(columns.FormattableColumn[list[dict[str, ty.Any]]]): |
| 56 |
"""Format column for list of dict content""" |
| 57 |
|
| 58 |
def human_readable(self) -> str: |
| 59 |
return utils.format_list_of_dicts(self._value) or '' |
| 60 |
|
| 61 |
def machine_readable(self) -> list[dict[str, ty.Any]]: |
| 62 |
return [dict(x) for x in self._value or []] |
| 63 |
|
| 64 |
|
| 65 |
class SizeColumn(columns.FormattableColumn[int | float]): |
| 66 |
"""Format column for file size content""" |
| 67 |
|
| 68 |
def human_readable(self) -> str: |
| 69 |
return utils.format_size(self._value) |