Skip to content

Commit

Permalink
refactor(configuration.py): Shift the code to a module level function1
Browse files Browse the repository at this point in the history
  • Loading branch information
gutsytechster committed Jul 24, 2020
1 parent 87b1011 commit b163b7e
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/pip/_internal/commands/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shutil
import subprocess

from pip._vendor.six import PY3
from pip._vendor.six import PY2

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
Expand All @@ -27,6 +27,19 @@
logger = logging.getLogger(__name__)


def parse_editor_value(editor):
# type: (str) -> List[str]

if PY2:
return [editor]
result = shutil.which(editor)
if result:
return [result]
if os.path.exists(editor):
return [editor]
return shlex.split(editor)


class ConfigurationCommand(Command):
"""
Manage local and global configuration.
Expand Down Expand Up @@ -235,38 +248,20 @@ def print_env_var_values(self):
def open_in_editor(self, options, args):
# type: (Values, List[str]) -> None
editor = self._determine_editor(options)
editor_command = parse_editor_value(editor)

fname = self.configuration.get_file_to_edit()
if fname is None:
raise PipError("Could not determine appropriate file.")

try:
if PY3:
# Since Python2 doesn't support shutil.which(), we only support
# editor args for Python 3.
args = self._get_editor_cmd(editor)
else:
args = [editor]
args.append(fname)
subprocess.check_call(args)
subprocess.check_call(editor_command + [fname])
except subprocess.CalledProcessError as e:
raise PipError(
"Editor Subprocess exited with exit code {}"
.format(e.returncode)
)

def _get_editor_cmd(self, editor):
# type: (str) -> List[str]

# Currently mypy fails for Python2 as it could not recognise the
# shutil.which()
result = shutil.which(editor) # type: ignore
if result:
return [result]
if os.path.exists(editor):
return [editor]
return shlex.split(editor)

def _get_n_args(self, args, example, n):
# type: (List[str], str, int) -> Any
"""Helper to make sure the command got the right number of arguments
Expand Down

0 comments on commit b163b7e

Please sign in to comment.