From 3f05ebbabfa89b02e43fd357ec0ade5d8ec8e0fc Mon Sep 17 00:00:00 2001 From: Artem Yushkovskiy Date: Fri, 8 Nov 2019 17:37:06 +0300 Subject: [PATCH] review on hiding --- neuromation/api/client.py | 8 ++------ neuromation/api/quota.py | 10 +++++----- neuromation/cli/config.py | 2 +- neuromation/cli/formatters/config.py | 4 ++-- tests/api/test_quota.py | 16 ++++++++-------- tests/cli/test_formatters.py | 10 +++++----- 6 files changed, 23 insertions(+), 27 deletions(-) diff --git a/neuromation/api/client.py b/neuromation/api/client.py index 55826529d..782f194d2 100644 --- a/neuromation/api/client.py +++ b/neuromation/api/client.py @@ -6,7 +6,7 @@ import aiohttp -from neuromation.api.quota import Quota +from neuromation.api.quota import _Quota from .config import _Config from .core import _Core @@ -48,7 +48,7 @@ def __init__( self._storage = Storage._create(self._core, self._config) self._users = Users._create(self._core) self._parser = Parser._create(self._config, self.username) - self._quota = Quota._create(self._core, self._config) + self._quota = _Quota._create(self._core, self._config) self._images: Optional[Images] = None async def close(self) -> None: @@ -101,10 +101,6 @@ def images(self) -> Images: def parse(self) -> Parser: return self._parser - @property - def quota(self) -> Quota: - return self._quota - def _get_session_cookie(self) -> Optional["Morsel[str]"]: for cookie in self._core._session.cookie_jar: if cookie.key == "NEURO_SESSION": diff --git a/neuromation/api/quota.py b/neuromation/api/quota.py index a3eefd369..bf6534402 100644 --- a/neuromation/api/quota.py +++ b/neuromation/api/quota.py @@ -9,7 +9,7 @@ @dataclass(frozen=True) -class QuotaInfo: +class _QuotaInfo: name: str cpu_time_spent: float cpu_time_limit: float @@ -30,12 +30,12 @@ def _get_remaining_time(self, time_spent: float, time_limit: float) -> float: return 0.0 -class Quota(metaclass=NoPublicConstructor): +class _Quota(metaclass=NoPublicConstructor): def __init__(self, core: _Core, config: _Config) -> None: self._core = core self._config = config - async def get(self, user: Optional[str] = None) -> QuotaInfo: + async def get(self, user: Optional[str] = None) -> _QuotaInfo: user = user or self._config.auth_token.username url = URL(f"stats/users/{user}") async with self._core.request("GET", url) as resp: @@ -43,14 +43,14 @@ async def get(self, user: Optional[str] = None) -> QuotaInfo: return _quota_info_from_api(res) -def _quota_info_from_api(payload: Dict[str, Any]) -> QuotaInfo: +def _quota_info_from_api(payload: Dict[str, Any]) -> _QuotaInfo: jobs = payload["jobs"] spent_gpu = float(int(jobs["total_gpu_run_time_minutes"]) * 60) spent_cpu = float(int(jobs["total_non_gpu_run_time_minutes"]) * 60) quota = payload["quota"] limit_gpu = float(quota.get("total_gpu_run_time_minutes", "inf")) * 60 limit_cpu = float(quota.get("total_non_gpu_run_time_minutes", "inf")) * 60 - return QuotaInfo( + return _QuotaInfo( name=payload["name"], gpu_time_spent=spent_gpu, gpu_time_limit=limit_gpu, diff --git a/neuromation/cli/config.py b/neuromation/cli/config.py index 38517154f..71b8a4159 100644 --- a/neuromation/cli/config.py +++ b/neuromation/cli/config.py @@ -55,7 +55,7 @@ async def show_quota(root: Root, user: Optional[str]) -> None: """ Print quota and remaining computation time. """ - quota = await root.client.quota.get(user) + quota = await root.client._quota.get(user) fmt = QuotaInfoFormatter() click.echo(fmt(quota)) diff --git a/neuromation/cli/formatters/config.py b/neuromation/cli/formatters/config.py index 75618d92a..c21721b99 100644 --- a/neuromation/cli/formatters/config.py +++ b/neuromation/cli/formatters/config.py @@ -4,7 +4,7 @@ from click import style from neuromation.api import Preset -from neuromation.api.quota import QuotaInfo +from neuromation.api.quota import _QuotaInfo from neuromation.cli.root import Root from neuromation.cli.utils import format_size @@ -72,7 +72,7 @@ def _format_presets(self, presets: Dict[str, Preset]) -> Iterator[str]: class QuotaInfoFormatter: QUOTA_NOT_SET = "infinity" - def __call__(self, quota: QuotaInfo) -> str: + def __call__(self, quota: _QuotaInfo) -> str: gpu_details = self._format_quota_details( quota.gpu_time_spent, quota.gpu_time_limit, quota.gpu_time_left ) diff --git a/tests/api/test_quota.py b/tests/api/test_quota.py index ce896a812..9033dbc26 100644 --- a/tests/api/test_quota.py +++ b/tests/api/test_quota.py @@ -4,7 +4,7 @@ from aiohttp import web from neuromation.api import AuthorizationError, Client -from neuromation.api.quota import QuotaInfo +from neuromation.api.quota import _QuotaInfo from tests import _TestServerFactory @@ -34,8 +34,8 @@ async def handle_stats(request: web.Request) -> web.StreamResponse: srv = await aiohttp_server(app) async with make_client(srv.make_url("/")) as client: - quota = await client.quota.get() - assert quota == QuotaInfo( + quota = await client._quota.get() + assert quota == _QuotaInfo( name=client.username, gpu_time_spent=float(101 * 60), gpu_time_limit=float(201 * 60), @@ -67,8 +67,8 @@ async def handle_stats(request: web.Request) -> web.StreamResponse: srv = await aiohttp_server(app) async with make_client(srv.make_url("/")) as client: - quota = await client.quota.get("another-user") - assert quota == QuotaInfo( + quota = await client._quota.get("another-user") + assert quota == _QuotaInfo( name="another-user", gpu_time_spent=float(101 * 60), gpu_time_limit=float(201 * 60), @@ -97,8 +97,8 @@ async def handle_stats(request: web.Request) -> web.StreamResponse: srv = await aiohttp_server(app) async with make_client(srv.make_url("/")) as client: - quota = await client.quota.get() - assert quota == QuotaInfo( + quota = await client._quota.get() + assert quota == _QuotaInfo( name=client.username, gpu_time_spent=float(101 * 60), gpu_time_limit=float("inf"), @@ -124,4 +124,4 @@ async def handle_stats(request: web.Request) -> web.StreamResponse: with pytest.raises( AuthorizationError, match='"uri": "user://another-user", "action": "read"' ): - await client.quota.get("another-user") + await client._quota.get("another-user") diff --git a/tests/cli/test_formatters.py b/tests/cli/test_formatters.py index 1afc35780..1499cd051 100644 --- a/tests/cli/test_formatters.py +++ b/tests/cli/test_formatters.py @@ -34,7 +34,7 @@ ImageProgressSave, ) from neuromation.api.parsing_utils import _ImageNameParser -from neuromation.api.quota import QuotaInfo +from neuromation.api.quota import _QuotaInfo from neuromation.cli.formatters import ( BaseFilesFormatter, ConfigFormatter, @@ -1481,7 +1481,7 @@ async def test_output_for_tpu_presets(self, root: Root, monkeypatch: Any) -> Non class TestQuotaInfoFormatter: def test_output(self) -> None: - quota = QuotaInfo( + quota = _QuotaInfo( name="user", gpu_time_spent=0.0, gpu_time_limit=0.0, @@ -1499,7 +1499,7 @@ def test_output(self) -> None: ) def test_output_no_quota(self) -> None: - quota = QuotaInfo( + quota = _QuotaInfo( name="user", gpu_time_spent=0.0, gpu_time_limit=float("inf"), @@ -1515,7 +1515,7 @@ def test_output_no_quota(self) -> None: ) def test_output_too_many_hours(self) -> None: - quota = QuotaInfo( + quota = _QuotaInfo( name="user", gpu_time_spent=float((1 * 60 + 29) * 60), gpu_time_limit=float((9 * 60 + 59) * 60), @@ -1533,7 +1533,7 @@ def test_output_too_many_hours(self) -> None: ) def test_output_spent_more_than_quota_left_zero(self) -> None: - quota = QuotaInfo( + quota = _QuotaInfo( name="user", gpu_time_spent=float(9 * 60 * 60), gpu_time_limit=float(1 * 60 * 60),