MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / execute_actions

Method execute_actions

utils/src/apis.py:88–155  ·  view source on GitHub ↗

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 trace

(
        self, actions: str, edit_slide: SlidePage, found_code: bool = False
    )

Source from the content-addressed store, hash-verified

86 return "\n\n".join(api_doc)
87
88 def execute_actions(
89 self, actions: str, edit_slide: SlidePage, found_code: bool = False
90 ) -> Union[tuple[str, str], None]:
91 """
92 Execute a series of actions on a slide.
93
94 Args:
95 actions (str): The actions to execute.
96 edit_slide (SlidePage): The slide to edit.
97 found_code (bool): Whether code was found in the actions.
98
99 Returns:
100 tuple: The API lines and traceback if an error occurs.
101 None: If no error occurs.
102 """
103 api_calls = actions.strip().split("\n")
104 self.api_history.append(
105 [HistoryMark.API_CALL_ERROR, edit_slide.slide_idx, actions]
106 )
107 for line_idx, line in enumerate(api_calls):
108 try:
109 if line_idx == len(api_calls) - 1 and not found_code:
110 raise ValueError(
111 "No code block found in the output, please output the api calls without any prefix."
112 )
113 if line.startswith("def"):
114 raise PermissionError("The function definition were not allowed.")
115 if line.startswith("#"):
116 if len(self.command_history) != 0:
117 self.command_history[-1][0] = HistoryMark.COMMENT_CORRECT
118 self.command_history.append([HistoryMark.COMMENT_ERROR, line, None])
119 continue
120 if not self.function_regex.match(line):
121 continue
122 found_code = True
123 func = line.split("(")[0]
124 if func not in self.registered_functions:
125 raise NameError(f"The function {func} is not defined.")
126 # only one of clone and del can be used in a row
127 if func.startswith("clone") or func.startswith("del"):
128 tag = func.split("_")[0]
129 if (
130 self.command_history[-1][-1] == None
131 or self.command_history[-1][-1] == tag
132 ):
133 self.command_history[-1][-1] = tag
134 else:
135 raise ValueError(
136 "Invalid command: Both 'clone_paragraph' and 'del_paragraph'/'del_image' are used within a single command. "
137 "Each command must only perform one of these operations based on the quantity_change."
138 )
139 self.code_history.append([HistoryMark.CODE_RUN_ERROR, line, None])
140 partial_func = partial(self.registered_functions[func], edit_slide)
141 eval(line, {}, {func: partial_func})
142 self.code_history[-1][0] = HistoryMark.CODE_RUN_CORRECT
143 except:
144 trace_msg = traceback.format_exc()
145 if len(self.code_history) != 0:

Callers 4

synergizeMethod · 0.45
rebuild_pptxFunction · 0.45
synergizeMethod · 0.45
synergizeMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected