-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically identifies the class name based on the specified line number. #2280
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||
from manimlib.scene.scene import Scene | ||||||
|
||||||
from typing import TYPE_CHECKING | ||||||
|
||||||
if TYPE_CHECKING: | ||||||
Module = importlib.util.types.ModuleType | ||||||
from typing import Optional | ||||||
|
@@ -142,35 +143,48 @@ def get_indent(code_lines: list[str], line_number: int) -> str: | |||||
return n_spaces * " " | ||||||
|
||||||
|
||||||
def insert_embed_line_to_module(module: Module, line_number: int): | ||||||
def insert_embed_line_to_module(module: Module, run_config: Dict) -> None: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of passing |
||||||
""" | ||||||
This is hacky, but convenient. When user includes the argument "-e", it will try | ||||||
to recreate a file that inserts the line `self.embed()` into the end of the scene's | ||||||
construct method. If there is an argument passed in, it will insert the line after | ||||||
the last line in the sourcefile which includes that string. | ||||||
""" | ||||||
lines = inspect.getsource(module).splitlines() | ||||||
line_number = run_config.embed_line | ||||||
|
||||||
# Add the relevant embed line to the code | ||||||
indent = get_indent(lines, line_number) | ||||||
lines.insert(line_number, indent + "self.embed()") | ||||||
new_code = "\n".join(lines) | ||||||
|
||||||
# When the user executes the `-e <line_number>` command, | ||||||
# it should automatically identifies the nearest class | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# defined above `<line_number>` as 'scene_names'. | ||||||
classes = list(filter(lambda line: line.startswith("class"), lines[:line_number])) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about classes that are defined as nested class, i.e. inside some other classes? They would have an indentation so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the Scene Class (like: |
||||||
if classes: | ||||||
from re import search | ||||||
|
||||||
name_search = search(r"(\w+)\(", classes[-1]) | ||||||
run_config.update(scene_names=[name_search.group(1)]) | ||||||
else: | ||||||
log.error(f"No 'class' has been found above {line_number}!") | ||||||
|
||||||
# Execute the code, which presumably redefines the user's | ||||||
# scene to include this embed line, within the relevant module. | ||||||
code_object = compile(new_code, module.__name__, 'exec') | ||||||
exec(code_object, module.__dict__) | ||||||
|
||||||
|
||||||
def get_module(file_name: Optional[str], embed_line: Optional[int], is_reload: bool = False) -> Module: | ||||||
module = ModuleLoader.get_module(file_name, is_reload) | ||||||
if embed_line: | ||||||
insert_embed_line_to_module(module, embed_line) | ||||||
def get_module(run_config: Dict) -> Module: | ||||||
module = ModuleLoader.get_module(run_config.file_name, run_config.is_reload) | ||||||
if run_config.embed_line: | ||||||
insert_embed_line_to_module(module, run_config) | ||||||
return module | ||||||
|
||||||
|
||||||
def main(scene_config: Dict, run_config: Dict): | ||||||
module = get_module(run_config.file_name, run_config.embed_line, run_config.is_reload) | ||||||
module = get_module(run_config) | ||||||
all_scene_classes = get_scene_classes(module) | ||||||
scenes = get_scenes_to_render(all_scene_classes, scene_config, run_config) | ||||||
if len(scenes) == 0: | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the Cambridge dictionary