pltr-cli 0.4.0__py3-none-any.whl → 0.5.1__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,563 @@ 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 create_transaction(
311
+ self, dataset_rid: str, branch: str = "master", transaction_type: str = "APPEND"
312
+ ) -> Dict[str, Any]:
313
+ """
314
+ Create a new transaction for a dataset.
315
+
316
+ Args:
317
+ dataset_rid: Dataset Resource Identifier
318
+ branch: Dataset branch name
319
+ transaction_type: Transaction type (APPEND, UPDATE, SNAPSHOT, DELETE)
320
+
321
+ Returns:
322
+ Transaction information dictionary
323
+ """
324
+ try:
325
+ # Try to use the v2 API's Dataset.create_transaction method
326
+ transaction = self.service.Dataset.create_transaction(
327
+ dataset_rid=dataset_rid,
328
+ branch=branch,
329
+ transaction_type=transaction_type,
330
+ )
331
+
332
+ return {
333
+ "transaction_rid": getattr(transaction, "rid", None),
334
+ "dataset_rid": dataset_rid,
335
+ "branch": branch,
336
+ "transaction_type": transaction_type,
337
+ "status": getattr(transaction, "status", "OPEN"),
338
+ "created_time": getattr(transaction, "created_time", None),
339
+ "created_by": getattr(transaction, "created_by", None),
340
+ }
341
+ except AttributeError:
342
+ # Fallback to service.create_transaction if available
343
+ try:
344
+ transaction = self.service.create_transaction(
345
+ dataset_rid=dataset_rid,
346
+ branch=branch,
347
+ transaction_type=transaction_type,
348
+ )
349
+ return {
350
+ "transaction_rid": getattr(transaction, "rid", None),
351
+ "dataset_rid": dataset_rid,
352
+ "branch": branch,
353
+ "transaction_type": transaction_type,
354
+ "status": getattr(transaction, "status", "OPEN"),
355
+ "created_time": getattr(transaction, "created_time", None),
356
+ "created_by": getattr(transaction, "created_by", None),
357
+ }
358
+ except Exception as e:
359
+ raise RuntimeError(
360
+ f"Failed to create transaction for dataset {dataset_rid}: {e}"
361
+ )
362
+ except Exception as e:
363
+ raise RuntimeError(
364
+ f"Failed to create transaction for dataset {dataset_rid}: {e}"
365
+ )
366
+
367
+ def commit_transaction(
368
+ self, dataset_rid: str, transaction_rid: str
369
+ ) -> Dict[str, Any]:
370
+ """
371
+ Commit an open transaction.
372
+
373
+ Args:
374
+ dataset_rid: Dataset Resource Identifier
375
+ transaction_rid: Transaction Resource Identifier
376
+
377
+ Returns:
378
+ Transaction commit result
379
+ """
380
+ try:
381
+ # Try the v2 API first (Dataset.commit_transaction)
382
+ self.service.Dataset.commit_transaction(
383
+ dataset_rid=dataset_rid, transaction_rid=transaction_rid
384
+ )
385
+
386
+ return {
387
+ "transaction_rid": transaction_rid,
388
+ "dataset_rid": dataset_rid,
389
+ "status": "COMMITTED",
390
+ "committed_time": None, # Would need to get this from a status call
391
+ "success": True,
392
+ }
393
+ except AttributeError:
394
+ # Fallback to service.commit_transaction
395
+ try:
396
+ self.service.commit_transaction(
397
+ dataset_rid=dataset_rid, transaction_rid=transaction_rid
398
+ )
399
+ return {
400
+ "transaction_rid": transaction_rid,
401
+ "dataset_rid": dataset_rid,
402
+ "status": "COMMITTED",
403
+ "committed_time": None,
404
+ "success": True,
405
+ }
406
+ except Exception as e:
407
+ raise RuntimeError(
408
+ f"Failed to commit transaction {transaction_rid}: {e}"
409
+ )
410
+ except Exception as e:
411
+ raise RuntimeError(f"Failed to commit transaction {transaction_rid}: {e}")
412
+
413
+ def abort_transaction(
414
+ self, dataset_rid: str, transaction_rid: str
415
+ ) -> Dict[str, Any]:
416
+ """
417
+ Abort an open transaction.
418
+
419
+ Args:
420
+ dataset_rid: Dataset Resource Identifier
421
+ transaction_rid: Transaction Resource Identifier
422
+
423
+ Returns:
424
+ Transaction abort result
425
+ """
426
+ try:
427
+ # Try the v2 API first (Dataset.abort_transaction)
428
+ self.service.Dataset.abort_transaction(
429
+ dataset_rid=dataset_rid, transaction_rid=transaction_rid
430
+ )
431
+
432
+ return {
433
+ "transaction_rid": transaction_rid,
434
+ "dataset_rid": dataset_rid,
435
+ "status": "ABORTED",
436
+ "aborted_time": None, # Would need to get this from a status call
437
+ "success": True,
438
+ }
439
+ except AttributeError:
440
+ # Fallback to service.abort_transaction
441
+ try:
442
+ self.service.abort_transaction(
443
+ dataset_rid=dataset_rid, transaction_rid=transaction_rid
444
+ )
445
+ return {
446
+ "transaction_rid": transaction_rid,
447
+ "dataset_rid": dataset_rid,
448
+ "status": "ABORTED",
449
+ "aborted_time": None,
450
+ "success": True,
451
+ }
452
+ except Exception as e:
453
+ raise RuntimeError(
454
+ f"Failed to abort transaction {transaction_rid}: {e}"
455
+ )
456
+ except Exception as e:
457
+ raise RuntimeError(f"Failed to abort transaction {transaction_rid}: {e}")
458
+
459
+ def get_transaction_status(
460
+ self, dataset_rid: str, transaction_rid: str
461
+ ) -> Dict[str, Any]:
462
+ """
463
+ Get the status of a specific transaction.
464
+
465
+ Args:
466
+ dataset_rid: Dataset Resource Identifier
467
+ transaction_rid: Transaction Resource Identifier
468
+
469
+ Returns:
470
+ Transaction status information
471
+ """
472
+ try:
473
+ # Try the v2 API first (Dataset.get_transaction)
474
+ transaction = self.service.Dataset.get_transaction(
475
+ dataset_rid=dataset_rid, transaction_rid=transaction_rid
476
+ )
477
+
478
+ return {
479
+ "transaction_rid": transaction_rid,
480
+ "dataset_rid": dataset_rid,
481
+ "status": getattr(transaction, "status", None),
482
+ "transaction_type": getattr(transaction, "transaction_type", None),
483
+ "branch": getattr(transaction, "branch", None),
484
+ "created_time": getattr(transaction, "created_time", None),
485
+ "created_by": getattr(transaction, "created_by", None),
486
+ "committed_time": getattr(transaction, "committed_time", None),
487
+ "aborted_time": getattr(transaction, "aborted_time", None),
488
+ }
489
+ except AttributeError:
490
+ # Fallback to service.get_transaction
491
+ try:
492
+ transaction = self.service.get_transaction(
493
+ dataset_rid=dataset_rid, transaction_rid=transaction_rid
494
+ )
495
+ return {
496
+ "transaction_rid": transaction_rid,
497
+ "dataset_rid": dataset_rid,
498
+ "status": getattr(transaction, "status", None),
499
+ "transaction_type": getattr(transaction, "transaction_type", None),
500
+ "branch": getattr(transaction, "branch", None),
501
+ "created_time": getattr(transaction, "created_time", None),
502
+ "created_by": getattr(transaction, "created_by", None),
503
+ "committed_time": getattr(transaction, "committed_time", None),
504
+ "aborted_time": getattr(transaction, "aborted_time", None),
505
+ }
506
+ except Exception as e:
507
+ raise RuntimeError(
508
+ f"Failed to get transaction status {transaction_rid}: {e}"
509
+ )
510
+ except Exception as e:
511
+ raise RuntimeError(
512
+ f"Failed to get transaction status {transaction_rid}: {e}"
513
+ )
514
+
515
+ def get_transactions(
516
+ self, dataset_rid: str, branch: str = "master"
517
+ ) -> List[Dict[str, Any]]:
518
+ """
519
+ Get list of transactions for a dataset branch.
520
+
521
+ Args:
522
+ dataset_rid: Dataset Resource Identifier
523
+ branch: Dataset branch name
524
+
525
+ Returns:
526
+ List of transaction information dictionaries
527
+ """
528
+ try:
529
+ # Try the v2 API first (Dataset.list_transactions)
530
+ transactions = self.service.Dataset.list_transactions(
531
+ dataset_rid=dataset_rid, branch=branch
532
+ )
533
+
534
+ return [
535
+ {
536
+ "transaction_rid": getattr(transaction, "rid", None),
537
+ "status": getattr(transaction, "status", None),
538
+ "transaction_type": getattr(transaction, "transaction_type", None),
539
+ "branch": getattr(transaction, "branch", branch),
540
+ "created_time": getattr(transaction, "created_time", None),
541
+ "created_by": getattr(transaction, "created_by", None),
542
+ "committed_time": getattr(transaction, "committed_time", None),
543
+ "aborted_time": getattr(transaction, "aborted_time", None),
544
+ }
545
+ for transaction in transactions
546
+ ]
547
+ except AttributeError:
548
+ # Fallback to service.list_transactions
549
+ try:
550
+ transactions = self.service.list_transactions(
551
+ dataset_rid=dataset_rid, branch=branch
552
+ )
553
+
554
+ return [
555
+ {
556
+ "transaction_rid": getattr(transaction, "rid", None),
557
+ "created_time": getattr(transaction, "created_time", None),
558
+ "created_by": getattr(transaction, "created_by", None),
559
+ "status": getattr(transaction, "status", None),
560
+ "transaction_type": getattr(
561
+ transaction, "transaction_type", None
562
+ ),
563
+ "branch": getattr(transaction, "branch", branch),
564
+ "committed_time": getattr(transaction, "committed_time", None),
565
+ "aborted_time": getattr(transaction, "aborted_time", None),
566
+ }
567
+ for transaction in transactions
568
+ ]
569
+ except AttributeError:
570
+ # Method not available in this SDK version
571
+ raise NotImplementedError(
572
+ "Transaction listing is not supported by this SDK version. "
573
+ "This feature may require a newer version of foundry-platform-python."
574
+ )
575
+ except Exception as e:
576
+ raise RuntimeError(
577
+ f"Failed to get transactions for dataset {dataset_rid}: {e}"
578
+ )
579
+ except Exception as e:
580
+ raise RuntimeError(
581
+ f"Failed to get transactions for dataset {dataset_rid}: {e}"
582
+ )
583
+
584
+ def get_views(self, dataset_rid: str) -> List[Dict[str, Any]]:
585
+ """
586
+ Get list of views for a dataset.
587
+
588
+ Args:
589
+ dataset_rid: Dataset Resource Identifier
590
+
591
+ Returns:
592
+ List of view information dictionaries
593
+ """
594
+ try:
595
+ # Note: This method may not be available in all SDK versions
596
+ views = self.service.list_views(dataset_rid=dataset_rid)
597
+
598
+ return [
599
+ {
600
+ "view_rid": getattr(view, "rid", None),
601
+ "name": getattr(view, "name", None),
602
+ "description": getattr(view, "description", None),
603
+ "created_time": getattr(view, "created_time", None),
604
+ "created_by": getattr(view, "created_by", None),
605
+ }
606
+ for view in views
607
+ ]
608
+ except AttributeError:
609
+ # Method not available in this SDK version
610
+ raise NotImplementedError(
611
+ "View listing is not supported by this SDK version. "
612
+ "This feature may require a newer version of foundry-platform-python."
613
+ )
614
+ except Exception as e:
615
+ raise RuntimeError(f"Failed to get views for dataset {dataset_rid}: {e}")
616
+
617
+ def create_view(
618
+ self, dataset_rid: str, view_name: str, description: Optional[str] = None
619
+ ) -> Dict[str, Any]:
620
+ """
621
+ Create a new view for a dataset.
622
+
623
+ Args:
624
+ dataset_rid: Dataset Resource Identifier
625
+ view_name: Name for the new view
626
+ description: Optional description for the view
627
+
628
+ Returns:
629
+ Created view information
630
+ """
631
+ try:
632
+ # Note: This method may not be available in all SDK versions
633
+ view = self.service.create_view(
634
+ dataset_rid=dataset_rid,
635
+ name=view_name,
636
+ description=description,
637
+ )
638
+
639
+ return {
640
+ "view_rid": getattr(view, "rid", None),
641
+ "name": view_name,
642
+ "description": description,
643
+ "dataset_rid": dataset_rid,
644
+ "created_time": getattr(view, "created_time", None),
645
+ "created_by": getattr(view, "created_by", None),
646
+ }
647
+ except AttributeError:
648
+ # Method not available in this SDK version
649
+ raise NotImplementedError(
650
+ "View creation is not supported by this SDK version. "
651
+ "This feature may require a newer version of foundry-platform-python."
652
+ )
653
+ except Exception as e:
654
+ raise RuntimeError(
655
+ f"Failed to create view '{view_name}' for dataset {dataset_rid}: {e}"
656
+ )
657
+
75
658
  def _format_dataset_info(self, dataset: Any) -> Dict[str, Any]:
