MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / unregister_command_set

Method unregister_command_set

cmd2/cmd2.py:1004–1047  ·  view source on GitHub ↗

Uninstalls a CommandSet and unloads all associated commands. :param cmdset: CommandSet to uninstall

(self, cmdset: CommandSet[Any])

Source from the content-addressed store, hash-verified

1002 setattr(self, help_func_name, cmd_help)
1003
1004 def unregister_command_set(self, cmdset: CommandSet[Any]) -> None:
1005 """Uninstalls a CommandSet and unloads all associated commands.
1006
1007 :param cmdset: CommandSet to uninstall
1008 """
1009 if cmdset in self._installed_command_sets:
1010 self._check_uninstallable(cmdset)
1011 cmdset.on_unregister()
1012 self._unregister_subcommands(cmdset)
1013
1014 methods: list[tuple[str, Callable[..., Any]]] = inspect.getmembers(
1015 cmdset,
1016 predicate=lambda meth: ( # type: ignore[arg-type]
1017 isinstance(meth, Callable) # type: ignore[arg-type]
1018 and hasattr(meth, "__name__")
1019 and meth.__name__.startswith(COMMAND_FUNC_PREFIX)
1020 ),
1021 )
1022
1023 for cmd_func_name, command_method in methods:
1024 command = cmd_func_name[len(COMMAND_FUNC_PREFIX) :]
1025
1026 # Enable the command before uninstalling it to make sure we remove both
1027 # the real functions and the ones used by the DisabledCommand object.
1028 if command in self.disabled_commands:
1029 self.enable_command(command)
1030
1031 if command in self._cmd_to_command_sets:
1032 del self._cmd_to_command_sets[command]
1033
1034 # Only remove the parser if this is the actual
1035 # command since command synonyms don't own it.
1036 if cmd_func_name == command_method.__name__:
1037 self.command_parsers.remove(command_method)
1038
1039 if hasattr(self, COMPLETER_FUNC_PREFIX + command):
1040 delattr(self, COMPLETER_FUNC_PREFIX + command)
1041 if hasattr(self, HELP_FUNC_PREFIX + command):
1042 delattr(self, HELP_FUNC_PREFIX + command)
1043
1044 delattr(self, cmd_func_name)
1045
1046 cmdset.on_unregistered()
1047 self._installed_command_sets.remove(cmdset)
1048
1049 def _check_uninstallable(self, cmdset: CommandSet[Any]) -> None:
1050 cmdset_id = id(cmdset)

Callers 9

test_command_synonymsFunction · 0.80
test_load_commandsFunction · 0.80
test_subcommandsFunction · 0.80
test_nested_subcommandsFunction · 0.80
test_ns_providerFunction · 0.80
do_unloadMethod · 0.80

Calls 6

_check_uninstallableMethod · 0.95
enable_commandMethod · 0.95
removeMethod · 0.80
on_unregisterMethod · 0.45
on_unregisteredMethod · 0.45

Tested by 8

test_command_synonymsFunction · 0.64
test_load_commandsFunction · 0.64
test_subcommandsFunction · 0.64
test_nested_subcommandsFunction · 0.64
test_ns_providerFunction · 0.64