(self, cmdset: CommandSet[Any])
| 1047 | self._installed_command_sets.remove(cmdset) |
| 1048 | |
| 1049 | def _check_uninstallable(self, cmdset: CommandSet[Any]) -> None: |
| 1050 | cmdset_id = id(cmdset) |
| 1051 | |
| 1052 | def check_parser_uninstallable(parser: Cmd2ArgumentParser) -> None: |
| 1053 | try: |
| 1054 | subparsers_action = parser.get_subparsers_action() |
| 1055 | except ValueError: |
| 1056 | # No subcommands to check |
| 1057 | return |
| 1058 | |
| 1059 | # Prevent redundant traversal of parser aliases |
| 1060 | checked_parsers: set[Cmd2ArgumentParser] = set() |
| 1061 | |
| 1062 | for subparser in subparsers_action.choices.values(): |
| 1063 | if subparser in checked_parsers: |
| 1064 | continue |
| 1065 | checked_parsers.add(subparser) |
| 1066 | |
| 1067 | attached_cmdset_id = getattr(subparser, constants.PARSER_ATTR_COMMANDSET_ID, None) |
| 1068 | if attached_cmdset_id is not None and attached_cmdset_id != cmdset_id: |
| 1069 | raise CommandSetRegistrationError( |
| 1070 | f"Cannot uninstall CommandSet: '{subparser.prog}' is required by another CommandSet" |
| 1071 | ) |
| 1072 | check_parser_uninstallable(subparser) |
| 1073 | |
| 1074 | methods: list[tuple[str, Callable[..., Any]]] = inspect.getmembers( |
| 1075 | cmdset, |
| 1076 | predicate=lambda meth: ( # type: ignore[arg-type] |
| 1077 | isinstance(meth, Callable) # type: ignore[arg-type] |
| 1078 | and hasattr(meth, "__name__") |
| 1079 | and meth.__name__.startswith(COMMAND_FUNC_PREFIX) |
| 1080 | ), |
| 1081 | ) |
| 1082 | |
| 1083 | for cmd_func_name, command_method in methods: |
| 1084 | # We only need to check if it's safe to remove the parser if this |
| 1085 | # is the actual command since command synonyms don't own it. |
| 1086 | if cmd_func_name == command_method.__name__: |
| 1087 | command_parser = self.command_parsers.get(command_method) |
| 1088 | if command_parser is not None: |
| 1089 | check_parser_uninstallable(command_parser) |
| 1090 | |
| 1091 | def _register_subcommands(self, cmdset: CmdOrSet) -> None: |
| 1092 | """Register subcommands with their base command. |
no test coverage detected