Skip to content

Commit 68c7750

Browse files
committed
Added CommandSet.on_unregistered()
1 parent 72fc6bf commit 68c7750

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
## 1.3.9 (September 01, 2020)
1+
## 1.3.9 (September 03, 2020)
2+
* Breaking Changes
3+
* `CommandSet.on_unregister()` is now called as first step in unregistering a `CommandSet` and not
4+
the last. `CommandSet.on_unregistered()` is now the last step.
25
* Enhancements
3-
* Added `on_registered()` callback to `CommandSet` class. This is called by `cmd2.Cmd` after a
4-
`CommandSet` is registered and all its commands have been added to the CLI.
6+
* Added `CommandSet.on_registered()`. This is called by `cmd2.Cmd` after a `CommandSet` is registered
7+
and all its commands have been added to the CLI.
8+
* Added `CommandSet.on_unregistered()`. This is called by `cmd2.Cmd` after a `CommandSet` is unregistered
9+
and all its commands have been removed from the CLI.
510

611
## 1.3.8 (August 28, 2020)
712
* Bug Fixes

cmd2/cmd2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,15 @@ def register_command_set(self, cmdset: CommandSet) -> None:
505505
self._register_subcommands(cmdset)
506506
cmdset.on_registered()
507507
except Exception:
508+
cmdset.on_unregister()
508509
for attrib in installed_attributes:
509510
delattr(self, attrib)
510511
if cmdset in self._installed_command_sets:
511512
self._installed_command_sets.remove(cmdset)
512513
if cmdset in self._cmd_to_command_sets.values():
513514
self._cmd_to_command_sets = \
514515
{key: val for key, val in self._cmd_to_command_sets.items() if val is not cmdset}
515-
cmdset.on_unregister()
516+
cmdset.on_unregistered()
516517
raise
517518

518519
def _install_command_function(self, command: str, command_wrapper: Callable, context=''):
@@ -560,6 +561,7 @@ def unregister_command_set(self, cmdset: CommandSet):
560561
"""
561562
if cmdset in self._installed_command_sets:
562563
self._check_uninstallable(cmdset)
564+
cmdset.on_unregister()
563565
self._unregister_subcommands(cmdset)
564566

565567
methods = inspect.getmembers(
@@ -585,7 +587,7 @@ def unregister_command_set(self, cmdset: CommandSet):
585587
if hasattr(self, HELP_FUNC_PREFIX + cmd_name):
586588
delattr(self, HELP_FUNC_PREFIX + cmd_name)
587589

588-
cmdset.on_unregister()
590+
cmdset.on_unregistered()
589591
self._installed_command_sets.remove(cmdset)
590592

591593
def _check_uninstallable(self, cmdset: CommandSet):

cmd2/command_definition.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ def __init__(self):
5555

5656
def on_register(self, cmd) -> None:
5757
"""
58-
Called by cmd2.Cmd when a CommandSet is registered. Subclasses can override this
59-
to perform an initialization requiring access to the Cmd object.
58+
Called by cmd2.Cmd as the first step to registering a CommandSet. The commands defined in this class have
59+
not be added to the CLI object at this point. Subclasses can override this to perform any initialization
60+
requiring access to the Cmd object (e.g. configure commands and their parsers based on CLI state data).
6061
6162
:param cmd: The cmd2 main application
6263
:type cmd: cmd2.Cmd
@@ -68,16 +69,22 @@ def on_register(self, cmd) -> None:
6869

6970
def on_registered(self) -> None:
7071
"""
71-
Called by cmd2.Cmd after a CommandSet is registered and all its commands have been added
72-
to the CLI. Subclasses can override this to perform custom steps.
72+
Called by cmd2.Cmd after a CommandSet is registered and all its commands have been added to the CLI.
73+
Subclasses can override this to perform custom steps related to the newly added commands (e.g. setting
74+
them to a disabled state).
7375
"""
7476
pass
7577

7678
def on_unregister(self) -> None:
7779
"""
78-
Called by ``cmd2.Cmd`` when a CommandSet is unregistered and removed.
80+
Called by ``cmd2.Cmd`` as the first step to unregistering a CommandSet. Subclasses can override this to
81+
perform any cleanup steps which require their commands being registered in the CLI.
82+
"""
83+
pass
7984

80-
:param cmd:
81-
:type cmd: cmd2.Cmd
85+
def on_unregistered(self) -> None:
86+
"""
87+
Called by ``cmd2.Cmd`` after a CommandSet has been unregistered and all its commands removed from the CLI.
88+
Subclasses can override this to perform remaining cleanup steps.
8289
"""
8390
self._cmd = None

tests_isolated/test_commandset/test_commandset.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ def on_registered(self) -> None:
3030
print("in on_registered now")
3131

3232
def on_unregister(self) -> None:
33-
print("in on_unregister now")
3433
super().on_unregister()
34+
print("in on_unregister now")
35+
36+
def on_unregistered(self) -> None:
37+
super().on_unregistered()
38+
print("in on_unregistered now")
3539

3640
def do_apple(self, statement: cmd2.Statement):
3741
self._cmd.poutput('Apple!')
@@ -209,9 +213,10 @@ def test_load_commands(command_sets_manual, capsys):
209213
assert 'Alone' not in cmds_cats
210214
assert 'Fruits' not in cmds_cats
211215

212-
# Make sure unregistration callback ran
216+
# Make sure unregistration callbacks ran
213217
out, err = capsys.readouterr()
214218
assert "in on_unregister now" in out
219+
assert "in on_unregistered now" in out
215220

216221
# uninstall a second time and verify no errors happen
217222
command_sets_manual.unregister_command_set(cmd_set)

0 commit comments

Comments
 (0)