pltr-cli 0.4.0__py3-none-any.whl → 0.5.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.
pltr/services/dataset.py CHANGED
@@ -1,20 +1,22 @@
1
1
  """
2
- Dataset service wrapper for Foundry SDK v2 API.
3
- Only includes operations that are actually supported by foundry-platform-sdk v1.27.0.
2
+ Dataset service wrapper for Foundry SDK.
4
3
  """
5
4
 
6
- from typing import Any, Optional, Dict
5
+ from typing import Any, Optional, List, Dict, Union
6
+ from pathlib import Path
7
7
 
8
8
  from .base import BaseService
9
9
 
10
10
 
11
11
  class DatasetService(BaseService):
12
- """Service wrapper for Foundry dataset operations using v2 API."""
12
+ """Service wrapper for Foundry dataset operations."""
13
13
 
14
14
  def _get_service(self) -> Any:
15
15
  """Get the Foundry datasets service."""
16
16
  return self.client.datasets
17
17
 
18
+ # list_datasets method removed - not supported by foundry-platform-sdk v1.27.0
19
+
18
20
  def get_dataset(self, dataset_rid: str) -> Dict[str, Any]:
19
21
  """
20
22
  Get information about a specific dataset.
@@ -32,7 +34,26 @@ class DatasetService(BaseService):
32
34
  except Exception as e:
33
35
  raise RuntimeError(f"Failed to get dataset {dataset_rid}: {e}")
34
36
 
35
- # get_schema method removed - uses preview-only API
37
+ def get_schema(self, dataset_rid: str) -> Dict[str, Any]:
38
+ """
39
+ Get dataset schema.
40
+
41
+ Args:
42
+ dataset_rid: Dataset Resource Identifier
43
+
44
+ Returns:
45
+ Schema information
46
+ """
47
+ try:
48
+ schema = self.service.Dataset.get_schema(dataset_rid)
49
+ return {
50
+ "dataset_rid": dataset_rid,
51
+ "schema": schema,
52
+ "type": str(type(schema)),
53
+ "status": "Schema retrieved successfully",
54
+ }
55
+ except Exception as e:
56
+ raise RuntimeError(f"Failed to get schema for dataset {dataset_rid}: {e}")
36
57
 
37
58
  def create_dataset(
38
59
  self, name: str, parent_folder_rid: Optional[str] = None
@@ -48,11 +69,20 @@ class DatasetService(BaseService):
48
69
  Created dataset information
49
70
  """
50
71
  try:
51
- # The create method parameters depend on the SDK version
72
+ # Try the v2 API first (Dataset.create)
52
73
  dataset = self.service.Dataset.create(
53
74
  name=name, parent_folder_rid=parent_folder_rid
54
75
  )
55
76
  return self._format_dataset_info(dataset)
77
+ except AttributeError:
78
+ # Fallback to service.create_dataset
79
+ try:
80
+ dataset = self.service.create_dataset(
81
+ name=name, parent_folder_rid=parent_folder_rid
82
+ )
83
+ return self._format_dataset_info(dataset)
84
+ except Exception as e:
85
+ raise RuntimeError(f"Failed to create dataset '{name}': {e}")
56
86
  except Exception as e:
57
87
  raise RuntimeError(f"Failed to create dataset '{name}': {e}")
58
88
 
@@ -68,10 +98,328 @@ class DatasetService(BaseService):
68
98
  Table data in specified format
