Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/notebook/_objects.py
469 views
1
#!/usr/bin/env python
2
import functools
3
from typing import Any
4
from typing import Optional
5
6
from ..management import workspace as _ws
7
8
9
class Secrets(object):
10
"""Wrapper for accessing secrets as object attributes."""
11
12
def __getattr__(self, name: str) -> Optional[str]:
13
if name.startswith('_ipython') or name.startswith('_repr_'):
14
raise AttributeError(name)
15
return _ws.get_secret(name)
16
17
def __getitem__(self, name: str) -> Optional[str]:
18
return _ws.get_secret(name)
19
20
21
class Stage(object):
22
23
def __new__(cls, *args: Any, **kwargs: Any) -> Any:
24
# We are remapping the methods and attributes here so that
25
# autocomplete still works in Jupyter / IPython, but we
26
# bypass the real method / attribute calls and apply them
27
# to the currently selected stage.
28
for name in [x for x in dir(_ws.Stage) if not x.startswith('_')]:
29
if name in ['from_dict', 'refresh', 'update']:
30
continue
31
attr = getattr(_ws.Stage, name)
32
33
def make_wrapper(m: str, is_method: bool = False) -> Any:
34
if is_method:
35
def wrap(self: Stage, *a: Any, **kw: Any) -> Any:
36
return getattr(_ws.get_stage(), m)(*a, **kw)
37
return functools.update_wrapper(wrap, attr)
38
else:
39
def wrap(self: Stage, *a: Any, **kw: Any) -> Any:
40
return getattr(_ws.get_stage(), m)
41
return property(functools.update_wrapper(wrap, attr))
42
43
setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))
44
45
for name in [
46
x for x in _ws.Stage.__annotations__.keys()
47
if not x.startswith('_')
48
]:
49
50
def make_wrapper(m: str, is_method: bool = False) -> Any:
51
def wrap(self: Stage) -> Any:
52
return getattr(_ws.get_stage(), m)
53
return property(functools.update_wrapper(wrap, attr))
54
55
setattr(cls, name, make_wrapper(m=name))
56
57
cls.__doc__ = _ws.Stage.__doc__
58
59
return super().__new__(cls, *args, **kwargs)
60
61
62
class WorkspaceGroup(object):
63
64
def __new__(cls, *args: Any, **kwargs: Any) -> Any:
65
# We are remapping the methods and attributes here so that
66
# autocomplete still works in Jupyter / IPython, but we
67
# bypass the real method / attribute calls and apply them
68
# to the currently selected workspace group.
69
for name in [x for x in dir(_ws.WorkspaceGroup) if not x.startswith('_')]:
70
if name in ['from_dict', 'refresh', 'update']:
71
continue
72
73
attr = getattr(_ws.WorkspaceGroup, name)
74
75
def make_wrapper(m: str, is_method: bool = False) -> Any:
76
if is_method:
77
def wrap(self: WorkspaceGroup, *a: Any, **kw: Any) -> Any:
78
return getattr(_ws.get_workspace_group(), m)(*a, **kw)
79
return functools.update_wrapper(wrap, attr)
80
else:
81
def wrap(self: WorkspaceGroup, *a: Any, **kw: Any) -> Any:
82
return getattr(_ws.get_workspace_group(), m)
83
return property(functools.update_wrapper(wrap, attr))
84
85
setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))
86
87
for name in [
88
x for x in _ws.WorkspaceGroup.__annotations__.keys()
89
if not x.startswith('_')
90
]:
91
92
def make_wrapper(m: str, is_method: bool = False) -> Any:
93
def wrap(self: WorkspaceGroup) -> Any:
94
return getattr(_ws.get_workspace_group(), m)
95
return property(functools.update_wrapper(wrap, attr))
96
97
setattr(cls, name, make_wrapper(m=name))
98
99
cls.__doc__ = _ws.WorkspaceGroup.__doc__
100
101
return super().__new__(cls, *args, **kwargs)
102
103
def __str__(self) -> str:
104
return _ws.get_workspace_group().__str__()
105
106
def __repr__(self) -> str:
107
return _ws.get_workspace_group().__repr__()
108
109
110
class Workspace(object):
111
112
def __new__(cls, *args: Any, **kwargs: Any) -> Any:
113
# We are remapping the methods and attributes here so that
114
# autocomplete still works in Jupyter / IPython, but we
115
# bypass the real method / attribute calls and apply them
116
# to the currently selected workspace.
117
for name in [x for x in dir(_ws.Workspace) if not x.startswith('_')]:
118
if name in ['from_dict', 'refresh', 'update']:
119
continue
120
121
attr = getattr(_ws.Workspace, name)
122
123
def make_wrapper(m: str, is_method: bool = False) -> Any:
124
if is_method:
125
def wrap(self: Workspace, *a: Any, **kw: Any) -> Any:
126
return getattr(_ws.get_workspace(), m)(*a, **kw)
127
return functools.update_wrapper(wrap, attr)
128
else:
129
def wrap(self: Workspace, *a: Any, **kw: Any) -> Any:
130
return getattr(_ws.get_workspace(), m)
131
return property(functools.update_wrapper(wrap, attr))
132
133
setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))
134
135
for name in [
136
x for x in _ws.Workspace.__annotations__.keys()
137
if not x.startswith('_')
138
]:
139
140
def make_wrapper(m: str, is_method: bool = False) -> Any:
141
def wrap(self: Workspace) -> Any:
142
return getattr(_ws.get_workspace(), m)
143
return property(functools.update_wrapper(wrap, attr))
144
145
setattr(cls, name, make_wrapper(m=name))
146
147
cls.__doc__ = _ws.Workspace.__doc__
148
149
return super().__new__(cls, *args, **kwargs)
150
151
def __str__(self) -> str:
152
return _ws.get_workspace().__str__()
153
154
def __repr__(self) -> str:
155
return _ws.get_workspace().__repr__()
156
157
158
class Organization(object):
159
160
def __new__(cls, *args: Any, **kwargs: Any) -> Any:
161
# We are remapping the methods and attributes here so that
162
# autocomplete still works in Jupyter / IPython, but we
163
# bypass the real method / attribute calls and apply them
164
# to the currently selected organization.
165
for name in [x for x in dir(_ws.Organization) if not x.startswith('_')]:
166
if name in ['from_dict', 'refresh', 'update']:
167
continue
168
169
attr = getattr(_ws.Organization, name)
170
171
def make_wrapper(m: str, is_method: bool = False) -> Any:
172
if is_method:
173
def wrap(self: Organization, *a: Any, **kw: Any) -> Any:
174
return getattr(_ws.get_organization(), m)(*a, **kw)
175
return functools.update_wrapper(wrap, attr)
176
else:
177
def wrap(self: Organization, *a: Any, **kw: Any) -> Any:
178
return getattr(_ws.get_organization(), m)
179
return property(functools.update_wrapper(wrap, attr))
180
181
setattr(cls, name, make_wrapper(m=name, is_method=callable(attr)))
182
183
for name in [
184
x for x in _ws.Organization.__annotations__.keys()
185
if not x.startswith('_')
186
]:
187
188
def make_wrapper(m: str, is_method: bool = False) -> Any:
189
def wrap(self: Organization) -> Any:
190
return getattr(_ws.get_organization(), m)
191
return property(functools.update_wrapper(wrap, attr))
192
193
setattr(cls, name, make_wrapper(m=name))
194
195
cls.__doc__ = _ws.Organization.__doc__
196
197
return super().__new__(cls, *args, **kwargs)
198
199
def __str__(self) -> str:
200
return _ws.get_organization().__str__()
201
202
def __repr__(self) -> str:
203
return _ws.get_organization().__repr__()
204
205
206
secrets = Secrets()
207
stage = Stage()
208
organization = Organization()
209
workspace_group = WorkspaceGroup()
210
workspace = Workspace()
211
212
213
__all__ = ['secrets', 'stage', 'workspace', 'workspace_group', 'organization']
214
215