move contstants and regular expressions to LazyObject

This commit is contained in:
Bernardas 2016-07-10 19:13:21 +00:00
parent a16c9f936f
commit a219ad4bb2

View file

@ -1,20 +1,30 @@
import re
import subprocess
from xonsh.lazyasd import LazyObject
PIP_RE = LazyObject(lambda: re.compile("pip(?:\d|\.)*"),
globals(), 'PIP_RE')
PIP_LIST_RE = LazyObject(lambda: re.compile("pip(?:\d|\.)* (?:uninstall|show)"),
globals(), 'PIP_LIST_RE')
def get_pip_commands():
help_text = str(subprocess.check_output(['pip', '--help'], stderr=subprocess.DEVNULL))
commands = re.findall(" (\w+) ", help_text)
return [c for c in commands if c not in ['completion', 'help']]
ALL_COMMANDS = LazyObject(lambda: get_pip_commands(),
globals(), 'ALL_COMMANDS')
def complete_pip(prefix, line, begidx, endidx, ctx):
"""
Completes python's package manager pip
"""
if not re.search("pip(?:\d|\.)*", line):
if not PIP_RE.search(line):
return
if re.search("pip(?:\d|\.)* (?:uninstall|show)", line):
if PIP_LIST_RE.search(line):
items = subprocess.check_output(['pip', 'list'], stderr=subprocess.DEVNULL)
items = items.decode('utf-8').splitlines()
return set(i.split()[0] for i in items)
@ -23,11 +33,10 @@ def complete_pip(prefix, line, begidx, endidx, ctx):
# "pip show " -> no complete (note space)
return
all_commands = get_pip_commands()
if prefix in all_commands:
if prefix in ALL_COMMANDS:
# "pip show" -> suggest replacing new with other command (note no space)
return all_commands, len(prefix)
return ALL_COMMANDS, len(prefix)
elif prefix:
# "pip sh" -> suggest "show"
return [c for c in all_commands if c.startswith(prefix)], len(prefix)
return set(all_commands)
return [c for c in ALL_COMMANDS if c.startswith(prefix)], len(prefix)
return set(ALL_COMMANDS)