Skip to content

Commit 6abddc0

Browse files
committed
Add tuned_models samples
Change-Id: I4b85a0179d2e7e4c112ed6250a7e602de57787c4
1 parent 1ffbcb3 commit 6abddc0

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

google/generativeai/types/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.generativeai.types.generation_types import *
2222
from google.generativeai.types.helper_types import *
2323
from google.generativeai.types.model_types import *
24+
from google.generativeai.types.permission_types import *
2425
from google.generativeai.types.safety_types import *
2526
from google.generativeai.types.text_types import *
2627

google/generativeai/types/permission_types.py

+68-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from google.generativeai.utils import flatten_update_paths
2929
from google.generativeai import string_utils
3030

31+
__all__ = ['Permission', 'Permissions']
3132

3233
GranteeType = protos.Permission.GranteeType
3334
Role = protos.Permission.Role
@@ -89,7 +90,7 @@ def valid_id(name: str) -> bool:
8990

9091

9192
@string_utils.prettyprint
92-
@dataclasses.dataclass
93+
@dataclasses.dataclass(init=False)
9394
class Permission:
9495
"""
9596
A permission to access a resource.
@@ -100,6 +101,24 @@ class Permission:
100101
grantee_type: Optional[GranteeType]
101102
email_address: Optional[str] = None
102103

104+
def __init__(
105+
self,
106+
name: str,
107+
role: RoleOptions,
108+
grantee_type: Optional[GranteeTypeOptions] = None,
109+
email_address: Optional[str] = None,
110+
):
111+
self.name = name
112+
if role is None:
113+
self.role = None
114+
else:
115+
self.role = to_role(role)
116+
if grantee_type is None:
117+
self.grantee_type = None
118+
else:
119+
self.grantee_type = to_grantee_type(grantee_type)
120+
self.email_address = email_address
121+
103122
def delete(
104123
self,
105124
client: glm.PermissionServiceClient | None = None,
@@ -133,7 +152,8 @@ def _apply_update(self, path, value):
133152

134153
def update(
135154
self,
136-
updates: dict[str, Any],
155+
updates: dict[str, Any]|None,
156+
*,
137157
client: glm.PermissionServiceClient | None = None,
138158
) -> Permission:
139159
"""
@@ -279,6 +299,12 @@ def _make_create_permission_request(
279299
f"Invalid operation: An 'email_address' must be provided when 'grantee_type' is not set to 'EVERYONE'. Currently, 'grantee_type' is set to '{grantee_type}' and 'email_address' is '{email_address if email_address else 'not provided'}'."
280300
)
281301

302+
if email_address and grantee_type is None:
303+
if email_address.endswith("googlegroups.com"):
304+
grantee_type = GranteeType.GROUP
305+
else:
306+
grantee_type = GranteeType.USER
307+
282308
permission = protos.Permission(
283309
role=role,
284310
grantee_type=grantee_type,
@@ -367,6 +393,10 @@ def list(
367393
permission = type(permission).to_dict(permission)
368394
yield Permission(**permission)
369395

396+
def __iter__(self):
397+
return self.list()
398+
399+
370400
async def list_async(
371401
self,
372402
page_size: Optional[int] = None,
@@ -385,6 +415,42 @@ async def list_async(
385415
permission = type(permission).to_dict(permission)
386416
yield Permission(**permission)
387417

418+
async def __aiter__(self):
419+
return await self.async_list()
420+
421+
@classmethod
422+
def get(
423+
cls,
424+
name: str,
425+
client: glm.PermissionServiceClient | None = None,
426+
) -> Permission:
427+
"""
428+
Get information about a specific permission.
429+
430+
Args:
431+
name: The name of the permission to get.
432+
433+
Returns:
434+
Requested permission as an instance of `Permission`.
435+
"""
436+
return Permission.get(name)
437+
438+
@classmethod
439+
async def get_async(
440+
cls,
441+
name: str
442+
):
443+
"""
444+
Get information about a specific permission.
445+
446+
Args:
447+
name: The name of the permission to get.
448+
449+
Returns:
450+
Requested permission as an instance of `Permission`.
451+
"""
452+
return await Permission.get_async(name)
453+
388454
def transfer_ownership(
389455
self,
390456
email_address: str,

0 commit comments

Comments
 (0)