Skip to content

Commit 38f070a

Browse files
authored
Merge pull request #540 from python-cmd2/delete_deprecated_hooks
Deleted the hook methods which were deprecated in the previous release
2 parents c0f283b + fcfa8ed commit 38f070a

File tree

6 files changed

+42
-117
lines changed

6 files changed

+42
-117
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
for formatting help/description text
1010
* Aliases are now sorted alphabetically
1111
* The **set** command now tab-completes settable parameter names
12+
* Deletions
13+
* The ``preparse``, ``postparsing_precmd``, and ``postparsing_postcmd`` methods *deprecated* in the previous release
14+
have been deleted
15+
* The new application lifecycle hook system allows for registration of callbacks to be called at various points
16+
in the lifecycle and is more powerful and flexible than the previous system
1217

1318
## 0.9.4 (August 21, 2018)
1419
* Bug Fixes

cmd2/cmd2.py

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,58 +1649,6 @@ def precmd(self, statement: Statement) -> Statement:
16491649
"""
16501650
return statement
16511651

1652-
# ----- Methods which are cmd2-specific lifecycle hooks which are not present in cmd -----
1653-
1654-
# noinspection PyMethodMayBeStatic
1655-
def preparse(self, raw: str) -> str:
1656-
"""Hook method executed before user input is parsed.
1657-
1658-
WARNING: If it's a multiline command, `preparse()` may not get all the
1659-
user input. _complete_statement() really does two things: a) parse the
1660-
user input, and b) accept more input in case it's a multiline command
1661-
the passed string doesn't have a terminator. `preparse()` is currently
1662-
called before we know whether it's a multiline command, and before we
1663-
know whether the user input includes a termination character.
1664-
1665-
If you want a reliable pre parsing hook method, register a postparsing
1666-
hook, modify the user input, and then reparse it.
1667-
1668-
:param raw: raw command line input :return: potentially modified raw command line input
1669-
:return: a potentially modified version of the raw input string
1670-
"""
1671-
return raw
1672-
1673-
# noinspection PyMethodMayBeStatic
1674-
def postparsing_precmd(self, statement: Statement) -> Tuple[bool, Statement]:
1675-
"""This runs after parsing the command-line, but before anything else; even before adding cmd to history.
1676-
1677-
NOTE: This runs before precmd() and prior to any potential output redirection or piping.
1678-
1679-
If you wish to fatally fail this command and exit the application entirely, set stop = True.
1680-
1681-
If you wish to just fail this command you can do so by raising an exception:
1682-
1683-
- raise EmptyStatement - will silently fail and do nothing
1684-
- raise <AnyOtherException> - will fail and print an error message
1685-
1686-
:param statement: the parsed command-line statement as a Statement object
1687-
:return: (stop, statement) containing a potentially modified version of the statement object
1688-
"""
1689-
stop = False
1690-
return stop, statement
1691-
1692-
# noinspection PyMethodMayBeStatic
1693-
def postparsing_postcmd(self, stop: bool) -> bool:
1694-
"""This runs after everything else, including after postcmd().
1695-
1696-
It even runs when an empty line is entered. Thus, if you need to do something like update the prompt due
1697-
to notifications from a background thread, then this is the method you want to override to do it.
1698-
1699-
:param stop: True implies the entire application should exit.
1700-
:return: True implies the entire application should exit.
1701-
"""
1702-
return stop
1703-
17041652
def parseline(self, line: str) -> Tuple[str, str, str]:
17051653
"""Parse the line into a command name and a string containing the arguments.
17061654
@@ -1740,9 +1688,6 @@ def onecmd_plus_hooks(self, line: str) -> bool:
17401688
data = func(data)
17411689
if data.stop:
17421690
break
1743-
# postparsing_precmd is deprecated
1744-
if not data.stop:
1745-
(data.stop, data.statement) = self.postparsing_precmd(data.statement)
17461691
# unpack the data object
17471692
statement = data.statement
17481693
stop = data.stop
@@ -1807,9 +1752,7 @@ def _run_cmdfinalization_hooks(self, stop: bool, statement: Optional[Statement])
18071752
data = func(data)
18081753
# retrieve the final value of stop, ignoring any
18091754
# modifications to the statement
1810-
stop = data.stop
1811-
# postparsing_postcmd is deprecated
1812-
return self.postparsing_postcmd(stop)
1755+
return data.stop
18131756
except Exception as ex:
18141757
self.perror(ex)
18151758