69
99
  """
70
100
  try:
101
+ # Try the v2 API first (Dataset.read_table)
71
102
  return self.service.Dataset.read_table(dataset_rid, format=format)
103
+ except AttributeError:
104
+ # Fallback to service.read_table
105
+ try:
106
+ return self.service.read_table(dataset_rid, format=format)
107
+ except Exception as e:
108
+ raise RuntimeError(f"Failed to read dataset {dataset_rid}: {e}")
72
109
  except Exception as e:
73
110
  raise RuntimeError(f"Failed to read dataset {dataset_rid}: {e}")
74
111
 
112
+ def delete_dataset(self, dataset_rid: str) -> bool:
113
+ """
114
+ Delete a dataset.
115
+
116
+ Args:
117
+ dataset_rid: Dataset Resource Identifier
118
+
119
+ Returns:
120
+ True if deletion was successful
121
+ """
122
+ try:
123
+ self.service.delete_dataset(dataset_rid)
124
+ return True
125
+ except Exception as e:
126
+ raise RuntimeError(f"Failed to delete dataset {dataset_rid}: {e}")
127
+
128
+ def upload_file(
129
+ self,
130
+ dataset_rid: str,
131
+ file_path: Union[str, Path],
132
+ branch: str = "master",
133
+ transaction_rid: Optional[str] = None,
134
+ ) -> Dict[str, Any]:
135
+ """
136
+ Upload a file to a dataset.
137
+
138
+ Args:
139
+ dataset_rid: Dataset Resource Identifier
140
+ file_path: Path to file to upload
141
+ branch: Dataset branch name
142
+ transaction_rid: Transaction RID (optional)
143
+
144
+ Returns:
145
+ Upload result information
146
+ """
147
+ file_path = Path(file_path)
148
+ if not file_path.exists():
149
+ raise FileNotFoundError(f"File not found: {file_path}")
150
+
151
+ try:
152
+ with open(file_path, "rb") as f:
153
+ result = self.service.upload_file(
154
+ dataset_rid=dataset_rid,
155
+ file_path=file_path.name,
156
+ file_data=f,
157
+ branch=branch,
158
+ transaction_rid=transaction_rid,
159
+ )
160
+
161
+ return {
162
+ "dataset_rid": dataset_rid,
163
+ "file_path": str(file_path),
164
+ "branch": branch,
165
+ "size_bytes": file_path.stat().st_size,
166
+ "uploaded": True,
167
+ "transaction_rid": getattr(result, "transaction_rid", transaction_rid),
168
+ }
169
+ except Exception as e:
170
+ raise RuntimeError(
171
+ f"Failed to upload file {file_path} to dataset {dataset_rid}: {e}"
172
+ )
173
+
174
+ def download_file(
175
+ self,
176
+ dataset_rid: str,
177
+ file_path: str,
178
+ output_path: Union[str, Path],
179
+ branch: str = "master",
180
+ ) -> Dict[str, Any]:
181
+ """
182
+ Download a file from a dataset.
183
+
184
+ Args:
185
+ dataset_rid: Dataset Resource Identifier
186
+ file_path: Path of file within dataset
187
+ output_path: Local path to save the downloaded file
188
+ branch: Dataset branch name
189
+
190
+ Returns:
191
+ Download result information
192
+ """
193
+ output_path = Path(output_path)
194
+
195
+ try:
196
+ # Ensure output directory exists
197
+ output_path.parent.mkdir(parents=True, exist_ok=True)
198
+
199
+ file_content = self.service.download_file(
200
+ dataset_rid=dataset_rid, file_path=file_path, branch=branch
201
+ )
202
+
203
+ # Write file content to disk
204
+ with open(output_path, "wb") as f:
205
+ if hasattr(file_content, "read"):
206
+ # If it's a stream
207
+ f.write(file_content.read())
208
+ else:
209
+ # If it's bytes
210
+ f.write(file_content)
211
+
212
+ return {
213
+ "dataset_rid": dataset_rid,
214
+ "file_path": file_path,
215
+ "output_path": str(output_path),
216
+ "branch": branch,
217
+ "size_bytes": output_path.stat().st_size,
218
+ "downloaded": True,
219
+ }
220
+ except Exception as e:
221
+ raise RuntimeError(
222
+ f"Failed to download file {file_path} from dataset {dataset_rid}: {e}"
223
+ )
224
+
225
+ def list_files(
226
+ self, dataset_rid: str, branch: str = "master"
227
+ ) -> List[Dict[str, Any]]:
228
+ """
229
+ List files in a dataset.
230
+
231
+ Args:
232
+ dataset_rid: Dataset Resource Identifier
233
+ branch: Dataset branch name
234
+
235
+ Returns:
236
+ List of file information dictionaries
237
+ """
238
+ try:
239
+ files = self.service.list_files(dataset_rid=dataset_rid, branch=branch)
240
+
241
+ return [
242
+ {
243
+ "path": file.path,
244
+ "size_bytes": getattr(file, "size_bytes", None),
245
+ "last_modified": getattr(file, "last_modified", None),
246
+ "transaction_rid": getattr(file, "transaction_rid", None),
247
+ }
248
+ for file in files
249
+ ]
250
+ except Exception as e:
251
+ raise RuntimeError(f"Failed to list files in dataset {dataset_rid}: {e}")
252
+
253
+ def get_branches(self, dataset_rid: str) -> List[Dict[str, Any]]:
254
+ """
255
+ Get list of branches for a dataset.
256
+
257
+ Args:
258
+ dataset_rid: Dataset Resource Identifier
259
+
260
+ Returns:
261
+ List of branch information dictionaries
262
+ """
263
+ try:
264
+ branches = self.service.list_branches(dataset_rid=dataset_rid)
265
+
266
+ return [
267
+ {
268
+ "name": branch.name,
269
+ "transaction_rid": getattr(branch, "transaction_rid", None),
270
+ "created_time": getattr(branch, "created_time", None),
271
+ "created_by": getattr(branch, "created_by", None),
272
+ }
273
+ for branch in branches
274
+ ]
275
+ except Exception as e:
276
+ raise RuntimeError(f"Failed to get branches for dataset {dataset_rid}: {e}")
277
+
278
+ def create_branch(
279
+ self, dataset_rid: str, branch_name: str, parent_branch: str = "master"
280
+ ) -> Dict[str, Any]:
281
+ """
282
+ Create a new branch for a dataset.
283
+
284
+ Args:
285
+ dataset_rid: Dataset Resource Identifier
286
+ branch_name: Name for the new branch
287
+ parent_branch: Parent branch to branch from
288
+
289
+ Returns:
290
+ Created branch information
291
+ """
292
+ try:
293
+ branch = self.service.create_branch(
294
+ dataset_rid=dataset_rid,
295
+ branch_name=branch_name,
296
+ parent_branch=parent_branch,
297
+ )
298
+
299
+ return {
300
+ "name": branch.name,
301
+ "dataset_rid": dataset_rid,
302
+ "parent_branch": parent_branch,
303
+ "transaction_rid": getattr(branch, "transaction_rid", None),
304
+ }
305
+ except Exception as e:
306
+ raise RuntimeError(
307
+ f"Failed to create branch '{branch_name}' for dataset {dataset_rid}: {e}"
308
+ )
309
+
310
+ def get_transactions(
311
+ self, dataset_rid: str, branch: str = "master"
312
+ ) -> List[Dict[str, Any]]:
313
+ """
314
+ Get list of transactions for a dataset branch.
315
+
316
+ Args:
317
+ dataset_rid: Dataset Resource Identifier
318
+ branch: Dataset branch name
319
+
320
+ Returns:
321
+ List of transaction information dictionaries
322
+ """
323
+ try:
324
+ # Note: This method may not be available in all SDK versions
325
+ transactions = self.service.list_transactions(
326
+ dataset_rid=dataset_rid, branch=branch
327
+ )
328
+
329
+ return [
330
+ {
331
+ "transaction_rid": getattr(transaction, "rid", None),
332
+ "created_time": getattr(transaction, "created_time", None),
333
+ "created_by": getattr(transaction, "created_by", None),
334
+ "status": getattr(transaction, "status", None),
335
+ }
336
+ for transaction in transactions
337
+ ]
338
+ except AttributeError:
339
+ # Method not available in this SDK version
340
+ raise NotImplementedError(
341
+ "Transaction listing is not supported by this SDK version. "
342
+ "This feature may require a newer version of foundry-platform-python."
343
+ )
344
+ except Exception as e:
345
+ raise RuntimeError(
346
+ f"Failed to get transactions for dataset {dataset_rid}: {e}"
347
+ )
348
+
349
+ def get_views(self, dataset_rid: str) -> List[Dict[str, Any]]:
350
+ """
351
+ Get list of views for a dataset.
352
+
353
+ Args:
354
+ dataset_rid: Dataset Resource Identifier
355
+
356
+ Returns:
357
+ List of view information dictionaries
358
+ """
359
+ try:
360
+ # Note: This method may not be available in all SDK versions
361
+ views = self.service.list_views(dataset_rid=dataset_rid)
362
+
363
+ return [
364
+ {
365
+ "view_rid": getattr(view, "rid", None),
366
+ "name": getattr(view, "name", None),
367
+ "description": getattr(view, "description", None),
368
+ "created_time": getattr(view, "created_time", None),
369
+ "created_by": getattr(view, "created_by", None),
370
+ }
371
+ for view in views
372
+ ]
373
+ except AttributeError:
374
+ # Method not available in this SDK version
375
+ raise NotImplementedError(
376
+ "View listing is not supported by this SDK version. "
377
+ "This feature may require a newer version of foundry-platform-python."
378
+ )
379
+ except Exception as e:
380
+ raise RuntimeError(f"Failed to get views for dataset {dataset_rid}: {e}")
381
+
382
+ def create_view(
383
+ self, dataset_rid: str, view_name: str, description: Optional[str] = None
384
+ ) -> Dict[str, Any]:
385
+ """
386
+ Create a new view for a dataset.
387
+
388
+ Args:
389
+ dataset_rid: Dataset Resource Identifier
390
+ view_name: Name for the new view
391
+ description: Optional description for the view
392
+
393
+ Returns:
394
+ Created view information
395
+ """
396
+ try:
397
+ # Note: This method may not be available in all SDK versions
398
+ view = self.service.create_view(
399
+ dataset_rid=dataset_rid,
400
+ name=view_name,
401
+ description=description,
402
+ )
403
+
404
+ return {
405
+ "view_rid": getattr(view, "rid", None),
406
+ "name": view_name,
407
+ "description": description,
408
+ "dataset_rid": dataset_rid,
409
+ "created_time": getattr(view, "created_time", None),
410
+ "created_by": getattr(view, "created_by", None),
411
+ }
412
+ except AttributeError:
413
+ # Method not available in this SDK version
414
+ raise NotImplementedError(
415
+ "View creation is not supported by this SDK version. "
416
+ "This feature may require a newer version of foundry-platform-python."
417
+ )
418
+ except Exception as e:
419
+ raise RuntimeError(
420
+ f"Failed to create view '{view_name}' for dataset {dataset_rid}: {e}"
421
+ )
422
+
75
423
  def _format_dataset_info(self, dataset: Any) -> Dict[str, Any]:
76
424
  """
