1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-01-17 08:48:05 +00:00

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

View file

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

View file

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