Path: blob/main/singlestoredb/notebook/_objects.py
469 views
#!/usr/bin/env python1import functools2from typing import Any3from typing import Optional45from ..management import workspace as _ws678class Secrets(object):9"""Wrapper for accessing secrets as object attributes."""1011def __getattr__(self, name: str) -> Optional[str]:12if name.startswith('_ipython') or name.startswith('_repr_'):13raise AttributeError(name)14return _ws.get_secret(name)1516def __getitem__(self, name: str) -> Optional[str]:17return _ws.get_secret(name)181920class Stage(object):2122def __new__(cls, *args: Any, **kwargs: Any) -> Any:23# We are remapping the methods and attributes here so that24# autocomplete still works in Jupyter / IPython, but we25# bypass the real method / attribute calls and apply them26# to the currently selected stage.27for name in [x for x in dir(_ws.Stage) if not x.startswith('_')]:28if name in ['from_dict', 'refresh', 'update']:29continue30attr = getattr(_ws.Stage, name)3132def make_wrapper(m: str, is_method: bool = False) -> Any:33if is_method:34def wrap(self: Stage, *a: Any, **kw: Any) -> Any:35return getattr(_ws.get_stage(), m)(*a, **kw)36return functools.update_wrapper(wrap, attr)37else:38def wrap(self: Stage, *a: Any, **kw: Any) -> Any:39return getattr(_ws.get_stage(), m)40return property(functools.update_wrapper(wrap, attr))4142setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))4344for name in [45x for x in _ws.Stage.__annotations__.keys()46if not x.startswith('_')47]:4849def make_wrapper(m: str, is_method: bool = False) -> Any:50def wrap(self: Stage) -> Any:51return getattr(_ws.get_stage(), m)52return property(functools.update_wrapper(wrap, attr))5354setattr(cls, name, make_wrapper(m=name))5556cls.__doc__ = _ws.Stage.__doc__5758return super().__new__(cls, *args, **kwargs)596061class WorkspaceGroup(object):6263def __new__(cls, *args: Any, **kwargs: Any) -> Any:64# We are remapping the methods and attributes here so that65# autocomplete still works in Jupyter / IPython, but we66# bypass the real method / attribute calls and apply them67# to the currently selected workspace group.68for name in [x for x in dir(_ws.WorkspaceGroup) if not x.startswith('_')]:69if name in ['from_dict', 'refresh', 'update']:70continue7172attr = getattr(_ws.WorkspaceGroup, name)7374def make_wrapper(m: str, is_method: bool = False) -> Any:75if is_method:76def wrap(self: WorkspaceGroup, *a: Any, **kw: Any) -> Any:77return getattr(_ws.get_workspace_group(), m)(*a, **kw)78return functools.update_wrapper(wrap, attr)79else:80def wrap(self: WorkspaceGroup, *a: Any, **kw: Any) -> Any:81return getattr(_ws.get_workspace_group(), m)82return property(functools.update_wrapper(wrap, attr))8384setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))8586for name in [87x for x in _ws.WorkspaceGroup.__annotations__.keys()88if not x.startswith('_')89]:9091def make_wrapper(m: str, is_method: bool = False) -> Any:92def wrap(self: WorkspaceGroup) -> Any:93return getattr(_ws.get_workspace_group(), m)94return property(functools.update_wrapper(wrap, attr))9596setattr(cls, name, make_wrapper(m=name))9798cls.__doc__ = _ws.WorkspaceGroup.__doc__99100return super().__new__(cls, *args, **kwargs)101102def __str__(self) -> str:103return _ws.get_workspace_group().__str__()104105def __repr__(self) -> str:106return _ws.get_workspace_group().__repr__()107108109class Workspace(object):110111def __new__(cls, *args: Any, **kwargs: Any) -> Any:112# We are remapping the methods and attributes here so that113# autocomplete still works in Jupyter / IPython, but we114# bypass the real method / attribute calls and apply them115# to the currently selected workspace.116for name in [x for x in dir(_ws.Workspace) if not x.startswith('_')]:117if name in ['from_dict', 'refresh', 'update']:118continue119120attr = getattr(_ws.Workspace, name)121122def make_wrapper(m: str, is_method: bool = False) -> Any:123if is_method:124def wrap(self: Workspace, *a: Any, **kw: Any) -> Any:125return getattr(_ws.get_workspace(), m)(*a, **kw)126return functools.update_wrapper(wrap, attr)127else:128def wrap(self: Workspace, *a: Any, **kw: Any) -> Any:129return getattr(_ws.get_workspace(), m)130return property(functools.update_wrapper(wrap, attr))131132setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))133134for name in [135x for x in _ws.Workspace.__annotations__.keys()136if not x.startswith('_')137]:138139def make_wrapper(m: str, is_method: bool = False) -> Any:140def wrap(self: Workspace) -> Any:141return getattr(_ws.get_workspace(), m)142return property(functools.update_wrapper(wrap, attr))143144setattr(cls, name, make_wrapper(m=name))145146cls.__doc__ = _ws.Workspace.__doc__147148return super().__new__(cls, *args, **kwargs)149150def __str__(self) -> str:151return _ws.get_workspace().__str__()152153def __repr__(self) -> str:154return _ws.get_workspace().__repr__()155156157class Organization(object):158159def __new__(cls, *args: Any, **kwargs: Any) -> Any:160# We are remapping the methods and attributes here so that161# autocomplete still works in Jupyter / IPython, but we162# bypass the real method / attribute calls and apply them163# to the currently selected organization.164for name in [x for x in dir(_ws.Organization) if not x.startswith('_')]:165if name in ['from_dict', 'refresh', 'update']:166continue167168attr = getattr(_ws.Organization, name)169170def make_wrapper(m: str, is_method: bool = False) -> Any:171if is_method:172def wrap(self: Organization, *a: Any, **kw: Any) -> Any:173return getattr(_ws.get_organization(), m)(*a, **kw)174return functools.update_wrapper(wrap, attr)175else:176def wrap(self: Organization, *a: Any, **kw: Any) -> Any:177return getattr(_ws.get_organization(), m)178return property(functools.update_wrapper(wrap, attr))179180setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))181182for name in [183x for x in _ws.Organization.__annotations__.keys()184if not x.startswith('_')185]:186187def make_wrapper(m: str, is_method: bool = False) -> Any:188def wrap(self: Organization) -> Any:189return getattr(_ws.get_organization(), m)190return property(functools.update_wrapper(wrap, attr))191192setattr(cls, name, make_wrapper(m=name))193194cls.__doc__ = _ws.Organization.__doc__195196return super().__new__(cls, *args, **kwargs)197198def __str__(self) -> str:199return _ws.get_organization().__str__()200201def __repr__(self) -> str:202return _ws.get_organization().__repr__()203204205secrets = Secrets()206stage = Stage()207organization = Organization()208workspace_group = WorkspaceGroup()209workspace = Workspace()210211212__all__ = ['secrets', 'stage', 'workspace', 'workspace_group', 'organization']213214215