1
1
from enum import Enum
2
- from typing import Set , List , TypedDict , Optional
2
+ from typing import Set , List , Optional , Union , TypedDict
3
+
3
4
4
5
from labelbox import Client
5
6
from labelbox .exceptions import ResourceCreationError
7
+ from labelbox .pydantic_compat import BaseModel
6
8
from labelbox .schema .user import User
7
9
from labelbox .schema .project import Project
8
- from labelbox .pydantic_compat import BaseModel , PrivateAttr
9
10
10
- class GroupColor (Enum ):
11
+
12
+ class UserGroupColor (Enum ):
11
13
"""
12
14
Enum representing the colors available for a group.
13
15
@@ -33,7 +35,7 @@ class GroupColor(Enum):
33
35
GRAY = "B8C4D3"
34
36
35
37
36
- class GroupUser (BaseModel ):
38
+ class UserGroupUser (BaseModel ):
37
39
"""
38
40
Represents a user in a group.
39
41
@@ -44,17 +46,16 @@ class GroupUser(BaseModel):
44
46
id : str
45
47
email : str
46
48
47
-
48
49
def __hash__ (self ):
49
50
return hash ((self .id ))
50
51
51
52
def __eq__ (self , other ):
52
- if not isinstance (other , GroupUser ):
53
+ if not isinstance (other , UserGroupUser ):
53
54
return False
54
55
return self .id == other .id
55
56
56
57
57
- class GroupProject (BaseModel ):
58
+ class UserGroupProject (BaseModel ):
58
59
"""
59
60
Represents a project in a group.
60
61
@@ -78,76 +79,77 @@ def __eq__(self, other):
78
79
Returns:
79
80
bool: True if the two GroupProject objects are equal, False otherwise.
80
81
"""
81
- if not isinstance (other , GroupProject ):
82
+ if not isinstance (other , UserGroupProject ):
82
83
return False
83
84
return self .id == other .id
84
85
85
86
86
- class GroupParmeters (TypedDict ):
87
+ class UserGroupParameters (TypedDict ):
87
88
"""
88
- Represents the parameters for a group.
89
+ Represents the parameters for a user group.
89
90
90
91
Attributes:
91
- id (Optional[str]): The ID of the group.
92
- name (Optional[str]): The name of the group.
93
- color (Optional[GroupColor ]): The color of the group.
94
- users (Optional[Set[GroupUser]] ): The users in the group.
95
- projects (Optional[Set[GroupProject]] ): The projects associated with the group.
92
+ id (Optional[str]): The ID of the user group.
93
+ name (Optional[str]): The name of the user group.
94
+ color (Optional[UserGroupColor ]): The color of the user group.
95
+ users (Optional[Set[Union[UserGroupUser, User]]] ): The users in the user group.
96
+ projects (Optional[Set[Union[UserGroupProject, Project]]] ): The projects associated with the user group.
96
97
"""
97
98
id : Optional [str ]
98
99
name : Optional [str ]
99
- color : Optional [GroupColor ]
100
- users : Optional [Set [GroupUser ]]
101
- projects : Optional [Set [GroupProject ]]
100
+ color : Optional [UserGroupColor ]
101
+ users : Optional [Set [Union [ UserGroupUser , User ] ]]
102
+ projects : Optional [Set [Union [ UserGroupProject , Project ] ]]
102
103
103
104
104
- class Group () :
105
+ class UserGroup :
105
106
"""
106
- Represents a group of users in Labelbox.
107
+ Represents a user group in Labelbox.
107
108
108
109
Args:
109
110
client (Client): The Labelbox client.
110
- data (dict): The data dictionary containing group information .
111
+ **kwargs: Additional keyword arguments for initializing the UserGroup object .
111
112
112
113
Attributes:
113
- id (str): The ID of the group.
114
- name (str): The name of the group.
115
- color (GroupColor ): The color of the group.
116
- users (List[str] ): The list of user IDs in the group.
117
- projects (List[str] ): The list of project IDs in the group.
118
- client (Client): The Labelbox client.
114
+ _id (str): The ID of the user group.
115
+ _name (str): The name of the user group.
116
+ _color (UserGroupColor ): The color of the user group.
117
+ _users (Set[Union[UserGroupUser, User]] ): The set of user IDs in the user group.
118
+ _projects (Set[Union[UserGroupProject, Project]] ): The set of project IDs in the user group.
119
+ _client (Client): The Labelbox client.
119
120
"""
120
- _id : str
121
+ _id : str = None
121
122
_name : str = None
122
- _color : GroupColor = None
123
- _users : Set [GroupUser ] = None
124
- _projects : Set [GroupProject ] = None
125
- _client : Client
123
+ _color : UserGroupColor = None
124
+ _users : Set [Union [ UserGroupUser , User ] ] = None
125
+ _projects : Set [Union [ UserGroupProject , Project ] ] = None
126
+ _client : Client
126
127
127
- def __init__ (self , client : Client , ** kwargs : GroupParmeters ):
128
+ def __init__ (self , client : Client , ** kwargs : UserGroupParameters ):
128
129
"""
129
130
Initializes a Group object.
130
131
131
132
Args:
132
133
client (Client): The Labelbox client.
133
134
**kwargs: Additional keyword arguments for initializing the Group object.
134
135
"""
135
- self . id = kwargs [ 'id' ]
136
- self .color = kwargs .get ('color' , GroupColor .BLUE )
136
+ super (). __init__ ()
137
+ self .color = kwargs .get ('color' , UserGroupColor .BLUE )
137
138
self .users = kwargs .get ('users' , {})
138
139
self .projects = kwargs .get ('projects' , {})
139
140
self .client = client
140
141
141
142
# runs against _gql
142
- if client .enable_experimental is False :
143
+ if not client .enable_experimental :
143
144
raise RuntimeError ("Experimental features are not enabled. Please enable them in the client to use this feature." )
144
145
146
+ if 'id' not in kwargs and 'name' not in kwargs :
147
+ raise ValueError ("Either 'id' or 'name' must be provided" )
148
+
145
149
# partial respentation of the group, reload
146
- if self .id is not None :
150
+ if 'id' in kwargs :
151
+ self .id = kwargs ['id' ]
147
152
self ._reload ()
148
- else :
149
- self .name = kwargs ['name' ]
150
- super ().__init__ ()
151
153
152
154
def _reload (self ):
153
155
"""
@@ -188,9 +190,9 @@ def _reload(self):
188
190
}
189
191
result = self .client .execute (query , params )
190
192
self .name = result ["userGroup" ]["name" ]
191
- self .color = GroupColor (result ["userGroup" ]["color" ])
192
- self .projects = {GroupProject (id = project ["id" ], name = project ["name" ]) for project in result ["userGroup" ]["projects" ]["nodes" ]}
193
- self .users = {GroupUser (id = member ["id" ], email = member ["email" ]) for member in result ["userGroup" ]["members" ]["nodes" ]}
193
+ self .color = UserGroupColor (result ["userGroup" ]["color" ])
194
+ self .projects = {UserGroupProject (id = project ["id" ], name = project ["name" ]) for project in result ["userGroup" ]["projects" ]["nodes" ]}
195
+ self .users = {UserGroupUser (id = member ["id" ], email = member ["email" ]) for member in result ["userGroup" ]["members" ]["nodes" ]}
194
196
195
197
@property
196
198
def id (self ) -> str :
@@ -233,7 +235,7 @@ def name(self, value: str) -> None:
233
235
self ._name = value
234
236
235
237
@property
236
- def color (self ) -> GroupColor :
238
+ def color (self ) -> UserGroupColor :
237
239
"""
238
240
Gets the color of the group.
239
241
@@ -243,18 +245,17 @@ def color(self) -> GroupColor:
243
245
return self ._color
244
246
245
247
@color .setter
246
- def color (self , value : GroupColor ) -> None :
248
+ def color (self , value : UserGroupColor ) -> None :
247
249
"""
248
250
Sets the color of the group.
249
251
250
252
Args:
251
253
value (GroupColor): The color to set.
252
254
"""
253
255
self ._color = value
254
- self ._update ()
255
256
256
257
@property
257
- def users (self ) -> Set [GroupUser ]:
258
+ def users (self ) -> Set [Union [ UserGroupUser , User ] ]:
258
259
"""
259
260
Gets the list of user IDs in the group.
260
261
@@ -264,7 +265,7 @@ def users(self) -> Set[GroupUser]:
264
265
return self ._users
265
266
266
267
@users .setter
267
- def users (self , value : Set [GroupUser ]) -> None :
268
+ def users (self , value : Set [Union [ UserGroupUser , User ] ]) -> None :
268
269
"""
269
270
Sets the list of user IDs in the group.
270
271
@@ -274,7 +275,7 @@ def users(self, value: Set[GroupUser]) -> None:
274
275
self ._users = value
275
276
276
277
@property
277
- def projects (self ) -> Set [GroupProject ]:
278
+ def projects (self ) -> Set [UserGroupProject ]:
278
279
"""
279
280
Gets the list of project IDs in the group.
280
281
@@ -284,7 +285,7 @@ def projects(self) -> Set[GroupProject]:
284
285
return self ._projects
285
286
286
287
@projects .setter
287
- def projects (self , value : Set [GroupProject ]) -> None :
288
+ def projects (self , value : Set [UserGroupProject ]) -> None :
288
289
"""
289
290
Sets the list of project IDs in the group.
290
291
@@ -451,9 +452,9 @@ def groups(client: Client) -> List["Group"]:
451
452
client ,
452
453
group ["id" ],
453
454
group ["name" ],
454
- GroupColor (group ["color" ]),
455
- {GroupUser (id = member ["id" ], email = member ["email" ]) for member in group ["members" ]["nodes" ]},
456
- {GroupProject (id = project ["id" ], name = project ["name" ]) for project in group ["projects" ]["nodes" ]}
455
+ UserGroupColor (group ["color" ]),
456
+ {UserGroupUser (id = member ["id" ], email = member ["email" ]) for member in group ["members" ]["nodes" ]},
457
+ {UserGroupProject (id = project ["id" ], name = project ["name" ]) for project in group ["projects" ]["nodes" ]}
457
458
)
458
459
for group in groups
459
460
]
0 commit comments