Skip to content

Commit a479fa9

Browse files
committed
Defer five imports
- atexit - codecs - signal - tempfile - copy
1 parent 22cd9c3 commit a479fa9

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

cmd2/cmd2.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@
2424
"""
2525
# many imports are lazy-loaded when they are needed
2626
import argparse
27-
import atexit
2827
import cmd
29-
import codecs
3028
import collections
31-
import copy
3229
import datetime
3330
import functools
3431
import glob
@@ -38,10 +35,8 @@
3835
import platform
3936
import re
4037
import shlex
41-
import signal
4238
import subprocess
4339
import sys
44-
import tempfile
4540
import traceback
4641
from typing import Callable, List, Optional, Union, Tuple
4742

@@ -524,6 +519,7 @@ def complete_submenu(_self, text, line, begidx, endidx):
524519
self._copy_out_shared_attrs(_self, original_attributes)
525520

526521
# Pass the submenu's tab completion parameters back up to the menu that called complete()
522+
import copy
527523
_self.allow_appended_space = submenu.allow_appended_space
528524
_self.allow_closing_quote = submenu.allow_closing_quote
529525
_self.display_matches = copy.copy(submenu.display_matches)
@@ -661,6 +657,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
661657
readline.set_history_length(persistent_history_length)
662658
except FileNotFoundError:
663659
pass
660+
import atexit
664661
atexit.register(readline.write_history_file, persistent_history_file)
665662

666663
# Call super class constructor
@@ -935,6 +932,7 @@ def tokens_for_completion(self, line, begidx, endidx):
935932
On Failure
936933
Both items are None
937934
"""
935+
import copy
938936
unclosed_quote = ''
939937
quotes_to_try = copy.copy(constants.QUOTES)
940938

@@ -1677,6 +1675,7 @@ def complete(self, text, state):
16771675
# Since self.display_matches is empty, set it to self.completion_matches
16781676
# before we alter them. That way the suggestions will reflect how we parsed
16791677
# the token being completed and not how readline did.
1678+
import copy
16801679
self.display_matches = copy.copy(self.completion_matches)
16811680

16821681
# Check if we need to add an opening quote
@@ -1848,7 +1847,7 @@ def sigint_handler(self, signum, frame):
18481847

18491848
def preloop(self):
18501849
""""Hook method executed once when the cmdloop() method is called."""
1851-
1850+
import signal
18521851
# Register a default SIGINT signal handler for Ctrl+C
18531852
signal.signal(signal.SIGINT, self.sigint_handler)
18541853

@@ -2083,6 +2082,7 @@ def _redirect_output(self, statement):
20832082
# Re-raise the exception
20842083
raise ex
20852084
elif statement.output:
2085+
import tempfile
20862086
if (not statement.output_to) and (not can_clip):
20872087
raise EnvironmentError('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable')
20882088
self.kept_state = Statekeeper(self, ('stdout',))
@@ -2927,6 +2927,7 @@ def do_history(self, args):
29272927
if runme:
29282928
self.onecmd_plus_hooks(runme)
29292929
elif args.edit:
2930+
import tempfile
29302931
fd, fname = tempfile.mkstemp(suffix='.txt', text=True)
29312932
with os.fdopen(fd, 'w') as fobj:
29322933
for command in history:
@@ -3138,6 +3139,8 @@ def is_text_file(file_path):
31383139
Returns if a file contains only ASCII or UTF-8 encoded text
31393140
:param file_path: path to the file being checked
31403141
"""
3142+
import codecs
3143+
31413144
expanded_path = os.path.abspath(os.path.expanduser(file_path.strip()))
31423145
valid_text_file = False
31433146

0 commit comments

Comments
 (0)