@@ -1863,9 +1806,6 @@ def _complete_statement(self, line: str) -> Statement:
18631806
pipe runs out. We can't refactor it because we need to retain
18641807
backwards compatibility with the standard library version of cmd.
18651808
"""
1866-
# preparse() is deprecated, use self.register_postparsing_hook() instead
1867-
line = self.preparse(line)
1868-
18691809
while True:
18701810
try:
18711811
statement = self.statement_parser.parse(line)

docs/hooks.rst

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,21 @@ Command Processing Loop
7676
When you call `.cmdloop()`, the following sequence of events are repeated until
7777
the application exits:
7878

79-
1. Output the prompt
80-
2. Accept user input
81-
3. Call `preparse()` - for backwards compatibility with prior releases of cmd2, now deprecated
82-
4. Parse user input into `Statement` object
83-
5. Call methods registered with `register_postparsing_hook()`
84-
6. Call `postparsing_precmd()` - for backwards compatibility with prior releases of cmd2, now deprecated
85-
7. Redirect output, if user asked for it and it's allowed
86-
8. Start timer
87-
9. Call methods registered with `register_precmd_hook()`
88-
10. Call `precmd()` - for backwards compatibility with ``cmd.Cmd``
89-
11. Add statement to history
90-
12. Call `do_command` method
91-
13. Call methods registered with `register_postcmd_hook()`
92-
14. Call `postcmd(stop, statement)` - for backwards compatibility with ``cmd.Cmd``
93-
15. Stop timer and display the elapsed time
94-
16. Stop redirecting output if it was redirected
95-
17. Call methods registered with `register_cmdfinalization_hook()`
96-
18. Call `postparsing_postcmd()` - for backwards compatibility - deprecated
79+
#. Output the prompt
80+
#. Accept user input
81+
#. Parse user input into `Statement` object
82+
#. Call methods registered with `register_postparsing_hook()`
83+
#. Redirect output, if user asked for it and it's allowed
84+
#. Start timer
85+
#. Call methods registered with `register_precmd_hook()`
86+
#. Call `precmd()` - for backwards compatibility with ``cmd.Cmd``
87+
#. Add statement to history
88+
#. Call `do_command` method
89+
#. Call methods registered with `register_postcmd_hook()`
90+
#. Call `postcmd(stop, statement)` - for backwards compatibility with ``cmd.Cmd``
91+
#. Stop timer and display the elapsed time
92+
#. Stop redirecting output if it was redirected
93+
#. Call methods registered with `register_cmdfinalization_hook()`
9794

9895
By registering hook methods, steps 4, 8, 12, and 16 allow you to run code
9996
during, and control the flow of the command processing loop. Be aware that
@@ -305,21 +302,3 @@ If any command finalization hook raises an exception, no more command
305302
finalization hooks will be called. If the last hook to return a value returned
306303
``True``, then the exception will be rendered, and the application will
307304
terminate.
308-
309-
Deprecated Command Processing Hooks
310-
-----------------------------------
311-
312-
Inside the main loop, every time the user hits <Enter> the line is processed by the ``onecmd_plus_hooks`` method.
313-
314-
.. automethod:: cmd2.cmd2.Cmd.onecmd_plus_hooks
315-
316-
As the ``onecmd_plus_hooks`` name implies, there are a number of *hook* methods that can be defined in order to inject
317-
application-specific behavior at various points during the processing of a line of text entered by the user. ``cmd2``
318-
increases the 2 hooks provided by ``cmd`` (**precmd** and **postcmd**) to 6 for greater flexibility. Here are
319-
the various hook methods, presented in chronological order starting with the ones called earliest in the process.
320-
321-
.. automethod:: cmd2.cmd2.Cmd.preparse
322-
323-
.. automethod:: cmd2.cmd2.Cmd.postparsing_precmd
324-
325-
.. automethod:: cmd2.cmd2.Cmd.postparsing_postcmd

docs/integrating.rst

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,19 @@ script file.
135135
The **onecmd_plus_hooks()** method will do the following to execute a single
136136
``cmd2`` command in a normal fashion:
137137

138-
1. Call `preparse()` - for backwards compatibility with prior releases of cmd2, now deprecated
139-
2. Parse user input into `Statement` object
140-
3. Call methods registered with `register_postparsing_hook()`
141-
4. Call `postparsing_precmd()` - for backwards compatibility with prior releases of cmd2, now deprecated
142-
5. Redirect output, if user asked for it and it's allowed
143-
6. Start timer
144-
7. Call methods registered with `register_precmd_hook()`
145-
8. Call `precmd()` - for backwards compatibility with ``cmd.Cmd``
146-
9. Add statement to history
147-
10. Call `do_command` method
148-
11. Call methods registered with `register_postcmd_hook()`
149-
12. Call `postcmd(stop, statement)` - for backwards compatibility with ``cmd.Cmd``
150-
13. Stop timer and display the elapsed time
151-
14. Stop redirecting output if it was redirected
152-
15. Call methods registered with `register_cmdfinalization_hook()`
153-
16. Call `postparsing_postcmd()` - for backwards compatibility - deprecated
138+
#. Parse user input into `Statement` object
139+
#. Call methods registered with `register_postparsing_hook()`
140+
#. Redirect output, if user asked for it and it's allowed
141+
#. Start timer
142+
#. Call methods registered with `register_precmd_hook()`
143+
#. Call `precmd()` - for backwards compatibility with ``cmd.Cmd``
144+
#. Add statement to history
145+
#. Call `do_command` method
146+
#. Call methods registered with `register_postcmd_hook()`
147+
#. Call `postcmd(stop, statement)` - for backwards compatibility with ``cmd.Cmd``
148+
#. Stop timer and display the elapsed time
149+
#. Stop redirecting output if it was redirected
150+
#. Call methods registered with `register_cmdfinalization_hook()`
154151

155152
Running in this fashion enables the ability to integrate with an external event
156153
loop. However, how to integrate with any specific event loop is beyond the

tests/test_cmd2.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,10 +993,13 @@ def test_cmdloop_without_rawinput():
993993
class HookFailureApp(cmd2.Cmd):
994994
def __init__(self, *args, **kwargs):
995995
super().__init__(*args, **kwargs)
996+
# register a postparsing hook method
997+
self.register_postparsing_hook(self.postparsing_precmd)
996998

997-
def postparsing_precmd(self, statement):
999+
def postparsing_precmd(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
9981000
"""Simulate precmd hook failure."""
999-
return True, statement
1001+
data.stop = True
1002+
return data
10001003

10011004
@pytest.fixture
10021005
def hook_failure():

tests/test_plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ def prepost_hook_with_wrong_return_annotation(self) -> bool:
5151
# preparse hook
5252
#
5353
###
54-
def preparse(self, line: str) -> str:
54+
def preparse(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
5555
"""Preparsing hook"""
5656
self.called_preparse += 1
57-
return line
57+
return data
5858

5959
###
6060
#
@@ -322,6 +322,7 @@ def test_postloop_hooks(capsys):
322322
###
323323
def test_preparse(capsys):
324324
app = PluggedApp()
325+
app.register_postparsing_hook(app.preparse)
325326
app.onecmd_plus_hooks('say hello')
326327
out, err = capsys.readouterr()
327328
assert out == 'hello\n'

0 commit comments

Comments
 (0)