1
1
# type: ignore
2
2
from datetime import datetime , timezone
3
3
import json
4
- from labelbox . schema . iam_integration import IAMIntegration
4
+
5
5
import logging
6
6
import mimetypes
7
7
import os
10
10
import requests
11
11
import requests .exceptions
12
12
13
- from labelbox import utils
14
13
import labelbox .exceptions
14
+ from labelbox import utils
15
+ from labelbox import __version__ as SDK_VERSION
15
16
from labelbox .orm import query
16
17
from labelbox .orm .db_object import DbObject
17
18
from labelbox .pagination import PaginatedCollection
23
24
from labelbox .schema .organization import Organization
24
25
from labelbox .schema .data_row_metadata import DataRowMetadataOntology
25
26
from labelbox .schema .labeling_frontend import LabelingFrontend
27
+ from labelbox .schema .iam_integration import IAMIntegration
26
28
from labelbox .schema import role
27
- from labelbox import __version__ as SDK_VERSION
28
29
29
30
logger = logging .getLogger (__name__ )
30
31
@@ -504,15 +505,18 @@ def _create(self, db_object_type, data):
504
505
res = res ["create%s" % db_object_type .type_name ()]
505
506
return db_object_type (self , res )
506
507
507
- def create_dataset (self , ** kwargs ):
508
+ def create_dataset (self , iam_integration = IAMIntegration . _DEFAULT , ** kwargs ):
508
509
""" Creates a Dataset object on the server.
510
+ This will attempt to connect the organization's default IAM integration if it exists
509
511
510
512
Attribute values are passed as keyword arguments.
511
513
512
514
>>> project = client.get_project("<project_uid>")
513
515
>>> dataset = client.create_dataset(name="<dataset_name>", projects=project)
514
516
515
517
Args:
518
+ iam_integration (IAMIntegration) : Uses the default integration.
519
+ Optionally specify another integration or set as None to not use delegated access
516
520
**kwargs: Keyword arguments with Dataset attribute values.
517
521
Returns:
518
522
A new Dataset object.
@@ -521,31 +525,41 @@ def create_dataset(self, **kwargs):
521
525
any of the attribute names given in kwargs.
522
526
"""
523
527
dataset = self ._create (Dataset , kwargs )
524
- iam_integration = kwargs .get ('iam_integration' ) or self .get_organization ().get_default_iam_integration ()
525
- if iam_integration is not None :
526
- if not isinstance (iam_integration , IAMIntegration ):
527
- raise TypeError (f"iam integration must be a reference an `IAMIntegration` object. Found { type (iam_integration )} " )
528
-
529
- if not iam_integration .valid :
530
- raise ValueError ("Invalid integration is invalid. Please select another integration or remove default." )
531
- try :
532
- self .execute ("""
533
- mutation setSignerForDatasetPyApi($signerId: ID!, $datasetId: ID!) {
534
- setSignerForDataset(data: { signerId: $signerId}, where: {id: $datasetId}){id}}
535
- """ , {'signerId' : iam_integration .uid , 'datasetId' : dataset .uid })
536
- validation_result = self .execute ("""
537
- mutation validateDatasetPyApi($id: ID!){validateDataset(where: {id : $id}){
538
- valid checks{name, success}}}
539
- """ , {'id' : dataset .uid })
540
- if not validation_result ['validateDataset' ]['checks' ][0 ]['success' ]:
541
- raise labelbox .exceptions .LabelboxError (
542
- f"IAMIntegration { validation_result ['validateDataset' ]['checks' ]['name' ]} was not successfully added added to the project."
543
- )
544
- except Exception as e :
545
- dataset .delete ()
546
- raise e
547
- return dataset
548
528
529
+ if iam_integration == IAMIntegration ._DEFAULT :
530
+ iam_integration = self .get_organization (
531
+ ).get_default_iam_integration ()
532
+
533
+ if iam_integration is None :
534
+ return dataset
535
+
536
+ if not isinstance (iam_integration , IAMIntegration ):
537
+ raise TypeError (
538
+ f"iam integration must be a reference an `IAMIntegration` object. Found { type (iam_integration )} "
539
+ )
540
+
541
+ if not iam_integration .valid :
542
+ raise ValueError ("Integration is not valid. Please select another." )
543
+ try :
544
+ self .execute (
545
+ """mutation setSignerForDatasetPyApi($signerId: ID!, $datasetId: ID!) {
546
+ setSignerForDataset(data: { signerId: $signerId}, where: {id: $datasetId}){id}}
547
+ """ , {
548
+ 'signerId' : iam_integration .uid ,
549
+ 'datasetId' : dataset .uid
550
+ })
551
+ validation_result = self .execute (
552
+ """mutation validateDatasetPyApi($id: ID!){validateDataset(where: {id : $id}){
553
+ valid checks{name, success}}}
554
+ """ , {'id' : dataset .uid })
555
+ if not validation_result ['validateDataset' ]['checks' ][0 ]['success' ]:
556
+ raise labelbox .exceptions .LabelboxError (
557
+ f"IAMIntegration { validation_result ['validateDataset' ]['checks' ]['name' ]} was not successfully added added to the project."
558
+ )
559
+ except Exception as e :
560
+ dataset .delete ()
561
+ raise e
562
+ return dataset
549
563
550
564
def create_project (self , ** kwargs ):
551
565
""" Creates a Project object on the server.
@@ -643,5 +657,3 @@ def create_model(self, name, ontology_id):
643
657
"ontologyId" : ontology_id
644
658
})
645
659
return Model (self , result ['createModel' ])
646
-
647
-
0 commit comments