Refactor HyperglassSettings name

This commit is contained in:
thatmattlove 2021-09-22 22:30:16 -07:00
parent 082c4175f4
commit 023c1a5889
3 changed files with 18 additions and 18 deletions

View file

@ -21,7 +21,7 @@ from hyperglass.util import at_least, cpu_count
ListenHost = t.Union[None, IPvAnyAddress, t.Literal["localhost"]]
class HyperglassSystem(BaseSettings):
class HyperglassSettings(BaseSettings):
"""hyperglass system settings, required to start hyperglass."""
class Config:
@ -41,7 +41,7 @@ class HyperglassSystem(BaseSettings):
@validator("host", pre=True, always=True)
def validate_host(
cls: "HyperglassSystem", value: t.Any, values: t.Dict[str, t.Any]
cls: "HyperglassSettings", value: t.Any, values: t.Dict[str, t.Any]
) -> IPvAnyAddress:
"""Set default host based on debug mode."""
@ -65,7 +65,7 @@ class HyperglassSystem(BaseSettings):
@validator("redis_dsn", always=True)
def validate_redis_dsn(
cls: "HyperglassSystem", value: t.Any, values: t.Dict[str, t.Any]
cls: "HyperglassSettings", value: t.Any, values: t.Dict[str, t.Any]
) -> RedisDsn:
"""Construct a Redis DSN if none is provided."""
if value is None:
@ -78,28 +78,28 @@ class HyperglassSystem(BaseSettings):
return dsn
return value
def bind(self: "HyperglassSystem") -> str:
def bind(self: "HyperglassSettings") -> str:
"""Format a listen_address. Wraps IPv6 address in brackets."""
if self.host.version == 6:
return f"[{self.host!s}]:{self.port!s}"
return f"{self.host!s}:{self.port!s}"
@property
def log_level(self: "HyperglassSystem") -> str:
def log_level(self: "HyperglassSettings") -> str:
"""Get log level as string, inferred from debug mode."""
if self.debug:
return "DEBUG"
return "WARNING"
@property
def workers(self: "HyperglassSystem") -> int:
def workers(self: "HyperglassSettings") -> int:
"""Get worker count, inferred from debug mode."""
if self.debug:
return 1
return cpu_count(2)
@property
def redis(self: "HyperglassSystem") -> t.Dict[str, t.Union[None, int, str]]:
def redis(self: "HyperglassSettings") -> t.Dict[str, t.Union[None, int, str]]:
"""Get redis parameters as a dict for convenient connection setups."""
password = None
if self.redis_password is not None:
@ -112,21 +112,21 @@ class HyperglassSystem(BaseSettings):
}
@property
def redis_connection_pool(self: "HyperglassSystem") -> t.Dict[str, t.Any]:
def redis_connection_pool(self: "HyperglassSettings") -> t.Dict[str, t.Any]:
"""Get Redis ConnectionPool keyword arguments."""
return {"url": str(self.redis_dsn), "max_connections": at_least(8, cpu_count(2))}
@property
def dev_url(self: "HyperglassSystem") -> str:
def dev_url(self: "HyperglassSettings") -> str:
"""Get the hyperglass URL for when dev_mode is enabled."""
return f"http://localhost:{self.port!s}/"
@property
def prod_url(self: "HyperglassSystem") -> str:
def prod_url(self: "HyperglassSettings") -> str:
"""Get the UI-facing hyperglass URL/path."""
return "/api/"
@property
def static_path(self: "HyperglassSystem") -> Path:
def static_path(self: "HyperglassSettings") -> Path:
"""Get static asset path."""
return Path(self.app_path / "static")

View file

@ -5,15 +5,15 @@ import typing as t
if t.TYPE_CHECKING:
# Local
from .models.system import HyperglassSystem
from .models.system import HyperglassSettings
def _system_settings() -> "HyperglassSystem":
def _system_settings() -> "HyperglassSettings":
"""Get system settings from local environment."""
# Local
from .models.system import HyperglassSystem
from .models.system import HyperglassSettings
return HyperglassSystem()
return HyperglassSettings()
Settings = _system_settings()

View file

@ -14,7 +14,7 @@ from .redis import RedisManager
if t.TYPE_CHECKING:
# Project
from hyperglass.models.system import HyperglassSystem
from hyperglass.models.system import HyperglassSettings
class StateManager:
@ -23,11 +23,11 @@ class StateManager:
Maintains configuration objects in Redis cache and accesses them as needed.
"""
settings: "HyperglassSystem"
settings: "HyperglassSettings"
redis: RedisManager
_namespace: str = "hyperglass.state"
def __init__(self, *, settings: "HyperglassSystem") -> None:
def __init__(self, *, settings: "HyperglassSettings") -> None:
"""Set up Redis connection and add configuration objects."""
self.settings = settings