summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Finucane <sfinucan@redhat.com>2022-05-18 12:02:14 +0100
committerStephen Finucane <sfinucan@redhat.com>2022-05-25 17:09:04 +0100
commit6811218817ccc034161dfe1b14d51eb9edc05579 (patch)
tree01f99b76dad86440cb5d1c6a380f79e4fe19c07b
parent98095d406ac5b363807385404afce207f29e620b (diff)
downloadcliff-6811218817ccc034161dfe1b14d51eb9edc05579.tar.gz
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>
-rw-r--r--cliff/app.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/cliff/app.py b/cliff/app.py
index 798b41f..10a4941 100644
--- a/cliff/app.py
+++ b/cliff/app.py
@@ -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