File: pytango_pprint.py

package info (click to toggle)
pytango 10.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,304 kB
  • sloc: python: 27,795; cpp: 16,150; sql: 252; sh: 152; makefile: 43
file content (270 lines) | stat: -rw-r--r-- 6,616 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later

"""
This is an internal PyTango module.
"""

__all__ = ("pytango_pprint_init",)

__docformat__ = "restructuredtext"

import json
import textwrap

from tango import (
    StdStringVector,
    StdLongVector,
    CommandInfoList,
    AttributeInfoList,
    AttributeInfoListEx,
    DeviceDataHistoryList,
    GroupReplyList,
    GroupAttrReplyList,
    GroupCmdReplyList,
    DbData,
    DbDevInfos,
    DbDevExportInfos,
    DbDevImportInfos,
    DbHistoryList,
    LockerInfo,
    DevCommandInfo,
    AttributeDimension,
    CommandInfo,
    DeviceInfo,
    DeviceAttributeConfig,
    AttributeInfo,
    AttributeAlarmInfo,
    ChangeEventInfo,
    PeriodicEventInfo,
    ArchiveEventInfo,
    AttributeEventInfo,
    AttributeInfoEx,
    DeviceAttribute,
    DeviceAttributeHistory,
    DeviceData,
    DeviceDataHistory,
    DbDatum,
    DbDevInfo,
    DbDevImportInfo,
    DbDevExportInfo,
    DbServerInfo,
    GroupReply,
    GroupAttrReply,
    GroupCmdReply,
    DevError,
    EventData,
    AttrConfEventData,
    DataReadyEventData,
    TimeVal,
    DevFailed,
    CmdArgType,
    MultiAttrProp,
    ClientAddr,
)

from tango._tango import (
    AttributeConfig,
    AttributeConfig_2,
    AttributeConfig_3,
    AttributeConfig_5,
    AttributeAlarm,
    EventProperties,
    ChangeEventProp,
    PeriodicEventProp,
    ArchiveEventProp,
)
import collections.abc


_INDENT_LEVEL = 4
_INDENT = " " * _INDENT_LEVEL
_STRUCT_TYPES = (
    LockerInfo,
    DevCommandInfo,
    AttributeDimension,
    CommandInfo,
    DeviceInfo,
    DeviceAttributeConfig,
    AttributeInfo,
    AttributeAlarmInfo,
    ChangeEventInfo,
    PeriodicEventInfo,
    ArchiveEventInfo,
    AttributeEventInfo,
    AttributeInfoEx,
    DeviceAttribute,
    DeviceAttributeHistory,
    DeviceData,
    DeviceDataHistory,
    DbDatum,
    DbDevInfo,
    DbDevImportInfo,
    DbDevExportInfo,
    DbServerInfo,
    DevError,
    EventData,
    AttrConfEventData,
    DataReadyEventData,
    AttributeConfig,
    AttributeConfig_2,
    AttributeConfig_3,
    AttributeConfig_5,
    ChangeEventProp,
    PeriodicEventProp,
    ArchiveEventProp,
    AttributeAlarm,
    EventProperties,
    MultiAttrProp,
)
_SEQUENCE_TYPES = (
    StdStringVector,
    StdLongVector,
    CommandInfoList,
    AttributeInfoList,
    AttributeInfoListEx,
    DeviceDataHistoryList,
    GroupReplyList,
    GroupAttrReplyList,
    GroupCmdReplyList,
    DbData,
    DbDevInfos,
    DbDevExportInfos,
    DbDevImportInfos,
    DbHistoryList,
)


def __inc_param(obj, name):
    ret = not name.startswith("_")
    ret &= name not in ("except_flags",)
    ret &= not isinstance(getattr(obj, name), collections.abc.Callable)
    return ret