77
425
  Format dataset information for consistent output.
@@ -82,9 +430,19 @@ class DatasetService(BaseService):
82
430
  Returns:
83
431
  Formatted dataset information dictionary
84
432
  """
85
- # The v2 Dataset object only has rid, name, and parent_folder_rid
433
+ # The v2 Dataset object has different attributes
86
434
  return {
87
- "rid": dataset.rid,
88
- "name": dataset.name,
89
- "parent_folder_rid": dataset.parent_folder_rid,
435
+ "rid": getattr(dataset, "rid", "unknown"),
436
+ "name": getattr(dataset, "name", "Unknown"),
437
+ "description": getattr(dataset, "description", ""),
438
+ "path": getattr(dataset, "path", None),
439
+ "created": getattr(dataset, "created", None),
440
+ "modified": getattr(dataset, "modified", None),
441
+ # Try to get additional attributes that might exist
442
+ "created_time": getattr(dataset, "created_time", None),
443
+ "created_by": getattr(dataset, "created_by", None),
444
+ "last_modified": getattr(dataset, "last_modified", None),
445
+ "size_bytes": getattr(dataset, "size_bytes", None),
446
+ "schema_id": getattr(dataset, "schema_id", None),
447
+ "parent_folder_rid": getattr(dataset, "parent_folder_rid", None),
90
448
  }