msight_core.topics
msight_core.topics
Topic registry helpers.
This module defines a Topic with helpers to register and
retrieve topic metadata from Redis. Topics basic information is stored as JSON strings in the
hash key identified by :data:REDIS_TOPICS_FIELD and follow a small schema:
- name: topic name (string)
- description: optional textual description
- data_type: fully-qualified import path for the data class
The :class:Topic object provides convenience methods to serialize to/from
this dict schema and to register the topic in Redis. Use
:meth:get_topic to load topic metadata from Redis and optionally register a
new topic when it does not exist.
Topic
The Topic holds the topic name, a human-friendly description and the
data type (a subclass of :class:~msight_core.data.Data). The
:meth:to_dict / :meth:from_dict pair provide a stable JSON-serializable
representation used for storage in Redis.
to_dict()
Return a JSON-serializable mapping for this topic.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Contains keys |
register()
Register (or update) this topic in Redis.
The topic is stored in the hash named by :data:REDIS_TOPICS_FIELD.
If the topic already exists the value will be overwritten.
from_dict(d)
staticmethod
Create a :class:Topic from a deserialized mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
Mapping with keys |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Topic |
Topic
|
Constructed Topic instance with the resolved data_type. |
get_topic(redis_client, topic_name, register_if_not_exist=False, data_type=None, description=None)
Lookup a Topic by name in Redis and optionally register it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_client
|
Redis
|
Redis client instance (supports |
required |
topic_name
|
str
|
Name of the topic to retrieve. |
required |
register_if_not_exist
|
bool
|
If True and the topic is missing, a new
topic will be created using the provided |
False
|
data_type
|
Optional[Type[Data]]
|
Class object to use when registering a new topic (required
when |
None
|
description
|
str
|
Optional description used when registering. |
None
|
Returns:
| Type | Description |
|---|---|
Topic
|
Topic or None: The Topic instance when found or created; otherwise |
Topic
|
|
Topic
|
False. |