def __nested_json_like_repr(value) -> str:
    if isinstance(value, str):
        return f'"{value}"'
    elif type(value) in _STRUCT_TYPES:
        return str(value)
    elif isinstance(value, dict):
        try:
            return json.dumps(value, indent=_INDENT_LEVEL, sort_keys=True)
        except TypeError:
            return repr(value)
    else:
        return repr(value)


def __single_param(obj, param_name, f=repr, fmt="%s = %s"):
    param_value = getattr(obj, param_name)
    if param_name == "data_type":
        param_value = CmdArgType.values.get(param_value, param_value)
    return fmt % (param_name, f(param_value))


def __struct_params_s(obj, separator=", ", f=repr, fmt="%s = %s"):
    """method wrapper for printing all elements of a struct"""
    s = separator.join(
        [__single_param(obj, n, f, fmt) for n in dir(obj) if __inc_param(obj, n)]
    )
    return s


def __struct_params_repr(obj):
    """method wrapper for representing all elements of a struct"""
    return __struct_params_s(obj)


def __struct_params_str(obj, fmt, f=repr):
    """method wrapper for printing all elements of a struct."""
    return __struct_params_s(obj, "\n", f=f, fmt=fmt)


def __repr__Struct(self):
    """repr method for struct"""
    return f"{self.__class__.__name__}({__struct_params_repr(self)})"


def __str__Struct(self):
    """str method for struct"""
    fmt = "%s = %s"
    details = __struct_params_str(self, fmt, __nested_json_like_repr)
    details = __indented(details, strip_outer=False)
    result = f"{self.__class__.__name__}[\n{details}\n]"
    return result


def __registerSeqStr():
    """helper function to make internal sequences printable"""
    _SeqStr = lambda self: (self and f"[{', '.join(map(repr, self))}]") or "[]"
    _SeqRepr = lambda self: (self and f"[{', '.join(map(repr, self))}]") or "[]"

    for seq in _SEQUENCE_TYPES:
        seq.__str__ = _SeqStr
        seq.__repr__ = _SeqRepr


def __str__DevFailed(self):
    if isinstance(self.args, collections.abc.Sequence):
        seq_str = __str_error_stack_helper(self.args)
        return f"DevFailed[\n{seq_str}\n]"
    return f"DevFailed[{self.args}]"


def __str_error_stack_helper(errors):
    err_str = ",\n".join(str(err).strip() for err in errors)
    err_str = __indented(err_str, strip_outer=False)
    return err_str


def __repr__DevFailed(self):
    return f"DevFailed(args = {repr(self.args)})"


def __str__DevError(self):
    details = (
        f"desc = {__indented(self.desc)}\n"
        f"origin = {__indented(self.origin)}\n"
        f"reason = {self.reason}\n"
        f"severity = {self.severity}\n"
    )
    details = __indented(details, strip_outer=False)
    s = f"DevError[\n{details}\n]"
    return s


def __indented(text, strip_outer=True):
    indented = textwrap.indent(text.strip(), _INDENT)
    if strip_outer:
        return indented.strip()
    else:
        return indented


def __registerStructStr():
    """helper method to register str and repr methods for structures"""

    for struct in _STRUCT_TYPES:
        struct.__str__ = __str__Struct
        struct.__repr__ = __repr__Struct

    # special case for structs that already have a str representation
    TimeVal.__repr__ = __repr__Struct
    GroupReply.__repr__ = __repr__Struct
    GroupAttrReply.__repr__ = __repr__Struct
    GroupCmdReply.__repr__ = __repr__Struct
    ClientAddr.__repr__ = __repr__Struct

    # special case for DevFailed: we want a better pretty print
    # also, because it is an Exception it has the message attribute which
    # generates a Deprecation warning in python 2.6
    DevFailed.__str__ = __str__DevFailed
    DevFailed.__repr__ = __repr__DevFailed

    DevError.__str__ = __str__DevError


def pytango_pprint_init():
    __registerSeqStr()
    __registerStructStr()