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