|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -from typing import Any, List |
| 15 | +from typing import Any, Callable, List |
16 | 16 |
|
17 | 17 | from launch.frontend.parser import Parser |
18 | 18 | from launch.launch_description import LaunchDescription |
19 | 19 |
|
20 | | -from . import actions |
21 | | -from .entity import Entity |
| 20 | +from .entity import Entity, make_valid_name |
| 21 | + |
| 22 | +__all__ = [ |
| 23 | + 'launch', |
| 24 | +] |
22 | 25 |
|
23 | 26 |
|
24 | 27 | def launch(actions: List[Entity]) -> LaunchDescription: |
| 28 | + """Entrypoint of the launchfile, produces a LaunchDescription containing the listed entities.""" |
25 | 29 | parser = Parser() |
26 | 30 | root_entity = Entity('launch', children=actions) |
27 | 31 | return parser.parse_description(root_entity) |
28 | 32 |
|
29 | 33 |
|
| 34 | +def __make_action_factory(action_name: str) -> Callable[..., Any]: |
| 35 | + """Create a factory function that constructs an Entity of the given action type.""" |
| 36 | + action_name = make_valid_name(action_name) |
| 37 | + |
| 38 | + def fn(*args, **kwargs): |
| 39 | + return Entity(action_name, *args, **kwargs) |
| 40 | + |
| 41 | + fn.__doc__ = f'launch_frontend_py action: {action_name} (dynamically generated)' |
| 42 | + fn.__name__ = action_name |
| 43 | + fn.__qualname__ = action_name |
| 44 | + fn.__module__ = __name__ |
| 45 | + globals()[action_name] = fn |
| 46 | + __all__.append(action_name) |
| 47 | + return fn |
| 48 | + |
| 49 | + |
30 | 50 | def __getattr__(name: str) -> Any: |
31 | | - """Forward attribute access to the dynamic actions module.""" |
| 51 | + """ |
| 52 | + Dynamically create action factory functions on demand. |
| 53 | +
|
| 54 | + This is called in `from actions import <name>`, `getattr(actions, <name>)`, or `actions.<name>` |
| 55 | + """ |
32 | 56 | import importlib |
| 57 | + from launch.frontend import Parser |
| 58 | + from launch.frontend.expose import action_parse_methods |
33 | 59 |
|
34 | 60 | if name in __all__: |
| 61 | + # It's already been loaded, just return it |
35 | 62 | return importlib.import_module(f'.{name}', __name__) |
36 | 63 |
|
37 | | - return getattr(actions, name) |
| 64 | + # Not loaded here yet, perhaps in an extension that wasn't imported yet |
| 65 | + Parser.load_launch_extensions() |
38 | 66 |
|
| 67 | + if name in action_parse_methods: |
| 68 | + return __make_action_factory(name) |
| 69 | + elif name.endswith('_'): |
| 70 | + # The name has a trailing underscore, which we add to actions with reserved Python names |
| 71 | + # Try again without the underscore |
| 72 | + __getattr__(name[:-1]) |
| 73 | + else: |
| 74 | + # It's not registered, raise the usual error |
| 75 | + msg = f'module {__name__} has no attribute "{name}"' |
| 76 | + if base_name != name: |
| 77 | + msg += ' (or "{base_name}")' |
| 78 | + raise AttributeError(msg) |
39 | 79 |
|
40 | | -__all__ = [ |
41 | | - 'launch', |
42 | | -] |
| 80 | + |
| 81 | +def __preseed_all_actions() -> None: |
| 82 | + """ |
| 83 | + Pre-seed all available actions into this module. |
| 84 | +
|
| 85 | + This preloads all launch extensions and fills __all__ with available actions. |
| 86 | +
|
| 87 | + This is not necessary, since __getattr__ loads actions on demand, |
| 88 | + but it allows tools and users to see all available actions in __all__. |
| 89 | + """ |
| 90 | + from launch.frontend.expose import action_parse_methods |
| 91 | + from launch.frontend import Parser |
| 92 | + Parser.load_launch_extensions() |
| 93 | + |
| 94 | + action_names = list(action_parse_methods.keys()) |
| 95 | + for action_name in action_names: |
| 96 | + __getattr__(action_name) |
| 97 | + |
| 98 | +__preseed_all_actions() |
0 commit comments