|
1 | 1 | import functools |
2 | 2 | import logging |
3 | | -from collections.abc import Iterable |
4 | 3 | from typing import Any, Callable, Dict, Generator, List, NamedTuple, Optional, Type |
5 | 4 |
|
6 | 5 | from ..exceptions import DashboardException |
@@ -72,62 +71,9 @@ def __init__(self, gw_group: Optional[str] = None, traddr: Optional[str] = None) |
72 | 71 | self.channel = grpc.insecure_channel(self.gateway_addr) |
73 | 72 | self.stub = pb2_grpc.GatewayStub(self.channel) |
74 | 73 |
|
75 | | - def make_namedtuple_from_object(cls: Type[NamedTuple], obj: Any) -> NamedTuple: |
76 | | - return cls( |
77 | | - **{ |
78 | | - field: getattr(obj, field) |
79 | | - for field in cls._fields |
80 | | - if hasattr(obj, field) |
81 | | - } |
82 | | - ) # type: ignore |
83 | | - |
84 | 74 | Model = Dict[str, Any] |
85 | | - |
86 | | - def map_model( |
87 | | - model: Type[NamedTuple], |
88 | | - first: Optional[str] = None, |
89 | | - ) -> Callable[..., Callable[..., Model]]: |
90 | | - def decorator(func: Callable[..., Message]) -> Callable[..., Model]: |
91 | | - @functools.wraps(func) |
92 | | - def wrapper(*args, **kwargs) -> Model: |
93 | | - message = func(*args, **kwargs) |
94 | | - if first: |
95 | | - try: |
96 | | - message = getattr(message, first)[0] |
97 | | - except IndexError: |
98 | | - raise DashboardException( |
99 | | - msg="Not Found", http_status_code=404, component="nvmeof" |
100 | | - ) |
101 | | - |
102 | | - return make_namedtuple_from_object(model, message)._asdict() |
103 | | - |
104 | | - return wrapper |
105 | | - |
106 | | - return decorator |
107 | | - |
108 | 75 | Collection = List[Model] |
109 | 76 |
|
110 | | - def map_collection( |
111 | | - model: Type[NamedTuple], |
112 | | - pick: str, # pylint: disable=redefined-outer-name |
113 | | - finalize: Optional[Callable[[Message, Collection], Collection]] = None, |
114 | | - ) -> Callable[..., Callable[..., Collection]]: |
115 | | - def decorator(func: Callable[..., Message]) -> Callable[..., Collection]: |
116 | | - @functools.wraps(func) |
117 | | - def wrapper(*args, **kwargs) -> Collection: |
118 | | - message = func(*args, **kwargs) |
119 | | - collection: Iterable = getattr(message, pick) |
120 | | - out = [ |
121 | | - make_namedtuple_from_object(model, i)._asdict() for i in collection |
122 | | - ] |
123 | | - if finalize: |
124 | | - return finalize(message, out) |
125 | | - return out |
126 | | - |
127 | | - return wrapper |
128 | | - |
129 | | - return decorator |
130 | | - |
131 | 77 | import errno |
132 | 78 |
|
133 | 79 | NVMeoFError2HTTP = { |
@@ -266,21 +212,28 @@ def namedtuple_to_dict(obj): |
266 | 212 | ] |
267 | 213 | return obj |
268 | 214 |
|
269 | | - def convert_to_model(model: Type[NamedTuple]) -> Callable[..., Callable[..., Model]]: |
| 215 | + def convert_to_model(model: Type[NamedTuple], |
| 216 | + finalize: Optional[Callable[[Dict], Dict]] = None |
| 217 | + ) -> Callable[..., Callable[..., Model]]: |
270 | 218 | def decorator(func: Callable[..., Message]) -> Callable[..., Model]: |
271 | 219 | @functools.wraps(func) |
272 | 220 | def wrapper(*args, **kwargs) -> Model: |
273 | 221 | message = func(*args, **kwargs) |
274 | 222 | msg_dict = MessageToDict(message, including_default_value_fields=True, |
275 | 223 | preserving_proto_field_name=True) |
276 | | - return namedtuple_to_dict(obj_to_namedtuple(msg_dict, model)) |
| 224 | + |
| 225 | + result = namedtuple_to_dict(obj_to_namedtuple(msg_dict, model)) |
| 226 | + if finalize: |
| 227 | + return finalize(result) |
| 228 | + return result |
277 | 229 |
|
278 | 230 | return wrapper |
279 | 231 |
|
280 | 232 | return decorator |
281 | 233 |
|
282 | 234 | # pylint: disable-next=redefined-outer-name |
283 | | - def pick(field: str, first: bool = False) -> Callable[..., Callable[..., object]]: |
| 235 | + def pick(field: str, first: bool = False, |
| 236 | + ) -> Callable[..., Callable[..., object]]: |
284 | 237 | def decorator(func: Callable[..., Dict]) -> Callable[..., object]: |
285 | 238 | @functools.wraps(func) |
286 | 239 | def wrapper(*args, **kwargs) -> object: |
|
0 commit comments