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
271
272
273
274
275
276
277
278
279
280
281
282
283
284 | def get_broker_channels(
broker: "BrokerUsecase[MsgType, ConnectionType, Any]",
servers: list[str] | None = None,
) -> tuple[dict[str, Channel], dict[str, Operation]]:
"""Get the broker channels for an application."""
channels = {}
operations = {}
channel_servers = [
ref("servers", server_name) for server_name in (servers or ())
] or None
for sub in filter(lambda s: s.specification.include_in_schema, broker.subscribers):
sub_schema = sub.schema()
# A title cannot name the operations of several channels: written once
# per channel it leaves one operation and the rest unreferenced.
sub_title = sub.specification.config.title_ if len(sub_schema) == 1 else None
for sub_key, sub_channel in sub_schema.items():
channel_obj = Channel.from_sub(sub_channel, servers=channel_servers)
channel_key = clear_key(sub_key)
if channel_key in channels:
warnings.warn(
f"Overwrite channel handler, channels have the same names: `{channel_key}`",
RuntimeWarning,
stacklevel=1,
)
channels[channel_key] = channel_obj
operation_key = (
f"{channel_key}Subscribe"
if sub_title is None or sub_title == "/"
else sub_title
)
if operation_key in operations:
warnings.warn(
f"Overwrite channel handler, operations have the same names: `{operation_key}`",
RuntimeWarning,
stacklevel=1,
)
operations[operation_key] = Operation.from_sub(
messages=[
Reference(**ref("channels", channel_key, "messages", msg_name))
for msg_name in channel_obj.messages
],
channel=Reference(**ref("channels", channel_key)),
operation=sub_channel.operation,
)
for pub in filter(lambda p: p.specification.include_in_schema, broker.publishers):
for pub_key, pub_channel in pub.schema().items():
channel_obj = Channel.from_pub(pub_channel, servers=channel_servers)
channel_key = clear_key(pub_key)
if channel_key in channels:
warnings.warn(
f"Overwrite channel handler, channels have the same names: `{channel_key}`",
RuntimeWarning,
stacklevel=1,
)
channels[channel_key] = channel_obj
operations[channel_key] = Operation.from_pub(
messages=[
Reference(**ref("channels", channel_key, "messages", msg_name))
for msg_name in channel_obj.messages
],
channel=Reference(**ref("channels", channel_key)),
operation=pub_channel.operation,
)
return channels, operations
|