Move readlines function to powerline.lib.shell

This commit is contained in:
ZyX 2014-06-25 20:59:39 +04:00
parent 7f5c4968c1
commit 1498fc714c
2 changed files with 24 additions and 11 deletions

View File

@ -1,9 +1,15 @@
# vim:fileencoding=utf-8:noet
from __future__ import absolute_import, unicode_literals, division, print_function
from subprocess import Popen, PIPE
from locale import getlocale, getdefaultlocale, LC_MESSAGES
def _get_shell_encoding():
return getlocale(LC_MESSAGES)[1] or getdefaultlocale()[1] or 'utf-8'
def run_cmd(pl, cmd, stdin=None):
try:
p = Popen(cmd, stdout=PIPE, stdin=PIPE)
@ -12,11 +18,26 @@ def run_cmd(pl, cmd, stdin=None):
return None
else:
stdout, err = p.communicate(stdin)
encoding = getlocale(LC_MESSAGES)[1] or getdefaultlocale()[1] or 'utf-8'
stdout = stdout.decode(encoding)
stdout = stdout.decode(_get_shell_encoding())
return stdout.strip()
def asrun(pl, ascript):
'''Run the given AppleScript and return the standard output and error.'''
return run_cmd(pl, ['osascript', '-'], ascript)
def readlines(cmd, cwd):
'''Run command and read its output, line by line
:param list cmd:
Command which will be run.
:param str cwd:
Working directory of the command which will be run.
'''
p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd)
encoding = _get_shell_encoding()
p.stderr.close()
with p.stdout:
for line in p.stdout:
yield line[:-1].decode(encoding)

View File

@ -7,6 +7,7 @@ import sys
import re
from powerline.lib.vcs import get_branch_name as _get_branch_name, get_file_status
from powerline.lib.shell import readlines
_ref_pat = re.compile(br'ref:\s*refs/heads/(.+)')
@ -144,15 +145,6 @@ try:
def branch(self):
return get_branch_name(self.directory)
except ImportError:
from subprocess import Popen, PIPE
def readlines(cmd, cwd):
p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd)
p.stderr.close()
with p.stdout:
for line in p.stdout:
yield line[:-1].decode('utf-8')
class Repository(object):
__slots__ = ('directory', 'ignore_event')