apply ruff formatting
This commit is contained in:
@@ -21,9 +21,7 @@ logger = logging.getLogger(__name__)
|
||||
@handle_http_errors("create_presentation", service_type="slides")
|
||||
@require_google_service("slides", "slides")
|
||||
async def create_presentation(
|
||||
service,
|
||||
user_google_email: str,
|
||||
title: str = "Untitled Presentation"
|
||||
service, user_google_email: str, title: str = "Untitled Presentation"
|
||||
) -> str:
|
||||
"""
|
||||
Create a new Google Slides presentation.
|
||||
@@ -35,24 +33,22 @@ async def create_presentation(
|
||||
Returns:
|
||||
str: Details about the created presentation including ID and URL.
|
||||
"""
|
||||
logger.info(f"[create_presentation] Invoked. Email: '{user_google_email}', Title: '{title}'")
|
||||
|
||||
body = {
|
||||
'title': title
|
||||
}
|
||||
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations().create(body=body).execute
|
||||
logger.info(
|
||||
f"[create_presentation] Invoked. Email: '{user_google_email}', Title: '{title}'"
|
||||
)
|
||||
|
||||
presentation_id = result.get('presentationId')
|
||||
body = {"title": title}
|
||||
|
||||
result = await asyncio.to_thread(service.presentations().create(body=body).execute)
|
||||
|
||||
presentation_id = result.get("presentationId")
|
||||
presentation_url = f"https://docs.google.com/presentation/d/{presentation_id}/edit"
|
||||
|
||||
confirmation_message = f"""Presentation Created Successfully for {user_google_email}:
|
||||
- Title: {title}
|
||||
- Presentation ID: {presentation_id}
|
||||
- URL: {presentation_url}
|
||||
- Slides: {len(result.get('slides', []))} slide(s) created"""
|
||||
- Slides: {len(result.get("slides", []))} slide(s) created"""
|
||||
|
||||
logger.info(f"Presentation created successfully for {user_google_email}")
|
||||
return confirmation_message
|
||||
@@ -62,9 +58,7 @@ async def create_presentation(
|
||||
@handle_http_errors("get_presentation", is_read_only=True, service_type="slides")
|
||||
@require_google_service("slides", "slides_read")
|
||||
async def get_presentation(
|
||||
service,
|
||||
user_google_email: str,
|
||||
presentation_id: str
|
||||
service, user_google_email: str, presentation_id: str
|
||||
) -> str:
|
||||
"""
|
||||
Get details about a Google Slides presentation.
|
||||
@@ -76,44 +70,50 @@ async def get_presentation(
|
||||
Returns:
|
||||
str: Details about the presentation including title, slides count, and metadata.
|
||||
"""
|
||||
logger.info(f"[get_presentation] Invoked. Email: '{user_google_email}', ID: '{presentation_id}'")
|
||||
logger.info(
|
||||
f"[get_presentation] Invoked. Email: '{user_google_email}', ID: '{presentation_id}'"
|
||||
)
|
||||
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations().get(presentationId=presentation_id).execute
|
||||
)
|
||||
|
||||
title = result.get('title', 'Untitled')
|
||||
slides = result.get('slides', [])
|
||||
page_size = result.get('pageSize', {})
|
||||
title = result.get("title", "Untitled")
|
||||
slides = result.get("slides", [])
|
||||
page_size = result.get("pageSize", {})
|
||||
|
||||
slides_info = []
|
||||
for i, slide in enumerate(slides, 1):
|
||||
slide_id = slide.get('objectId', 'Unknown')
|
||||
page_elements = slide.get('pageElements', [])
|
||||
slide_id = slide.get("objectId", "Unknown")
|
||||
page_elements = slide.get("pageElements", [])
|
||||
|
||||
# Collect text from the slide whose JSON structure is very complicated
|
||||
# https://googleapis.github.io/google-api-python-client/docs/dyn/slides_v1.presentations.html#get
|
||||
slide_text = ""
|
||||
try:
|
||||
texts_from_elements = []
|
||||
for page_element in slide.get('pageElements', []):
|
||||
shape = page_element.get('shape', None)
|
||||
if shape and shape.get('text', None):
|
||||
text = shape.get('text', None)
|
||||
for page_element in slide.get("pageElements", []):
|
||||
shape = page_element.get("shape", None)
|
||||
if shape and shape.get("text", None):
|
||||
text = shape.get("text", None)
|
||||
if text:
|
||||
text_elements_in_shape = []
|
||||
for text_element in text.get('textElements', []):
|
||||
text_run = text_element.get('textRun', None)
|
||||
for text_element in text.get("textElements", []):
|
||||
text_run = text_element.get("textRun", None)
|
||||
if text_run:
|
||||
content = text_run.get('content', None)
|
||||
content = text_run.get("content", None)
|
||||
if content:
|
||||
start_index = text_element.get('startIndex', 0)
|
||||
text_elements_in_shape.append((start_index, content))
|
||||
start_index = text_element.get("startIndex", 0)
|
||||
text_elements_in_shape.append(
|
||||
(start_index, content)
|
||||
)
|
||||
|
||||
if text_elements_in_shape:
|
||||
# Sort text elements within a single shape
|
||||
text_elements_in_shape.sort(key=lambda item: item[0])
|
||||
full_text_from_shape = "".join([item[1] for item in text_elements_in_shape])
|
||||
full_text_from_shape = "".join(
|
||||
[item[1] for item in text_elements_in_shape]
|
||||
)
|
||||
texts_from_elements.append(full_text_from_shape)
|
||||
|
||||
# cleanup text we collected
|
||||
@@ -129,17 +129,19 @@ async def get_presentation(
|
||||
logger.warning(f"Failed to extract text from the slide {slide_id}: {e}")
|
||||
slide_text = f"<failed to extract text: {type(e)}, {e}>"
|
||||
|
||||
slides_info.append(f" Slide {i}: ID {slide_id}, {len(page_elements)} element(s), text: {slide_text if slide_text else 'empty'}")
|
||||
slides_info.append(
|
||||
f" Slide {i}: ID {slide_id}, {len(page_elements)} element(s), text: {slide_text if slide_text else 'empty'}"
|
||||
)
|
||||
|
||||
confirmation_message = f"""Presentation Details for {user_google_email}:
|
||||
- Title: {title}
|
||||
- Presentation ID: {presentation_id}
|
||||
- URL: https://docs.google.com/presentation/d/{presentation_id}/edit
|
||||
- Total Slides: {len(slides)}
|
||||
- Page Size: {page_size.get('width', {}).get('magnitude', 'Unknown')} x {page_size.get('height', {}).get('magnitude', 'Unknown')} {page_size.get('width', {}).get('unit', '')}
|
||||
- Page Size: {page_size.get("width", {}).get("magnitude", "Unknown")} x {page_size.get("height", {}).get("magnitude", "Unknown")} {page_size.get("width", {}).get("unit", "")}
|
||||
|
||||
Slides Breakdown:
|
||||
{chr(10).join(slides_info) if slides_info else ' No slides found'}"""
|
||||
{chr(10).join(slides_info) if slides_info else " No slides found"}"""
|
||||
|
||||
logger.info(f"Presentation retrieved successfully for {user_google_email}")
|
||||
return confirmation_message
|
||||
@@ -152,7 +154,7 @@ async def batch_update_presentation(
|
||||
service,
|
||||
user_google_email: str,
|
||||
presentation_id: str,
|
||||
requests: List[Dict[str, Any]]
|
||||
requests: List[Dict[str, Any]],
|
||||
) -> str:
|
||||
"""
|
||||
Apply batch updates to a Google Slides presentation.
|
||||
@@ -165,20 +167,19 @@ async def batch_update_presentation(
|
||||
Returns:
|
||||
str: Details about the batch update operation results.
|
||||
"""
|
||||
logger.info(f"[batch_update_presentation] Invoked. Email: '{user_google_email}', ID: '{presentation_id}', Requests: {len(requests)}")
|
||||
|
||||
body = {
|
||||
'requests': requests
|
||||
}
|
||||
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations().batchUpdate(
|
||||
presentationId=presentation_id,
|
||||
body=body
|
||||
).execute
|
||||
logger.info(
|
||||
f"[batch_update_presentation] Invoked. Email: '{user_google_email}', ID: '{presentation_id}', Requests: {len(requests)}"
|
||||
)
|
||||
|
||||
replies = result.get('replies', [])
|
||||
body = {"requests": requests}
|
||||
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations()
|
||||
.batchUpdate(presentationId=presentation_id, body=body)
|
||||
.execute
|
||||
)
|
||||
|
||||
replies = result.get("replies", [])
|
||||
|
||||
confirmation_message = f"""Batch Update Completed for {user_google_email}:
|
||||
- Presentation ID: {presentation_id}
|
||||
@@ -189,12 +190,16 @@ async def batch_update_presentation(
|
||||
if replies:
|
||||
confirmation_message += "\n\nUpdate Results:"
|
||||
for i, reply in enumerate(replies, 1):
|
||||
if 'createSlide' in reply:
|
||||
slide_id = reply['createSlide'].get('objectId', 'Unknown')
|
||||
confirmation_message += f"\n Request {i}: Created slide with ID {slide_id}"
|
||||
elif 'createShape' in reply:
|
||||
shape_id = reply['createShape'].get('objectId', 'Unknown')
|
||||
confirmation_message += f"\n Request {i}: Created shape with ID {shape_id}"
|
||||
if "createSlide" in reply:
|
||||
slide_id = reply["createSlide"].get("objectId", "Unknown")
|
||||
confirmation_message += (
|
||||
f"\n Request {i}: Created slide with ID {slide_id}"
|
||||
)
|
||||
elif "createShape" in reply:
|
||||
shape_id = reply["createShape"].get("objectId", "Unknown")
|
||||
confirmation_message += (
|
||||
f"\n Request {i}: Created shape with ID {shape_id}"
|
||||
)
|
||||
else:
|
||||
confirmation_message += f"\n Request {i}: Operation completed"
|
||||
|
||||
@@ -206,10 +211,7 @@ async def batch_update_presentation(
|
||||
@handle_http_errors("get_page", is_read_only=True, service_type="slides")
|
||||
@require_google_service("slides", "slides_read")
|
||||
async def get_page(
|
||||
service,
|
||||
user_google_email: str,
|
||||
presentation_id: str,
|
||||
page_object_id: str
|
||||
service, user_google_email: str, presentation_id: str, page_object_id: str
|
||||
) -> str:
|
||||
"""
|
||||
Get details about a specific page (slide) in a presentation.
|
||||
@@ -222,31 +224,33 @@ async def get_page(
|
||||
Returns:
|
||||
str: Details about the specific page including elements and layout.
|
||||
"""
|
||||
logger.info(f"[get_page] Invoked. Email: '{user_google_email}', Presentation: '{presentation_id}', Page: '{page_object_id}'")
|
||||
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations().pages().get(
|
||||
presentationId=presentation_id,
|
||||
pageObjectId=page_object_id
|
||||
).execute
|
||||
logger.info(
|
||||
f"[get_page] Invoked. Email: '{user_google_email}', Presentation: '{presentation_id}', Page: '{page_object_id}'"
|
||||
)
|
||||
|
||||
page_type = result.get('pageType', 'Unknown')
|
||||
page_elements = result.get('pageElements', [])
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations()
|
||||
.pages()
|
||||
.get(presentationId=presentation_id, pageObjectId=page_object_id)
|
||||
.execute
|
||||
)
|
||||
|
||||
page_type = result.get("pageType", "Unknown")
|
||||
page_elements = result.get("pageElements", [])
|
||||
|
||||
elements_info = []
|
||||
for element in page_elements:
|
||||
element_id = element.get('objectId', 'Unknown')
|
||||
if 'shape' in element:
|
||||
shape_type = element['shape'].get('shapeType', 'Unknown')
|
||||
element_id = element.get("objectId", "Unknown")
|
||||
if "shape" in element:
|
||||
shape_type = element["shape"].get("shapeType", "Unknown")
|
||||
elements_info.append(f" Shape: ID {element_id}, Type: {shape_type}")
|
||||
elif 'table' in element:
|
||||
table = element['table']
|
||||
rows = table.get('rows', 0)
|
||||
cols = table.get('columns', 0)
|
||||
elif "table" in element:
|
||||
table = element["table"]
|
||||
rows = table.get("rows", 0)
|
||||
cols = table.get("columns", 0)
|
||||
elements_info.append(f" Table: ID {element_id}, Size: {rows}x{cols}")
|
||||
elif 'line' in element:
|
||||
line_type = element['line'].get('lineType', 'Unknown')
|
||||
elif "line" in element:
|
||||
line_type = element["line"].get("lineType", "Unknown")
|
||||
elements_info.append(f" Line: ID {element_id}, Type: {line_type}")
|
||||
else:
|
||||
elements_info.append(f" Element: ID {element_id}, Type: Unknown")
|
||||
@@ -258,7 +262,7 @@ async def get_page(
|
||||
- Total Elements: {len(page_elements)}
|
||||
|
||||
Page Elements:
|
||||
{chr(10).join(elements_info) if elements_info else ' No elements found'}"""
|
||||
{chr(10).join(elements_info) if elements_info else " No elements found"}"""
|
||||
|
||||
logger.info(f"Page retrieved successfully for {user_google_email}")
|
||||
return confirmation_message
|
||||
@@ -272,7 +276,7 @@ async def get_page_thumbnail(
|
||||
user_google_email: str,
|
||||
presentation_id: str,
|
||||
page_object_id: str,
|
||||
thumbnail_size: str = "MEDIUM"
|
||||
thumbnail_size: str = "MEDIUM",
|
||||
) -> str:
|
||||
"""
|
||||
Generate a thumbnail URL for a specific page (slide) in a presentation.
|
||||
@@ -286,18 +290,23 @@ async def get_page_thumbnail(
|
||||
Returns:
|
||||
str: URL to the generated thumbnail image.
|
||||
"""
|
||||
logger.info(f"[get_page_thumbnail] Invoked. Email: '{user_google_email}', Presentation: '{presentation_id}', Page: '{page_object_id}', Size: '{thumbnail_size}'")
|
||||
logger.info(
|
||||
f"[get_page_thumbnail] Invoked. Email: '{user_google_email}', Presentation: '{presentation_id}', Page: '{page_object_id}', Size: '{thumbnail_size}'"
|
||||
)
|
||||
|
||||
result = await asyncio.to_thread(
|
||||
service.presentations().pages().getThumbnail(
|
||||
service.presentations()
|
||||
.pages()
|
||||
.getThumbnail(
|
||||
presentationId=presentation_id,
|
||||
pageObjectId=page_object_id,
|
||||
thumbnailProperties_thumbnailSize=thumbnail_size,
|
||||
thumbnailProperties_mimeType='PNG'
|
||||
).execute
|
||||
thumbnailProperties_mimeType="PNG",
|
||||
)
|
||||
.execute
|
||||
)
|
||||
|
||||
thumbnail_url = result.get('contentUrl', '')
|
||||
thumbnail_url = result.get("contentUrl", "")
|
||||
|
||||
confirmation_message = f"""Thumbnail Generated for {user_google_email}:
|
||||
- Presentation ID: {presentation_id}
|
||||
@@ -313,13 +322,13 @@ You can view or download the thumbnail using the provided URL."""
|
||||
|
||||
# Create comment management tools for slides
|
||||
_comment_tools = create_comment_tools("presentation", "presentation_id")
|
||||
read_presentation_comments = _comment_tools['read_comments']
|
||||
create_presentation_comment = _comment_tools['create_comment']
|
||||
reply_to_presentation_comment = _comment_tools['reply_to_comment']
|
||||
resolve_presentation_comment = _comment_tools['resolve_comment']
|
||||
read_presentation_comments = _comment_tools["read_comments"]
|
||||
create_presentation_comment = _comment_tools["create_comment"]
|
||||
reply_to_presentation_comment = _comment_tools["reply_to_comment"]
|
||||
resolve_presentation_comment = _comment_tools["resolve_comment"]
|
||||
|
||||
# Aliases for backwards compatibility and intuitive naming
|
||||
read_slide_comments = read_presentation_comments
|
||||
create_slide_comment = create_presentation_comment
|
||||
reply_to_slide_comment = reply_to_presentation_comment
|
||||
resolve_slide_comment = resolve_presentation_comment
|
||||
resolve_slide_comment = resolve_presentation_comment
|
||||
|
||||
Reference in New Issue
Block a user