kalbio 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kalbio/__init__.py +1 -0
- kalbio/_kaleidoscope_model.py +111 -0
- kalbio/activities.py +1202 -0
- kalbio/client.py +463 -0
- kalbio/dashboards.py +276 -0
- kalbio/entity_fields.py +474 -0
- kalbio/entity_types.py +188 -0
- kalbio/exports.py +126 -0
- kalbio/helpers.py +52 -0
- kalbio/imports.py +89 -0
- kalbio/labels.py +88 -0
- kalbio/programs.py +96 -0
- kalbio/property_fields.py +81 -0
- kalbio/record_views.py +191 -0
- kalbio/records.py +1173 -0
- kalbio/workspace.py +315 -0
- kalbio-0.2.0.dist-info/METADATA +289 -0
- kalbio-0.2.0.dist-info/RECORD +19 -0
- kalbio-0.2.0.dist-info/WHEEL +4 -0
kalbio/record_views.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Module for managing Kaleidoscope record views and their operations.
|
|
2
|
+
|
|
3
|
+
This module provides classes and services for interacting with record views in the Kaleidoscope system.
|
|
4
|
+
|
|
5
|
+
Classes:
|
|
6
|
+
RecordTransfer: TypedDict defining the structure for transferring records with key field values.
|
|
7
|
+
ViewField: TypedDict defining the structure for view fields with data and lookup field references.
|
|
8
|
+
RecordView: Model representing a record view with methods for extending views.
|
|
9
|
+
RecordViewsService: Service class for managing record view operations and API interactions.
|
|
10
|
+
|
|
11
|
+
Example:
|
|
12
|
+
```python
|
|
13
|
+
views = client.record_views.get_record_views()
|
|
14
|
+
for view in views:
|
|
15
|
+
print(f"View: {view.view_name}, Entity Slice: {view.entity_slice_id}")
|
|
16
|
+
|
|
17
|
+
# View: Customer Records, Entity Slice: abc-123-def
|
|
18
|
+
# View: Product Catalog, Entity Slice: xyz-456-ghi
|
|
19
|
+
```
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import logging
|
|
23
|
+
from functools import lru_cache
|
|
24
|
+
from kalbio._kaleidoscope_model import _KaleidoscopeBaseModel
|
|
25
|
+
from kalbio.client import KaleidoscopeClient
|
|
26
|
+
from pydantic import TypeAdapter
|
|
27
|
+
from typing import Any, Dict, List, Optional, TypedDict
|
|
28
|
+
|
|
29
|
+
_logger = logging.getLogger(__name__)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class RecordTransfer(TypedDict):
|
|
33
|
+
"""TypedDict defining the structure for transferring records with key field values.
|
|
34
|
+
|
|
35
|
+
Attributes:
|
|
36
|
+
record_id (str): The unique identifier of the record to transfer.
|
|
37
|
+
key_field_name_to_value (Dict[str, Any]): A dictionary mapping key field names to their values.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
record_id: str
|
|
41
|
+
key_field_name_to_value: Dict[str, Any]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ViewField(TypedDict):
|
|
45
|
+
"""TypedDict defining the structure for view fields with data and lookup field references.
|
|
46
|
+
|
|
47
|
+
Attributes:
|
|
48
|
+
data_field_id (Optional[str]): The ID of the data field, if applicable.
|
|
49
|
+
lookup_field_id (Optional[str]): The ID of the lookup field, if applicable.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
data_field_id: Optional[str]
|
|
53
|
+
lookup_field_id: Optional[str]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class RecordView(_KaleidoscopeBaseModel):
|
|
57
|
+
"""Represents a view of records in the Kaleidoscope system.
|
|
58
|
+
|
|
59
|
+
A RecordView defines how records are displayed and accessed, including the entity slice
|
|
60
|
+
they belong to, associated programs and operations, and the fields visible in the view.
|
|
61
|
+
|
|
62
|
+
Attributes:
|
|
63
|
+
id (str): UUID of the record view
|
|
64
|
+
entity_slice_id (str): ID of the entity slice this view belongs to.
|
|
65
|
+
program_ids (List[str]): List of program IDs associated with this view. Defaults to empty list.
|
|
66
|
+
operation_ids (Optional[List[str]]): Optional list of operation IDs associated with this view.
|
|
67
|
+
operation_definition_ids (Optional[List[str]]): Optional list of operation definition IDs.
|
|
68
|
+
view_fields (List[ViewField]): List of fields visible in this view. Defaults to empty list.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
view_name: str
|
|
72
|
+
entity_slice_id: str
|
|
73
|
+
program_ids: List[str] = []
|
|
74
|
+
operation_ids: Optional[List[str]] = None
|
|
75
|
+
operation_definition_ids: Optional[List[str]] = None
|
|
76
|
+
view_fields: List[ViewField] = []
|
|
77
|
+
|
|
78
|
+
def __str__(self):
|
|
79
|
+
return f"{self.view_name}"
|
|
80
|
+
|
|
81
|
+
class ExtendViewBody(TypedDict):
|
|
82
|
+
"""TypedDict defining the body for extending a record view.
|
|
83
|
+
|
|
84
|
+
Attributes:
|
|
85
|
+
new_key_field_name (str): The name of the new key field to add.
|
|
86
|
+
records_to_transfer (Optional[List[RecordTransfer]]): A list of records to transfer with their new key values.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
new_key_field_name: str
|
|
90
|
+
records_to_transfer: Optional[List[RecordTransfer]]
|
|
91
|
+
|
|
92
|
+
def extend_view(self, body: ExtendViewBody) -> None:
|
|
93
|
+
"""Extends the current record view by adding a key field.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
body (ExtendViewBody): The request body containing information about the key field to add.
|
|
97
|
+
"""
|
|
98
|
+
try:
|
|
99
|
+
resp = self._client._put(
|
|
100
|
+
"/record_views/" + self.id + "/add_key_field", dict(body)
|
|
101
|
+
)
|
|
102
|
+
if resp:
|
|
103
|
+
for key, value in resp.items():
|
|
104
|
+
if hasattr(self, key):
|
|
105
|
+
setattr(self, key, value)
|
|
106
|
+
except Exception as e:
|
|
107
|
+
_logger.error(f"Error updating record view: {e}")
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class RecordViewsService:
|
|
112
|
+
"""Service class for managing record views in Kaleidoscope.
|
|
113
|
+
|
|
114
|
+
This service provides methods to interact with record views, including retrieving,
|
|
115
|
+
creating, and managing RecordView objects. It handles the conversion of raw data
|
|
116
|
+
into RecordView instances and ensures proper client association.
|
|
117
|
+
|
|
118
|
+
Example:
|
|
119
|
+
```python
|
|
120
|
+
views = client.record_views.get_record_views()
|
|
121
|
+
for view in views:
|
|
122
|
+
print(f"View: {view.view_name}, Entity Slice: {view.entity_slice_id}")
|
|
123
|
+
|
|
124
|
+
# View: Customer Records, Entity Slice: abc-123-def
|
|
125
|
+
# View: Product Catalog, Entity Slice: xyz-456-ghi
|
|
126
|
+
```
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
def __init__(self, client: KaleidoscopeClient):
|
|
130
|
+
self._client = client
|
|
131
|
+
|
|
132
|
+
def _create_record_view(self, data: dict) -> RecordView:
|
|
133
|
+
"""Create a RecordView instance from a dictionary of data.
|
|
134
|
+
|
|
135
|
+
This internal method validates the provided data against the RecordView model,
|
|
136
|
+
sets the client reference, and returns the configured RecordView instance.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
data (dict): A dictionary containing the data to populate the RecordView.
|
|
140
|
+
Must conform to the RecordView model schema.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
RecordView: A validated RecordView instance with the client reference set.
|
|
144
|
+
|
|
145
|
+
Raises:
|
|
146
|
+
ValidationError: If the data could not be validated as an RecordView.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
record_view = RecordView.model_validate(data)
|
|
150
|
+
record_view._set_client(self._client)
|
|
151
|
+
|
|
152
|
+
return record_view
|
|
153
|
+
|
|
154
|
+
def _create_record_views_list(self, data: list[dict]) -> List[RecordView]:
|
|
155
|
+
"""Converts a list of data dictionaries into a list of RecordView objects and sets the client for each RecordView.
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
data (list): A list of dictionaries representing record view data.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
List[RecordView]: A list of RecordView objects with the client set.
|
|
162
|
+
|
|
163
|
+
Raises:
|
|
164
|
+
ValidationError: If the data could not be validated as a list of RecordView objects.
|
|
165
|
+
"""
|
|
166
|
+
record_views = TypeAdapter(List[RecordView]).validate_python(data)
|
|
167
|
+
for record_view in record_views:
|
|
168
|
+
record_view._set_client(self._client)
|
|
169
|
+
|
|
170
|
+
return record_views
|
|
171
|
+
|
|
172
|
+
@lru_cache
|
|
173
|
+
def get_record_views(self) -> List[RecordView]:
|
|
174
|
+
"""Retrieves a list of record views available in the workspace.
|
|
175
|
+
|
|
176
|
+
This method caches its values.
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
List[RecordView]: A list of RecordView objects representing the record views in the workspace.
|
|
180
|
+
|
|
181
|
+
Note:
|
|
182
|
+
If an exception occurs during the API request, it logs the error,
|
|
183
|
+
clears the cache, and returns an empty list.
|
|
184
|
+
"""
|
|
185
|
+
try:
|
|
186
|
+
resp = self._client._get("/record_views")
|
|
187
|
+
return self._create_record_views_list(resp)
|
|
188
|
+
except Exception as e:
|
|
189
|
+
_logger.error(f"Error fetching record views: {e}")
|
|
190
|
+
self.get_record_views.cache_clear()
|
|
191
|
+
return []
|