-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rework logger implementation - rework threadpool implementation - migrate proxyobject into its own module
- Loading branch information
Showing
10 changed files
with
829 additions
and
622 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
Simple Proxied Objects | ||
""" | ||
|
||
from .main import ProxyObject, ProxyObjT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
import threading | ||
import contextlib | ||
from typing import Any, Type, Tuple, Dict, List, Union, Optional, Callable, TypeVar, Generic, TYPE_CHECKING | ||
|
||
|
||
ProxyObjT = TypeVar('ProxyObjT') | ||
|
||
|
||
class ProxyObject(Generic[ProxyObjT]): | ||
def __init__( | ||
self, | ||
obj_cls: Optional[Type[ProxyObjT]] = None, | ||
obj_getter: Optional[Union[Callable, str]] = None, | ||
obj_args: Optional[List[Any]] = None, | ||
obj_kwargs: Optional[Dict[str, Any]] = None, | ||
threadsafe: Optional[bool] = False, | ||
debug_enabled: Optional[bool] = False, | ||
): | ||
""" | ||
Proxy Object | ||
args: | ||
obj_cls: the class of the object | ||
obj_getter: the function to get the object | ||
debug_enabled: if True, will raise an error if the object is not found | ||
""" | ||
# Intentionally underscore on the end to avoid conflicts with the settings | ||
assert obj_cls or obj_getter, "Either `obj_cls` or `obj_getter` must be provided" | ||
self.__obj_cls_ = obj_cls | ||
self.__obj_getter_ = obj_getter | ||
if self.__obj_getter_ and isinstance(self.__obj_getter_, str): | ||
from lazyops.utils.helpers import lazy_import | ||
self.__obj_getter_ = lazy_import(self.__obj_getter_) | ||
self.__threadlock_ = None if threadsafe else threading.Lock() | ||
self.__obj_ = None | ||
self.__obj_args_ = obj_args or [] | ||
self.__obj_kwargs_ = obj_kwargs or {} | ||
self.__debug_enabled_ = debug_enabled | ||
self.__last_attrs_: Dict[str, int] = {} | ||
|
||
@contextlib.contextmanager | ||
def _objlock_(self): | ||
""" | ||
Returns the object lock | ||
""" | ||
if self.__threadlock_ is not None: | ||
try: | ||
with self.__threadlock_: | ||
yield | ||
except Exception as e: | ||
raise e | ||
else: | ||
yield | ||
|
||
@property | ||
def _obj_(self) -> ProxyObjT: | ||
""" | ||
Returns the object | ||
""" | ||
if self.__obj_ is None: | ||
with self._objlock_(): | ||
if self.__obj_getter_: | ||
self.__obj_ = self.__obj_getter_(*self.__obj_args_, **self.__obj_kwargs_) | ||
elif self.__obj_cls_: | ||
self.__obj_ = self.__obj_cls_(*self.__obj_args_, **self.__obj_kwargs_) | ||
return self.__obj_ | ||
|
||
def __getattr__(self, name): | ||
""" | ||
Forward all unknown attributes to the proxy object | ||
""" | ||
if not self.__debug_enabled_: | ||
return getattr(self._obj_, name) | ||
|
||
# Try to debug the attribute | ||
if name not in self.__last_attrs_: | ||
self.__last_attrs_[name] = 0 | ||
self.__last_attrs_[name] += 1 | ||
if self.__last_attrs_[name] > 5: | ||
raise AttributeError(f"Proxy object has no attribute {name}") | ||
|
||
if hasattr(self._obj_, name): | ||
self.__last_attrs_[name] = 0 | ||
return getattr(self._obj_, name) | ||
raise AttributeError(f"Settings object has no attribute {name}") | ||
|
||
if TYPE_CHECKING: | ||
def __new__(cls, *args, **kwargs) -> ProxyObjT: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.