pltr-cli 0.1.2__py3-none-any.whl → 0.3.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.
@@ -0,0 +1,442 @@
1
+ """
2
+ Ontology service wrappers for Foundry SDK.
3
+ """
4
+
5
+ from typing import Any, Optional, Dict, List
6
+ from .base import BaseService
7
+
8
+
9
+ class OntologyService(BaseService):
10
+ """Service wrapper for Foundry ontology operations."""
11
+
12
+ def _get_service(self) -> Any:
13
+ """Get the Foundry ontologies service."""
14
+ return self.client.ontologies
15
+
16
+ def list_ontologies(self) -> List[Dict[str, Any]]:
17
+ """
18
+ List all ontologies visible to the current user.
19
+
20
+ Returns:
21
+ List of ontology information dictionaries
22
+ """
23
+ try:
24
+ result = self.service.Ontology.list()
25
+ ontologies = []
26
+ # The response has a 'data' field containing the list of ontologies
27
+ for ontology in result.data:
28
+ ontologies.append(self._format_ontology_info(ontology))
29
+ return ontologies
30
+ except Exception as e:
31
+ raise RuntimeError(f"Failed to list ontologies: {e}")
32
+
33
+ def get_ontology(self, ontology_rid: str) -> Dict[str, Any]:
34
+ """
35
+ Get a specific ontology by RID.
36
+
37
+ Args:
38
+ ontology_rid: Ontology Resource Identifier
39
+
40
+ Returns:
41
+ Ontology information dictionary
42
+ """
43
+ try:
44
+ ontology = self.service.Ontology.get(ontology_rid)
45
+ return self._format_ontology_info(ontology)
46
+ except Exception as e:
47
+ raise RuntimeError(f"Failed to get ontology {ontology_rid}: {e}")
48
+
49
+ def _format_ontology_info(self, ontology: Any) -> Dict[str, Any]:
50
+ """Format ontology information for consistent output."""
51
+ return {
52
+ "rid": ontology.rid,
53
+ "api_name": getattr(ontology, "api_name", None),
54
+ "display_name": getattr(ontology, "display_name", None),
55
+ "description": getattr(ontology, "description", None),
56
+ }
57
+
58
+
59
+ class ObjectTypeService(BaseService):
60
+ """Service wrapper for object type operations."""
61
+
62
+ def _get_service(self) -> Any:
63
+ """Get the Foundry ontologies service."""
64
+ return self.client.ontologies
65
+
66
+ def list_object_types(self, ontology_rid: str) -> List[Dict[str, Any]]:
67
+ """
68
+ List object types in an ontology.
69
+
70
+ Args:
71
+ ontology_rid: Ontology Resource Identifier
72
+
73
+ Returns:
74
+ List of object type information dictionaries
75
+ """
76
+ try:
77
+ # ObjectType is nested under Ontology in the SDK
78
+ result = self.service.Ontology.ObjectType.list(ontology_rid)
79
+ object_types = []
80
+ # The response has a 'data' field containing the list of object types
81
+ for obj_type in result.data:
82
+ object_types.append(self._format_object_type_info(obj_type))
83
+ return object_types
84
+ except Exception as e:
85
+ raise RuntimeError(f"Failed to list object types: {e}")
86
+
87
+ def get_object_type(self, ontology_rid: str, object_type: str) -> Dict[str, Any]:
88
+ """
89
+ Get a specific object type.
90
+
91
+ Args:
92
+ ontology_rid: Ontology Resource Identifier
93
+ object_type: Object type API name
94
+
95
+ Returns:
96
+ Object type information dictionary
97
+ """
98
+ try:
99
+ # ObjectType is nested under Ontology in the SDK
100
+ obj_type = self.service.Ontology.ObjectType.get(ontology_rid, object_type)
101
+ return self._format_object_type_info(obj_type)
102
+ except Exception as e:
103
+ raise RuntimeError(f"Failed to get object type {object_type}: {e}")
104
+
105
+ def list_outgoing_link_types(
106
+ self, ontology_rid: str, object_type: str
107
+ ) -> List[Dict[str, Any]]:
108
+ """
109
+ List outgoing link types for an object type.
110
+
111
+ Args:
112
+ ontology_rid: Ontology Resource Identifier
113
+ object_type: Object type API name
114
+
115
+ Returns:
116
+ List of link type information dictionaries
117
+ """
118
+ try:
119
+ # ObjectType is nested under Ontology in the SDK
120
+ result = self.service.Ontology.ObjectType.list_outgoing_link_types(
121
+ ontology_rid, object_type
122
+ )
123
+ link_types = []
124
+ # The response has a 'data' field containing the list of link types
125
+ for link_type in result.data:
126
+ link_types.append(self._format_link_type_info(link_type))
127
+ return link_types
128
+ except Exception as e:
129
+ raise RuntimeError(f"Failed to list link types: {e}")
130
+
131
+ def _format_object_type_info(self, obj_type: Any) -> Dict[str, Any]:
132
+ """Format object type information for consistent output."""
133
+ return {
134
+ "api_name": obj_type.api_name,
135
+ "display_name": getattr(obj_type, "display_name", None),
136
+ "description": getattr(obj_type, "description", None),
137
+ "primary_key": getattr(obj_type, "primary_key", None),
138
+ "properties": getattr(obj_type, "properties", {}),
139
+ }
140
+
141
+ def _format_link_type_info(self, link_type: Any) -> Dict[str, Any]:
142
+ """Format link type information for consistent output."""
143
+ return {
144
+ "api_name": link_type.api_name,
145
+ "display_name": getattr(link_type, "display_name", None),
146
+ "object_type": getattr(link_type, "object_type", None),
147
+ "linked_object_type": getattr(link_type, "linked_object_type", None),
148
+ }
149
+
150
+
151
+ class OntologyObjectService(BaseService):
152
+ """Service wrapper for ontology object operations."""
153
+
154
+ def _get_service(self) -> Any:
155
+ """Get the Foundry ontologies service."""
156
+ return self.client.ontologies
157
+
158
+ def list_objects(
159
+ self,
160
+ ontology_rid: str,
161
+ object_type: str,
162
+ page_size: Optional[int] = None,
163
+ properties: Optional[List[str]] = None,
164
+ ) -> List[Dict[str, Any]]:
165
+ """
166
+ List objects of a specific type.
167
+
168
+ Args:
169
+ ontology_rid: Ontology Resource Identifier
170
+ object_type: Object type API name
171
+ page_size: Number of results per page
172
+ properties: List of properties to include
173
+
174
+ Returns:
175
+ List of object dictionaries
176
+ """
177
+ try:
178
+ result = self.service.OntologyObject.list(
179
+ ontology_rid,
180
+ object_type,
181
+ page_size=page_size,
182
+ properties=properties,
183
+ )
184
+ objects = []
185
+ for obj in result:
186
+ objects.append(self._format_object(obj))
187
+ return objects
188
+ except Exception as e:
189
+ raise RuntimeError(f"Failed to list objects: {e}")
190
+
191
+ def get_object(
192
+ self,
193
+ ontology_rid: str,
194
+ object_type: str,
195
+ primary_key: str,
196
+ properties: Optional[List[str]] = None,
197
+ ) -> Dict[str, Any]:
198
+ """
199
+ Get a specific object by primary key.
200
+
201
+ Args:
202
+ ontology_rid: Ontology Resource Identifier
203
+ object_type: Object type API name
204
+ primary_key: Object primary key
205
+ properties: List of properties to include
206
+
207
+ Returns:
208
+ Object dictionary
209
+ """
210
+ try:
211
+ obj = self.service.OntologyObject.get(
212
+ ontology_rid, object_type, primary_key, properties=properties
213
+ )
214
+ return self._format_object(obj)
215
+ except Exception as e:
216
+ raise RuntimeError(f"Failed to get object {primary_key}: {e}")
217
+
218
+ def aggregate_objects(
219
+ self,
220
+ ontology_rid: str,
221
+ object_type: str,
222
+ aggregations: List[Dict[str, Any]],
223
+ group_by: Optional[List[str]] = None,
224
+ filter: Optional[Dict[str, Any]] = None,
225
+ ) -> Dict[str, Any]:
226
+ """
227
+ Aggregate objects with specified functions.
228
+
229
+ Args:
230
+ ontology_rid: Ontology Resource Identifier
231
+ object_type: Object type API name
232
+ aggregations: List of aggregation specifications
233
+ group_by: Fields to group by
234
+ filter: Filter criteria
235
+
236
+ Returns:
237
+ Aggregation results
238
+ """
239
+ try:
240
+ result = self.service.OntologyObject.aggregate(
241
+ ontology_rid,
242
+ object_type,
243
+ aggregations=aggregations,
244
+ group_by=group_by,
245
+ filter=filter,
246
+ )
247
+ return result
248
+ except Exception as e:
249
+ raise RuntimeError(f"Failed to aggregate objects: {e}")
250
+
251
+ def list_linked_objects(
252
+ self,
253
+ ontology_rid: str,
254
+ object_type: str,
255
+ primary_key: str,
256
+ link_type: str,
257
+ page_size: Optional[int] = None,
258
+ properties: Optional[List[str]] = None,
259
+ ) -> List[Dict[str, Any]]:
260
+ """
261
+ List objects linked to a specific object.
262
+
263
+ Args:
264
+ ontology_rid: Ontology Resource Identifier
265
+ object_type: Object type API name
266
+ primary_key: Object primary key
267
+ link_type: Link type API name
268
+ page_size: Number of results per page
269
+ properties: List of properties to include
270
+
271
+ Returns:
272
+ List of linked object dictionaries
273
+ """
274
+ try:
275
+ result = self.service.OntologyObject.list_linked_objects(
276
+ ontology_rid,
277
+ object_type,
278
+ primary_key,
279
+ link_type,
280
+ page_size=page_size,
281
+ properties=properties,
282
+ )
283
+ objects = []
284
+ for obj in result:
285
+ objects.append(self._format_object(obj))
286
+ return objects
287
+ except Exception as e:
288
+ raise RuntimeError(f"Failed to list linked objects: {e}")
289
+
290
+ def _format_object(self, obj: Any) -> Dict[str, Any]:
291
+ """Format object for consistent output."""
292
+ # Objects may have various properties - extract them dynamically
293
+ result = {}
294
+ if hasattr(obj, "__dict__"):
295
+ for key, value in obj.__dict__.items():
296
+ if not key.startswith("_"):
297
+ result[key] = value
298
+ return result
299
+
300
+
301
+ class ActionService(BaseService):
302
+ """Service wrapper for action operations."""
303
+
304
+ def _get_service(self) -> Any:
305
+ """Get the Foundry ontologies service."""
306
+ return self.client.ontologies
307
+
308
+ def apply_action(
309
+ self,
310
+ ontology_rid: str,
311
+ action_type: str,
312
+ parameters: Dict[str, Any],
313
+ ) -> Dict[str, Any]:
314
+ """
315
+ Apply an action with given parameters.
316
+
317
+ Args:
318
+ ontology_rid: Ontology Resource Identifier
319
+ action_type: Action type API name
320
+ parameters: Action parameters
321
+
322
+ Returns:
323
+ Action result
324
+ """
325
+ try:
326
+ result = self.service.Action.apply(ontology_rid, action_type, parameters)
327
+ return self._format_action_result(result)
328
+ except Exception as e:
329
+ raise RuntimeError(f"Failed to apply action {action_type}: {e}")
330
+
331
+ def validate_action(
332
+ self,
333
+ ontology_rid: str,
334
+ action_type: str,
335
+ parameters: Dict[str, Any],
336
+ ) -> Dict[str, Any]:
337
+ """
338
+ Validate action parameters without executing.
339
+
340
+ Args:
341
+ ontology_rid: Ontology Resource Identifier
342
+ action_type: Action type API name
343
+ parameters: Action parameters to validate
344
+
345
+ Returns:
346
+ Validation result
347
+ """
348
+ try:
349
+ result = self.service.Action.validate(ontology_rid, action_type, parameters)
350
+ return self._format_validation_result(result)
351
+ except Exception as e:
352
+ raise RuntimeError(f"Failed to validate action {action_type}: {e}")
353
+
354
+ def apply_batch_actions(
355
+ self,
356
+ ontology_rid: str,
357
+ action_type: str,
358
+ requests: List[Dict[str, Any]],
359
+ ) -> List[Dict[str, Any]]:
360
+ """
361
+ Apply multiple actions of the same type.
362
+
363
+ Args:
364
+ ontology_rid: Ontology Resource Identifier
365
+ action_type: Action type API name
366
+ requests: List of action requests (max 20)
367
+
368
+ Returns:
369
+ List of action results
370
+ """
371
+ try:
372
+ if len(requests) > 20:
373
+ raise ValueError("Maximum 20 actions can be applied in a batch")
374
+
375
+ result = self.service.Action.apply_batch(
376
+ ontology_rid, action_type, requests
377
+ )
378
+ return [self._format_action_result(r) for r in result]
379
+ except Exception as e:
380
+ raise RuntimeError(f"Failed to apply batch actions: {e}")
381
+
382
+ def _format_action_result(self, result: Any) -> Dict[str, Any]:
383
+ """Format action result for consistent output."""
384
+ return {
385
+ "rid": getattr(result, "rid", None),
386
+ "status": getattr(result, "status", None),
387
+ "created_objects": getattr(result, "created_objects", []),
388
+ "modified_objects": getattr(result, "modified_objects", []),
389
+ "deleted_objects": getattr(result, "deleted_objects", []),
390
+ }
391
+
392
+ def _format_validation_result(self, result: Any) -> Dict[str, Any]:
393
+ """Format validation result for consistent output."""
394
+ return {
395
+ "valid": getattr(result, "valid", False),
396
+ "errors": getattr(result, "errors", []),
397
+ "warnings": getattr(result, "warnings", []),
398
+ }
399
+
400
+
401
+ class QueryService(BaseService):
402
+ """Service wrapper for query operations."""
403
+
404
+ def _get_service(self) -> Any:
405
+ """Get the Foundry ontologies service."""
406
+ return self.client.ontologies
407
+
408
+ def execute_query(
409
+ self,
410
+ ontology_rid: str,
411
+ query_api_name: str,
412
+ parameters: Optional[Dict[str, Any]] = None,
413
+ ) -> Dict[str, Any]:
414
+ """
415
+ Execute a predefined query.
416
+
417
+ Args:
418
+ ontology_rid: Ontology Resource Identifier
419
+ query_api_name: Query API name
420
+ parameters: Query parameters
421
+
422
+ Returns:
423
+ Query results
424
+ """
425
+ try:
426
+ result = self.service.Query.execute(
427
+ ontology_rid, query_api_name, parameters=parameters or {}
428
+ )
429
+ return self._format_query_result(result)
430
+ except Exception as e:
431
+ raise RuntimeError(f"Failed to execute query {query_api_name}: {e}")
432
+
433
+ def _format_query_result(self, result: Any) -> Dict[str, Any]:
434
+ """Format query result for consistent output."""
435
+ # Query results can vary widely - extract what we can
436
+ if hasattr(result, "rows"):
437
+ return {"rows": result.rows, "columns": getattr(result, "columns", [])}
438
+ elif hasattr(result, "objects"):
439
+ return {"objects": result.objects}
440
+ else:
441
+ # Return as dict if possible
442
+ return result if isinstance(result, dict) else {"result": str(result)}