Skip to content

Commit

Permalink
Use sync mixin in auto class generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson committed Jan 15, 2025
1 parent 319822d commit 8345f3c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 96 deletions.
112 changes: 93 additions & 19 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import kr8s
import kr8s.asyncio
from kr8s._api import Api
from kr8s._async_utils import sync
from kr8s._async_utils import run_sync
from kr8s._data_utils import (
dict_to_selector,
dot_to_nested_dict,
Expand Down Expand Up @@ -768,6 +768,76 @@ async def list(cls, **kwargs) -> AsyncGenerator[Self]:
yield resource


class APIObjectSyncMixin(APIObject):
_asyncio = False

@classmethod
def get( # type: ignore
cls,
name: str | None = None,
namespace: str | None = None,
api: Api | None = None,
label_selector: str | dict[str, str] | None = None,
field_selector: str | dict[str, str] | None = None,
timeout: int = 2,
**kwargs,
) -> Self:
return run_sync(cls.async_get)(
name=name,
namespace=namespace,
api=api,
label_selector=label_selector,
field_selector=field_selector,
timeout=timeout,
**kwargs,
) # type: ignore

def exists(self, ensure=False) -> bool: # type: ignore
return run_sync(self.async_exists)(ensure=ensure) # type: ignore

def create(self) -> None: # type: ignore
return run_sync(self.async_create)() # type: ignore

def delete(self, propagation_policy: str | None = None) -> None: # type: ignore
return run_sync(self.async_delete)(propagation_policy=propagation_policy) # type: ignore

def refresh(self):
return run_sync(self.async_refresh)() # type: ignore

def patch(self, patch, *, subresource=None, type=None):
return run_sync(self.async_patch)(patch, subresource=subresource, type=type) # type: ignore

def scale(self, replicas=None):
return run_sync(self.async_scale)(replicas=replicas) # type: ignore

def watch(self):
yield from run_sync(self.async_watch)()

def wait(
self,
conditions: list[str] | str,
mode: Literal["any", "all"] = "any",
timeout: int | float | None = None,
):
return run_sync(self.async_wait)(conditions, mode=mode, timeout=timeout) # type: ignore

def annotate(self, annotations=None, **kwargs):
return run_sync(self.async_annotate)(annotations, **kwargs) # type: ignore

def label(self, labels=None, **kwargs):
return run_sync(self.async_label)(labels, **kwargs) # type: ignore

def set_owner(self, owner):
return run_sync(self.async_set_owner)(owner) # type: ignore

def adopt(self, child):
return run_sync(self.async_adopt)(child) # type: ignore

@classmethod
def list(cls):
yield from run_sync(cls.async_list)()


## v1 objects


Expand Down Expand Up @@ -1968,24 +2038,28 @@ def new_class(
if version is None:
version = "v1"
plural = plural or kind.lower() + "s"
newcls = type(
kind,
(APIObject,),
{
"kind": kind,
"version": version,
"_asyncio": asyncio,
"endpoint": plural.lower(),
"plural": plural.lower(),
"singular": kind.lower(),
"namespaced": namespaced,
"scalable": scalable or False,
"scalable_spec": scalable_spec or "replicas",
},
)
if not asyncio:
newcls = sync(newcls)
return newcls
construct_args = {
"kind": kind,
"version": version,
"_asyncio": asyncio,
"endpoint": plural.lower(),
"plural": plural.lower(),
"singular": kind.lower(),
"namespaced": namespaced,
"scalable": scalable or False,
"scalable_spec": scalable_spec or "replicas",
}
if asyncio:
return type(kind, (APIObject,), construct_args)
else:
return type(
kind,
(
APIObjectSyncMixin,
APIObject,
),
construct_args,
)


def object_from_spec(
Expand Down
79 changes: 2 additions & 77 deletions kr8s/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@
from __future__ import annotations

from functools import partial
from typing import Any, Literal
from typing import Any

import httpx
from typing_extensions import Self

from kr8s._api import Api

from ._async_utils import run_sync
from ._objects import (
APIObject as _APIObject,
)
from ._objects import APIObjectSyncMixin
from ._objects import (
Binding as _Binding,
)
Expand Down Expand Up @@ -143,76 +138,6 @@
from .portforward import PortForward


class APIObjectSyncMixin(_APIObject):
_asyncio = False

@classmethod
def get( # type: ignore
cls,
name: str | None = None,
namespace: str | None = None,
api: Api | None = None,
label_selector: str | dict[str, str] | None = None,
field_selector: str | dict[str, str] | None = None,
timeout: int = 2,
**kwargs,
) -> Self:
return run_sync(cls.async_get)(
name=name,
namespace=namespace,
api=api,
label_selector=label_selector,
field_selector=field_selector,
timeout=timeout,
**kwargs,
) # type: ignore

def exists(self, ensure=False) -> bool: # type: ignore
return run_sync(self.async_exists)(ensure=ensure) # type: ignore

def create(self) -> None: # type: ignore
return run_sync(self.async_create)() # type: ignore

def delete(self, propagation_policy: str | None = None) -> None: # type: ignore
return run_sync(self.async_delete)(propagation_policy=propagation_policy) # type: ignore

def refresh(self):
return run_sync(self.async_refresh)() # type: ignore

def patch(self, patch, *, subresource=None, type=None):
return run_sync(self.async_patch)(patch, subresource=subresource, type=type) # type: ignore

def scale(self, replicas=None):
return run_sync(self.async_scale)(replicas=replicas) # type: ignore

def watch(self):
yield from run_sync(self.async_watch)()

def wait(
self,
conditions: list[str] | str,
mode: Literal["any", "all"] = "any",
timeout: int | float | None = None,
):
return run_sync(self.async_wait)(conditions, mode=mode, timeout=timeout) # type: ignore

def annotate(self, annotations=None, **kwargs):
return run_sync(self.async_annotate)(annotations, **kwargs) # type: ignore

def label(self, labels=None, **kwargs):
return run_sync(self.async_label)(labels, **kwargs) # type: ignore

def set_owner(self, owner):
return run_sync(self.async_set_owner)(owner) # type: ignore

def adopt(self, child):
return run_sync(self.async_adopt)(child) # type: ignore

@classmethod
def list(cls):
yield from run_sync(cls.async_list)()


class Binding(APIObjectSyncMixin, _Binding):
__doc__ = _Binding.__doc__

Expand Down

0 comments on commit 8345f3c

Please sign in to comment.