1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

60 lines
1.7 KiB
Python
Raw Normal View History

"""hyperglass global state."""
# Standard Library
import typing as t
# Third Party
from redis import Redis, ConnectionPool
# Project
2021-09-16 15:35:12 -07:00
from hyperglass.util import repr_from_attrs
from hyperglass.configuration import params, devices, ui_params
# Local
from .redis import RedisManager
if t.TYPE_CHECKING:
# Project
from hyperglass.models.system import HyperglassSystem
class StateManager:
"""Global State Manager.
Maintains configuration objects in Redis cache and accesses them as needed.
"""
settings: "HyperglassSystem"
redis: RedisManager
_namespace: str = "hyperglass.state"
def __init__(self, *, settings: "HyperglassSystem") -> None:
"""Set up Redis connection and add configuration objects."""
self.settings = settings
connection_pool = ConnectionPool.from_url(**self.settings.redis_connection_pool)
redis = Redis(connection_pool=connection_pool)
self.redis = RedisManager(instance=redis, namespace=self._namespace)
# Add configuration objects.
self.redis.set("params", params)
self.redis.set("devices", devices)
self.redis.set("ui_params", ui_params)
2021-09-16 15:35:12 -07:00
def __repr__(self) -> str:
"""Represent state manager by name and namespace."""
return repr_from_attrs(self, ("redis", "namespace"))
def __str__(self) -> str:
"""Represent state manager by __repr__."""
return repr(self)
@classmethod
def properties(cls: "StateManager") -> t.Tuple[str, ...]:
"""Get all read-only properties of the state manager."""
return tuple(
attr
for attr in dir(cls)
if not attr.startswith("_") and "fget" in dir(getattr(cls, attr))
)