diff --git a/README.md b/README.md index 79f7adf3aa99516af3d937597c9e5d786fa174a8..4ea7f57e67944aa7e95b7452406f973be853d01c 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,11 @@ When VAPython is installed, a list of examples can be shown using the example ru ```bash python -m vapython.examples ``` -By entering the respective number, one of the examples can be selected and executed. +By entering the respective number, one of the examples can be selected and opened. +Note, that this will use the default application for `.py` files that is registered with your operating system. +This might not be a text editor. -Alternatively, a specific example can be run using the respective name: +Alternatively, a specific example can directly be executed using the respective name: ```bash python -m vapython.examples.<example_name> ``` diff --git a/scripts/templates/wrapper.py.j2 b/scripts/templates/wrapper.py.j2 index 8e27f4ef779592f178e083e3e6de97f06c7ed54a..86cc0805f68d315bd5d9fbdbc77931dd43a4172c 100644 --- a/scripts/templates/wrapper.py.j2 +++ b/scripts/templates/wrapper.py.j2 @@ -61,6 +61,14 @@ class VAInterface: self._event_thread = Thread(target=background_event_tread, args=(self._loop,) , daemon=True) self._event_thread.start() + try: + self._get_state() + except ConnectionError as e: + print(f"Could not connect to VA server. Error: {e}") + print("Please make sure the VA server is running and accessible.") + self.disconnect() + exit(1) + self._connected = True if add_event_handling: diff --git a/src/vapython/examples/__main__.py b/src/vapython/examples/__main__.py index fb80e23cece5001495879a56f6743bbed10addef..887b6c53cbccac43f33613f89b0c8a536bd3fe54 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__": diff --git a/tests/conftest.py b/tests/conftest.py index d4e8e062222b2fbedd0f63832b16663539688689..de967f1de753b1543965b88b1a238416dbdb76c1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,6 +18,7 @@ async def mocked_connection(session_mocker): async with ChannelFor([service]) as channel: session_mocker.patch("vapython.vanet._va_interface.Channel.__new__", return_value=channel) va = vapy.VA() + session_mocker.patch.object(va, "_get_state", autospec=True) va.connect(add_event_handling=False) yield va, service