File size: 12,977 Bytes
fcaa164 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
import inspect
import os
import re
import traceback
from copy import deepcopy
from dataclasses import dataclass
from enum import Enum
from functools import partial
from typing import Union
import PIL
from pptx.enum.text import MSO_ANCHOR, PP_ALIGN
from pptx.oxml import parse_xml
from pptx.shapes.base import BaseShape
from pptx.util import Pt
from src.presentation import Closure, Picture, ShapeElement, SlidePage
from src.utils import runs_merge
@dataclass
class HistoryMark:
"""
Mark the execution status of the API call, comment and a line of code.
"""
API_CALL_ERROR = "api_call_error"
API_CALL_CORRECT = "api_call_correct"
COMMENT_CORRECT = "comment_correct"
COMMENT_ERROR = "comment_error"
CODE_RUN_ERROR = "code_run_error"
CODE_RUN_CORRECT = "code_run_correct"
class CodeExecutor:
"""
Execute code actions and manage API call history, and providing error feedback.
"""
def __init__(self, retry_times: int):
"""
Initialize the CodeExecutor.
Args:
retry_times (int): The number of times to retry failed actions.
"""
self.api_history = []
self.command_history = []
self.code_history = []
self.retry_times = retry_times
self.registered_functions = API_TYPES.all_funcs()
self.function_regex = re.compile(r"^[a-z]+_[a-z_]+\(.+\)")
def get_apis_docs(self, funcs: list[callable], show_example: bool = True) -> str:
"""
Get the documentation for a list of API functions.
Args:
funcs (list[callable]): A list of functions to document.
show_example (bool): Whether to show examples in the documentation.
Returns:
str: The formatted API documentation.
"""
api_doc = []
for func in funcs:
sig = inspect.signature(func)
params = []
for name, param in sig.parameters.items():
if name == "slide":
continue
param_str = name
if param.annotation != inspect.Parameter.empty:
param_str += f": {param.annotation.__name__}"
if param.default != inspect.Parameter.empty:
param_str += f" = {repr(param.default)}"
params.append(param_str)
signature = f"def {func.__name__}({', '.join(params)})"
if not show_example:
api_doc.append(signature)
continue
doc = inspect.getdoc(func)
if doc is not None:
signature += f"\n\t{doc}"
api_doc.append(signature)
return "\n\n".join(api_doc)
def execute_actions(
self, actions: str, edit_slide: SlidePage, found_code: bool = False
) -> Union[tuple[str, str], None]:
"""
Execute a series of actions on a slide.
Args:
actions (str): The actions to execute.
edit_slide (SlidePage): The slide to edit.
found_code (bool): Whether code was found in the actions.
Returns:
tuple: The API lines and traceback if an error occurs.
None: If no error occurs.
"""
api_calls = actions.strip().split("\n")
self.api_history.append(
[HistoryMark.API_CALL_ERROR, edit_slide.slide_idx, actions]
)
for line_idx, line in enumerate(api_calls):
try:
if line_idx == len(api_calls) - 1 and not found_code:
raise ValueError(
"No code block found in the output, please output the api calls without any prefix."
)
if line.startswith("def"):
raise PermissionError("The function definition were not allowed.")
if line.startswith("#"):
if len(self.command_history) != 0:
self.command_history[-1][0] = HistoryMark.COMMENT_CORRECT
self.command_history.append([HistoryMark.COMMENT_ERROR, line, None])
continue
if not self.function_regex.match(line):
continue
found_code = True
func = line.split("(")[0]
if func not in self.registered_functions:
raise NameError(f"The function {func} is not defined.")
# only one of clone and del can be used in a row
if func.startswith("clone") or func.startswith("del"):
tag = func.split("_")[0]
if (
self.command_history[-1][-1] == None
or self.command_history[-1][-1] == tag
):
self.command_history[-1][-1] = tag
else:
raise ValueError(
"Invalid command: Both 'clone_paragraph' and 'del_paragraph'/'del_image' are used within a single command. "
"Each command must only perform one of these operations based on the quantity_change."
)
self.code_history.append([HistoryMark.CODE_RUN_ERROR, line, None])
partial_func = partial(self.registered_functions[func], edit_slide)
eval(line, {}, {func: partial_func})
self.code_history[-1][0] = HistoryMark.CODE_RUN_CORRECT
except:
trace_msg = traceback.format_exc()
if len(self.code_history) != 0:
self.code_history[-1][-1] = trace_msg
api_lines = (
"\n".join(api_calls[: line_idx - 1])
+ f"\n--> Error Line: {line}\n"
+ "\n".join(api_calls[line_idx + 1 :])
)
return api_lines, trace_msg
if len(self.command_history) != 0:
self.command_history[-1][0] = HistoryMark.COMMENT_CORRECT
self.api_history[-1][0] = HistoryMark.API_CALL_CORRECT
# supporting functions
def element_index(slide: SlidePage, element_id: int) -> ShapeElement:
"""
Find the an element in a slide.
Args:
slide (SlidePage): The slide
element_id (int): The ID of the element.
Returns:
ShapeElement: The shape corresponding to the element ID.
Raises:
IndexError: If the element is not found.
"""
for shape in slide:
if shape.shape_idx == element_id:
return shape
raise IndexError(f"Cannot find element {element_id}, is it deleted or not exist?")
def replace_para(paragraph_id: int, new_text: str, shape: BaseShape):
"""
Replace the text of a paragraph in a shape.
"""
para = shape.text_frame.paragraphs[paragraph_id]
runs_merge(para).text = new_text
def clone_para(paragraph_id: int, shape: BaseShape):
"""
Clone a paragraph in a shape.
"""
para = shape.text_frame.paragraphs[paragraph_id]
shape.text_frame.paragraphs[-1]._element.addnext(parse_xml(para._element.xml))
def del_para(paragraph_id: int, shape: BaseShape):
"""
Delete a paragraph from a shape.
"""
para = shape.text_frame.paragraphs[paragraph_id]
para._element.getparent().remove(para._element)
# api functions
def del_paragraph(slide: SlidePage, div_id: int, paragraph_id: int):
"""
Delete a paragraph from a slide.
Args:
slide (SlidePage): The slide containing the paragraph.
div_id (int): The ID of the division containing the paragraph.
paragraph_id (int): The ID of the paragraph to delete.
Raises:
IndexError: If the paragraph is not found.
"""
shape = element_index(slide, div_id)
assert (
shape.text_frame.is_textframe
), "The element does not have a text frame, please check the element id and type of element."
for para in shape.text_frame.paragraphs:
if para.idx == paragraph_id:
shape.text_frame.paragraphs.remove(para)
shape._closures["delete"].append(
Closure(partial(del_para, para.real_idx), para.real_idx)
)
return
else:
raise IndexError(
f"Cannot find the paragraph {paragraph_id} of the element {div_id},"
"may refer to a non-existed paragraph or you haven't cloned enough paragraphs beforehand."
)
def del_image(slide: SlidePage, figure_id: int):
"""
Delete an image from a slide.
Args:
slide (SlidePage): The slide containing the image.
figure_id (int): The ID of the image to delete.
"""
shape = element_index(slide, figure_id)
assert isinstance(shape, Picture), "The element is not a Picture."
slide.shapes.remove(shape)
def replace_paragraph(slide: SlidePage, div_id: int, paragraph_id: int, text: str):
"""
Replace the text of a paragraph in a slide.
Args:
slide (SlidePage): The slide containing the paragraph.
div_id (int): The ID of the division containing the paragraph.
paragraph_id (int): The ID of the paragraph to replace.
text (str): The new text to replace with.
Raises:
IndexError: If the paragraph is not found.
"""
shape = element_index(slide, div_id)
assert (
shape.text_frame.is_textframe
), "The element does not have a text frame, please check the element id and type of element."
for para in shape.text_frame.paragraphs:
if para.idx == paragraph_id:
para.text = text
shape._closures["replace"].append(
Closure(
partial(replace_para, para.real_idx, text),
para.real_idx,
)
)
return
else:
raise IndexError(
f"Cannot find the paragraph {paragraph_id} of the element {div_id},"
"Please: "
"1. check if you refer to a non-existed paragraph."
"2. check if you already deleted it."
)
def replace_image(slide: SlidePage, img_id: int, image_path: str):
"""
Replace an image in a slide.
Args:
slide (SlidePage): The slide containing the image.
img_id (int): The ID of the image to replace.
image_path (str): The path to the new image.
Raises:
ValueError: If the image path does not exist.
"""
if not os.path.exists(image_path):
raise ValueError(
f"The image {image_path} does not exist, consider use del_image if image_path in the given command is faked"
)
shape = element_index(slide, img_id)
assert isinstance(shape, Picture), "The element is not a Picture."
img_size = PIL.Image.open(image_path).size
r = min(shape.width / img_size[0], shape.height / img_size[1])
new_width = int(img_size[0] * r)
new_height = int(img_size[1] * r)
shape.width = Pt(new_width)
shape.height = Pt(new_height)
shape.img_path = image_path
def clone_paragraph(slide: SlidePage, div_id: int, paragraph_id: int):
"""
Clone a paragraph in a slide.
Args:
slide (SlidePage): The slide containing the paragraph.
div_id (int): The ID of the division containing the paragraph.
paragraph_id (int): The ID of the paragraph to clone.
Raises:
IndexError: If the paragraph is not found.
Mention: the cloned paragraph will have a paragraph_id one greater than the current maximum in the parent element.
"""
shape = element_index(slide, div_id)
assert (
shape.text_frame.is_textframe
), "The element does not have a text frame, please check the element id and type of element."
max_idx = max([para.idx for para in shape.text_frame.paragraphs])
for para in shape.text_frame.paragraphs:
if para.idx != paragraph_id:
continue
shape.text_frame.paragraphs.append(deepcopy(para))
shape.text_frame.paragraphs[-1].idx = max_idx + 1
shape.text_frame.paragraphs[-1].real_idx = len(shape.text_frame.paragraphs) - 1
shape._closures["clone"].append(
Closure(
partial(clone_para, para.real_idx),
para.real_idx,
)
)
return
raise IndexError(
f"Cannot find the paragraph {paragraph_id} of the element {div_id}, may refer to a non-existed paragraph."
)
class API_TYPES(Enum):
Agent = [
replace_image,
del_image,
clone_paragraph,
replace_paragraph,
del_paragraph,
]
@classmethod
def all_funcs(cls) -> dict[str, callable]:
funcs = {}
for attr in dir(cls):
if attr.startswith("__"):
continue
funcs |= {func.__name__: func for func in getattr(cls, attr).value}
return funcs
if __name__ == "__main__":
print(CodeExecutor(0).get_apis_docs(API_TYPES.Agent.value))
|