Defer loading cmd2

We were importing cmd2 purely so we could do some exception
transformation. However, this is only needed if we're in interactive
mode. Avoid both the import of cmd2 and the transformation of the
exceptions unless this is the case. This speeds up import time by ~30%
for the demoapp on my machine (~160mS after compared to ~210mS before)

Change-Id: I2356dc9803b4d0eef3528c6d057207509932e6b2
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2022-05-18 12:02:14 +01:00
parent 98095d406a
commit 6811218817
1 changed files with 6 additions and 3 deletions

View File

@ -20,8 +20,6 @@ import logging.handlers
import os
import sys
import cmd2
from cliff import _argparse
from . import complete
from . import help
@ -403,7 +401,12 @@ class App(object):
try:
parsed_args = cmd_parser.parse_args(sub_argv)
except SystemExit as ex:
raise cmd2.exceptions.Cmd2ArgparseError from ex
if self.interactive_mode:
# Defer importing cmd2 as it is a slow import
import cmd2
raise cmd2.exceptions.Cmd2ArgparseError from ex
else:
raise ex
result = cmd.run(parsed_args)
except BrokenPipeError as err1:
result = _SIGPIPE_EXIT