@@ -196,6 +196,7 @@ def check_errors(keywords, *path):
196196 return None
197197
198198 def get_error_status_code (error ):
199+ print (error )
199200 return error ["extensions" ]["exception" ].get ("status" )
200201
201202 if check_errors (["AUTHENTICATION_ERROR" ], "extensions" ,
@@ -409,7 +410,7 @@ def get_project(self, project_id):
409410 labelbox.exceptions.ResourceNotFoundError: If there is no
410411 Project with the given ID.
411412 """
412- return self ._get_single (Project , project_id )
413+ return self ._get_single (Entity . Project , project_id )
413414
414415 def get_dataset (self , dataset_id ):
415416 """ Gets a single Dataset with the given ID.
@@ -720,7 +721,62 @@ def get_ontologies(self, name_contains: str):
720721 res = PaginatedCollection (
721722 self , query_str , {'search' : name_contains , 'filter' :{'status' : 'ALL' }}, ['ontologies' , 'nodes' ],
722723 Entity .Ontology , ['ontologies' , 'nextCursor' ])
724+ # status can be ALL or UNUSED
723725 return res
724726
727+ def get_root_schema (self , root_schema_id ):
728+ return self ._get_single (Entity .RootSchemaNode , root_schema_id )
729+
730+
731+ def get_root_schemas (self , name_contains ):
732+ query_str = """query getRootSchemaNodePyApi($search: String, $filter: RootSchemaNodeFilter, $from : String, $first: PageSize){
733+ rootSchemaNodes(where: {filter: $filter, search: $search}, after: $from, first: $first){
734+ nodes {%s}
735+ nextCursor
736+ }
737+ }
738+ """ % query .results_query_part (Entity .RootSchemaNode )
739+ return PaginatedCollection (
740+ self , query_str , {'search' : name_contains , 'filter' :{'status' : 'ALL' }}, ['rootSchemaNodes' , 'nodes' ],
741+ Entity .RootSchema , ['rootSchemaNodes' , 'nextCursor' ])
742+
743+ # TODO: Also supports FeatreSchemaKind in the filter
744+ # status can be ALL or UNUSED
745+
746+ def create_ontology (self , name , normalized_ontology = None , root_schema_ids = None ):
747+ """
748+ - If I create an ontology with an empty ontology does it create the root schemas?
749+ - If I mix ontology with root schemas it reuses right?
750+
751+ - should be able to lookup root schema nodes for an ontology. Add relationship..
752+ """
753+ query_str = """mutation upsertRootSchemaNodePyApi($data: UpsertOntologyInput!){
754+ upsertOntology(data: $data){
755+ %s
756+ }
757+ } """ % query .results_query_part (Entity .Ontology )
758+ if normalized_ontology is None :
759+ if root_schema_ids is None :
760+ raise ValueError ("Must provide either a normalized ontology or a list of root_schema_ids" )
761+ return root_schema_ids
762+
763+ res = self .execute (query_str , {'data' : {'name' : name ,'normalized' : json .dumps (normalized_ontology )}})
764+ return Entity .Ontology (self , res ['upsertOntology' ])
765+
766+
767+ def create_root_schema (self , normalized_ontology ):
768+ query_str = """mutation upsertRootSchemaNodePyApi($data: UpsertRootSchemaNodeInput!){
769+ upsertRootSchemaNode(data: $data){
770+ %s
771+ }
772+ } """ % query .results_query_part (Entity .RootSchema )
773+ # TODO: Is this necessary?
774+ normalized_ontology = {k :v for k ,v in normalized_ontology .items () if v }
775+ # Check color. Quick gotcha..
776+ if 'color' not in normalized_ontology :
777+ raise KeyError ("Must provide color." )
778+ return Entity .RootSchemaNode (self , self .execute (query_str , {'data' : {'normalized' : json .dumps (normalized_ontology )}})['upsertRootSchemaNode' ])
779+
780+
725781
726782
0 commit comments