comparison sat_frontends/jp/cmd_info.py @ 3573:813595f88612

merge changes from main branch
author Goffi <goffi@goffi.org>
date Thu, 17 Jun 2021 13:05:58 +0200
parents 04283582966f
children 9c76678a39e2
comparison
equal deleted inserted replaced
3541:888109774673 3573:813595f88612
26 26
27 __commands__ = ["Info"] 27 __commands__ = ["Info"]
28 28
29 29
30 class Disco(base.CommandBase): 30 class Disco(base.CommandBase):
31 31 def __init__(self, host):
32 def __init__(self, host): 32 extra_outputs = {"default": self.default_output}
33 extra_outputs = {'default': self.default_output}
34 super(Disco, self).__init__( 33 super(Disco, self).__init__(
35 host, 'disco', use_output='complex', extra_outputs=extra_outputs, 34 host,
36 help=_('service discovery')) 35 "disco",
36 use_output="complex",
37 extra_outputs=extra_outputs,
38 help=_("service discovery"),
39 )
37 40
38 def add_parser_options(self): 41 def add_parser_options(self):
39 self.parser.add_argument("jid", help=_("entity to discover")) 42 self.parser.add_argument("jid", help=_("entity to discover"))
40 self.parser.add_argument( 43 self.parser.add_argument(
41 "-t", "--type", type=str, choices=('infos', 'items', 'both'), default='both', 44 "-t",
42 help=_("type of data to discover")) 45 "--type",
43 self.parser.add_argument("-n", "--node", default='', help=_("node to use")) 46 type=str,
47 choices=("infos", "items", "both"),
48 default="both",
49 help=_("type of data to discover"),
50 )
51 self.parser.add_argument("-n", "--node", default="", help=_("node to use"))
44 self.parser.add_argument( 52 self.parser.add_argument(
45 "-C", "--no-cache", dest='use_cache', action="store_false", 53 "-C",
46 help=_("ignore cache")) 54 "--no-cache",
55 dest="use_cache",
56 action="store_false",
57 help=_("ignore cache"),
58 )
47 59
48 def default_output(self, data): 60 def default_output(self, data):
49 features = data.get('features', []) 61 features = data.get("features", [])
50 identities = data.get('identities', []) 62 identities = data.get("identities", [])
51 extensions = data.get('extensions', {}) 63 extensions = data.get("extensions", {})
52 items = data.get('items', []) 64 items = data.get("items", [])
53 65
54 identities_table = common.Table(self.host, 66 identities_table = common.Table(
55 identities, 67 self.host,
56 headers=(_('category'), 68 identities,
57 _('type'), 69 headers=(_("category"), _("type"), _("name")),
58 _('name')), 70 use_buffer=True,
59 use_buffer=True) 71 )
60 72
61 extensions_tpl = [] 73 extensions_tpl = []
62 extensions_types = list(extensions.keys()) 74 extensions_types = list(extensions.keys())
63 extensions_types.sort() 75 extensions_types.sort()
64 for type_ in extensions_types: 76 for type_ in extensions_types:
67 field_lines = [] 79 field_lines = []
68 data, values = field 80 data, values = field
69 data_keys = list(data.keys()) 81 data_keys = list(data.keys())
70 data_keys.sort() 82 data_keys.sort()
71 for key in data_keys: 83 for key in data_keys:
72 field_lines.append(A.color('\t', C.A_SUBHEADER, key, A.RESET, ': ', 84 field_lines.append(
73 data[key])) 85 A.color("\t", C.A_SUBHEADER, key, A.RESET, ": ", data[key])
86 )
74 if len(values) == 1: 87 if len(values) == 1:
75 field_lines.append(A.color('\t', C.A_SUBHEADER, "value", A.RESET, 88 field_lines.append(
76 ': ', values[0] or (A.BOLD + "UNSET"))) 89 A.color(
90 "\t",
91 C.A_SUBHEADER,
92 "value",
93 A.RESET,
94 ": ",
95 values[0] or (A.BOLD + "UNSET"),
96 )
97 )
77 elif len(values) > 1: 98 elif len(values) > 1:
78 field_lines.append(A.color('\t', C.A_SUBHEADER, "values", A.RESET, 99 field_lines.append(
79 ': ')) 100 A.color("\t", C.A_SUBHEADER, "values", A.RESET, ": ")
101 )
80 102
81 for value in values: 103 for value in values:
82 field_lines.append(A.color('\t - ', A.BOLD, value)) 104 field_lines.append(A.color("\t - ", A.BOLD, value))
83 fields.append('\n'.join(field_lines)) 105 fields.append("\n".join(field_lines))
84 extensions_tpl.append('{type_}\n{fields}'.format( 106 extensions_tpl.append(
85 type_=type_, 107 "{type_}\n{fields}".format(type_=type_, fields="\n\n".join(fields))
86 fields='\n\n'.join(fields))) 108 )
87 109
88 items_table = common.Table( 110 items_table = common.Table(
89 self.host, 111 self.host, items, headers=(_("entity"), _("node"), _("name")), use_buffer=True
90 items, 112 )
91 headers=(
92 _('entity'),
93 _('node'),
94 _('name')),
95 use_buffer=True)
96 113
97 template = [] 114 template = []
98 if features: 115 if features:
99 template.append(A.color(C.A_HEADER, _("Features")) + "\n\n{features}") 116 template.append(A.color(C.A_HEADER, _("Features")) + "\n\n{features}")
100 if identities: 117 if identities:
102 if extensions: 119 if extensions:
103 template.append(A.color(C.A_HEADER, _("Extensions")) + "\n\n{extensions}") 120 template.append(A.color(C.A_HEADER, _("Extensions")) + "\n\n{extensions}")
104 if items: 121 if items:
105 template.append(A.color(C.A_HEADER, _("Items")) + "\n\n{items}") 122 template.append(A.color(C.A_HEADER, _("Items")) + "\n\n{items}")
106 123
107 print("\n\n".join(template).format( 124 print(
108 features = '\n'.join(features), 125 "\n\n".join(template).format(
109 identities = identities_table.display().string, 126 features="\n".join(features),
110 extensions = '\n'.join(extensions_tpl), 127 identities=identities_table.display().string,
111 items = items_table.display().string, 128 extensions="\n".join(extensions_tpl),
112 )) 129 items=items_table.display().string,
113 130 )
114 async def start(self): 131 )
115 infos_requested = self.args.type in ('infos', 'both') 132
116 items_requested = self.args.type in ('items', 'both') 133 async def start(self):
134 infos_requested = self.args.type in ("infos", "both")
135 items_requested = self.args.type in ("items", "both")
117 jids = await self.host.check_jids([self.args.jid]) 136 jids = await self.host.check_jids([self.args.jid])
118 jid = jids[0] 137 jid = jids[0]
119 138
120 # infos 139 # infos
121 if not infos_requested: 140 if not infos_requested:
124 try: 143 try:
125 infos = await self.host.bridge.discoInfos( 144 infos = await self.host.bridge.discoInfos(
126 jid, 145 jid,
127 node=self.args.node, 146 node=self.args.node,
128 use_cache=self.args.use_cache, 147 use_cache=self.args.use_cache,
129 profile_key=self.host.profile 148 profile_key=self.host.profile,
130 ) 149 )
131 except Exception as e: 150 except Exception as e:
132 self.disp(_(f"error while doing discovery: {e}"), error=True) 151 self.disp(_("error while doing discovery: {e}").format(e=e), error=True)
133 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 152 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
134 153
135 # items 154 # items
136 if not items_requested: 155 if not items_requested:
137 items = None 156 items = None
138 else: 157 else:
139 try: 158 try:
140 items = await self.host.bridge.discoItems( 159 items = await self.host.bridge.discoItems(
141 jid, 160 jid,
142 node=self.args.node, 161 node=self.args.node,
143 use_cache=self.args.use_cache, 162 use_cache=self.args.use_cache,
144 profile_key=self.host.profile 163 profile_key=self.host.profile,
145 ) 164 )
146 except Exception as e: 165 except Exception as e:
147 self.disp(_(f"error while doing discovery: {e}"), error=True) 166 self.disp(_("error while doing discovery: {e}").format(e=e), error=True)
148 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 167 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
149 168
150 # output 169 # output
151 data = {} 170 data = {}
152 171
153 if infos_requested: 172 if infos_requested:
154 features, identities, extensions = infos 173 features, identities, extensions = infos
155 features.sort() 174 features.sort()
156 identities.sort(key=lambda identity: identity[2]) 175 identities.sort(key=lambda identity: identity[2])
157 data.update({ 176 data.update(
158 'features': features, 177 {"features": features, "identities": identities, "extensions": extensions}
159 'identities': identities, 178 )
160 'extensions': extensions})
161 179
162 if items_requested: 180 if items_requested:
163 items.sort(key=lambda item: item[2]) 181 items.sort(key=lambda item: item[2])
164 data['items'] = items 182 data["items"] = items
165 183
166 await self.output(data) 184 await self.output(data)
167 self.host.quit() 185 self.host.quit()
168 186
187
169 class Version(base.CommandBase): 188 class Version(base.CommandBase):
170 189 def __init__(self, host):
171 def __init__(self, host): 190 super(Version, self).__init__(host, "version", help=_("software version"))
172 super(Version, self).__init__(host, 'version', help=_('software version'))
173 191
174 def add_parser_options(self): 192 def add_parser_options(self):
175 self.parser.add_argument("jid", type=str, help=_("Entity to request")) 193 self.parser.add_argument("jid", type=str, help=_("Entity to request"))
176 194
177 async def start(self): 195 async def start(self):
178 jids = await self.host.check_jids([self.args.jid]) 196 jids = await self.host.check_jids([self.args.jid])
179 jid = jids[0] 197 jid = jids[0]
180 try: 198 try:
181 data = await self.host.bridge.getSoftwareVersion(jid, self.host.profile) 199 data = await self.host.bridge.getSoftwareVersion(jid, self.host.profile)
182 except Exception as e: 200 except Exception as e:
183 self.disp(_(f"error while trying to get version: {e}"), error=True) 201 self.disp(_("error while trying to get version: {e}").format(e=e), error=True)
184 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 202 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
185 else: 203 else:
186 infos = [] 204 infos = []
187 name, version, os = data 205 name, version, os = data
188 if name: 206 if name:
189 infos.append(_(f"Software name: {name}")) 207 infos.append(_("Software name: {name}").format(name=name))
190 if version: 208 if version:
191 infos.append(_(f"Software version: {version}")) 209 infos.append(_("Software version: {version}").format(version=version))
192 if os: 210 if os:
193 infos.append(_(f"Operating System: {os}")) 211 infos.append(_("Operating System: {os}").format(os=os))
194 212
195 print("\n".join(infos)) 213 print("\n".join(infos))
196 self.host.quit() 214 self.host.quit()
197 215
198 216
199 class Session(base.CommandBase): 217 class Session(base.CommandBase):
200 218 def __init__(self, host):
201 def __init__(self, host): 219 extra_outputs = {"default": self.default_output}
202 extra_outputs = {'default': self.default_output}
203 super(Session, self).__init__( 220 super(Session, self).__init__(
204 host, 'session', use_output='dict', extra_outputs=extra_outputs, 221 host,
205 help=_('running session')) 222 "session",
223 use_output="dict",
224 extra_outputs=extra_outputs,
225 help=_("running session"),
226 )
206 227
207 def add_parser_options(self): 228 def add_parser_options(self):
208 pass 229 pass
209 230
210 async def default_output(self, data): 231 async def default_output(self, data):
211 started = data['started'] 232 started = data["started"]
212 data['started'] = '{short} (UTC, {relative})'.format( 233 data["started"] = "{short} (UTC, {relative})".format(
213 short=date_utils.date_fmt(started), 234 short=date_utils.date_fmt(started),
214 relative=date_utils.date_fmt(started, 'relative')) 235 relative=date_utils.date_fmt(started, "relative"),
215 await self.host.output(C.OUTPUT_DICT, 'simple', {}, data) 236 )
237 await self.host.output(C.OUTPUT_DICT, "simple", {}, data)
216 238
217 async def start(self): 239 async def start(self):
218 try: 240 try:
219 data = await self.host.bridge.sessionInfosGet(self.host.profile) 241 data = await self.host.bridge.sessionInfosGet(self.host.profile)
220 except Exception as e: 242 except Exception as e:
221 self.disp(_(f'Error getting session infos: {e}'), error=True) 243 self.disp(_("Error getting session infos: {e}").format(e=e), error=True)
222 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 244 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
223 else: 245 else:
224 await self.output(data) 246 await self.output(data)
225 self.host.quit() 247 self.host.quit()
226 248
227 249
228 class Devices(base.CommandBase): 250 class Devices(base.CommandBase):
229
230 def __init__(self, host): 251 def __init__(self, host):
231 super(Devices, self).__init__( 252 super(Devices, self).__init__(
232 host, 'devices', use_output=C.OUTPUT_LIST_DICT, 253 host, "devices", use_output=C.OUTPUT_LIST_DICT, help=_("devices of an entity")
233 help=_('devices of an entity')) 254 )
234 255
235 def add_parser_options(self): 256 def add_parser_options(self):
236 self.parser.add_argument( 257 self.parser.add_argument(
237 "jid", type=str, nargs='?', default='', help=_("Entity to request")) 258 "jid", type=str, nargs="?", default="", help=_("Entity to request")
259 )
238 260
239 async def start(self): 261 async def start(self):
240 try: 262 try:
241 data = await self.host.bridge.devicesInfosGet( 263 data = await self.host.bridge.devicesInfosGet(
242 self.args.jid, self.host.profile) 264 self.args.jid, self.host.profile
265 )
243 except Exception as e: 266 except Exception as e:
244 self.disp(_(f'Error getting devices infos: {e}'), error=True) 267 self.disp(_("Error getting devices infos: {e}").format(e=e), error=True)
245 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 268 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
246 else: 269 else:
247 data = data_format.deserialise(data, type_check=list) 270 data = data_format.deserialise(data, type_check=list)
248 await self.output(data) 271 await self.output(data)
249 self.host.quit() 272 self.host.quit()
252 class Info(base.CommandBase): 275 class Info(base.CommandBase):
253 subcommands = (Disco, Version, Session, Devices) 276 subcommands = (Disco, Version, Session, Devices)
254 277
255 def __init__(self, host): 278 def __init__(self, host):
256 super(Info, self).__init__( 279 super(Info, self).__init__(
257 host, 'info', use_profile=False, 280 host,
258 help=_('Get various pieces of information on entities')) 281 "info",
282 use_profile=False,
283 help=_("Get various pieces of information on entities"),
284 )