From 06fabe0ace64204b3b223a16b81d0c01ae272466 Mon Sep 17 00:00:00 2001 From: Pascal Palenda <pascal.palenda@akustik.rwth-aachen.de> Date: Mon, 25 Nov 2024 10:22:28 +0100 Subject: [PATCH] Feat: open the examples instead of running them --- src/vapython/examples/__main__.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/vapython/examples/__main__.py b/src/vapython/examples/__main__.py index fb80e23..887b6c5 100644 --- a/src/vapython/examples/__main__.py +++ b/src/vapython/examples/__main__.py @@ -1,16 +1,16 @@ -import importlib +import os import sys from pathlib import Path def main(): """ - Main function to list and execute example scripts. + Main function to list and open example scripts. This function performs the following steps: 1. Lists all Python files in the current directory that contain "example" in their name. 2. If no command-line arguments are provided, it prints the list of example files and prompts the user to select one by index. - 3. If a command-line argument is provided, it uses that as the index of the example file to run. - 4. Validates the selected index and either exits, prints an error message, or imports and runs the selected example's main function. + 3. If a command-line argument is provided, it uses that as the index of the example file to open. + 4. Validates the selected index and either exits, prints an error message, or opens the file. Returns: None """ @@ -25,7 +25,7 @@ def main(): print(f"{len(example_files)}: Exit") - example_index = int(input("Enter the index of the example you want to run: ")) + example_index = int(input("Enter the index of the example you want to open: ")) else: example_index = int(sys.argv[1]) @@ -37,14 +37,7 @@ def main(): return example_file = example_files[example_index] - module_name = f"vapython.examples.{example_file.stem}" - module = importlib.import_module(module_name) - - example_main = getattr(module, "main", None) - if example_main is not None: - example_main() - else: - print(f"Could not find 'main' function in module '{module_name}'") + os.startfile(example_file) # noqa: S606 if __name__ == "__main__": -- GitLab