76
659
  """
77
660
  Format dataset information for consistent output.
@@ -82,9 +665,19 @@ class DatasetService(BaseService):
82
665
  Returns:
83
666
  Formatted dataset information dictionary
84
667
  """
85
- # The v2 Dataset object only has rid, name, and parent_folder_rid
668
+ # The v2 Dataset object has different attributes
86
669
  return {
87
- "rid": dataset.rid,
88
- "name": dataset.name,
89
- "parent_folder_rid": dataset.parent_folder_rid,
670
+ "rid": getattr(dataset, "rid", "unknown"),
671
+ "name": getattr(dataset, "name", "Unknown"),
672
+ "description": getattr(dataset, "description", ""),
673
+ "path": getattr(dataset, "path", None),
674
+ "created": getattr(dataset, "created", None),
675
+ "modified": getattr(dataset, "modified", None),
676
+ # Try to get additional attributes that might exist
677
+ "created_time": getattr(dataset, "created_time", None),
678
+ "created_by": getattr(dataset, "created_by", None),
679
+ "last_modified": getattr(dataset, "last_modified", None),
680
+ "size_bytes": getattr(dataset, "size_bytes", None),
681
+ "schema_id": getattr(dataset, "schema_id", None),
682
+ "parent_folder_rid": getattr(dataset, "parent_folder_rid", None),
90
683
  }