From 1498fc714c7bebcbc9f84aefb97e8af8a8f9f082 Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 25 Jun 2014 20:59:39 +0400 Subject: [PATCH] Move readlines function to powerline.lib.shell --- powerline/lib/shell.py | 25 +++++++++++++++++++++++-- powerline/lib/vcs/git.py | 10 +--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/powerline/lib/shell.py b/powerline/lib/shell.py index db16fd5d..dd995b48 100644 --- a/powerline/lib/shell.py +++ b/powerline/lib/shell.py @@ -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) diff --git a/powerline/lib/vcs/git.py b/powerline/lib/vcs/git.py index 77d1f387..e9e16b8b 100644 --- a/powerline/lib/vcs/git.py +++ b/powerline/lib/vcs/git.py @@ -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')