Skip to content

Commit

Permalink
Merge pull request b-ryan#211 from b-ryan/fix/python3-compat
Browse files Browse the repository at this point in the history
fix python3 compatibility
  • Loading branch information
amtrivedi91 committed Nov 22, 2015
2 parents 6c101eb + ee1ac82 commit 90e1cfb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
12 changes: 10 additions & 2 deletions powerline_shell_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import os
import sys

py3 = sys.version_info.major == 3


def warn(msg):
print('[powerline-bash] ', msg)


class Powerline:
symbols = {
'compatible': {
Expand Down Expand Up @@ -67,8 +71,12 @@ def append(self, content, fg, bg, separator=None, separator_fg=None):
separator_fg if separator_fg is not None else bg))

def draw(self):
return (''.join(self.draw_segment(i) for i in range(len(self.segments)))
+ self.reset).encode('utf-8') + ' '
text = (''.join(self.draw_segment(i) for i in range(len(self.segments)))
+ self.reset) + ' '
if py3:
return text
else:
return text.encode('utf-8')

def draw_segment(self, idx):
segment = self.segments[idx]
Expand Down
4 changes: 3 additions & 1 deletion segments/cwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def get_fg_bg(name):


def add_cwd_segment():
cwd = (powerline.cwd or os.getenv('PWD')).decode('utf-8')
cwd = powerline.cwd or os.getenv('PWD')
if not py3:
cwd = cwd.decode("utf-8")
cwd = replace_home_dir(cwd)

if powerline.args.cwd_mode == 'plain':
Expand Down
7 changes: 3 additions & 4 deletions segments/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ def _get_git_detached_branch():
p = subprocess.Popen(['git', 'describe', '--tags', '--always'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=git_subprocess_env)
detached_ref = p.communicate()[0].rstrip('\n')
detached_ref = p.communicate()[0].decode("utf-8").rstrip('\n')
if p.returncode == 0:
branch = u'{} {}'.format(GIT_SYMBOLS['detached'],
detached_ref.decode('utf-8'))
branch = u'{} {}'.format(GIT_SYMBOLS['detached'], detached_ref)
else:
branch = 'Big Bang'
return branch
Expand Down Expand Up @@ -71,7 +70,7 @@ def add_git_segment():
if p.returncode != 0:
return

status = pdata[0].splitlines()
status = pdata[0].decode("utf-8").splitlines()

branch_info = parse_git_branch_info(status)
stats = parse_git_stats(status)
Expand Down
6 changes: 4 additions & 2 deletions segments/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ def get_hg_status():
has_modified_files = False
has_untracked_files = False
has_missing_files = False
output = subprocess.Popen(['hg', 'status'],
stdout=subprocess.PIPE).communicate()[0]

p = subprocess.Popen(['hg', 'status'], stdout=subprocess.PIPE)
output = p.communicate()[0].decode("utf-8")

for line in output.split('\n'):
if line == '':
continue
Expand Down
10 changes: 8 additions & 2 deletions segments/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import subprocess

def add_jobs_segment():
pppid = subprocess.Popen(['ps', '-p', str(os.getppid()), '-oppid='], stdout=subprocess.PIPE).communicate()[0].strip()
output = subprocess.Popen(['ps', '-a', '-o', 'ppid'], stdout=subprocess.PIPE).communicate()[0]
pppid_proc = subprocess.Popen(['ps', '-p', str(os.getppid()), '-oppid='],
stdout=subprocess.PIPE)
pppid = pppid_proc.communicate()[0].decode("utf-8").strip()

output_proc = subprocess.Popen(['ps', '-a', '-o', 'ppid'],
stdout=subprocess.PIPE)
output = output_proc.communicate()[0].decode("utf-8")

num_jobs = len(re.findall(str(pppid), output)) - 1

if num_jobs > 0:
Expand Down
2 changes: 1 addition & 1 deletion segments/node_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def add_node_version_segment():
try:
p1 = subprocess.Popen(["node", "--version"], stdout=subprocess.PIPE)
version = p1.communicate()[0].rstrip()
version = p1.communicate()[0].decode("utf-8").rstrip()
version = "node " + version
powerline.append(version, 15, 18)
except OSError:
Expand Down
2 changes: 1 addition & 1 deletion segments/ruby_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def add_ruby_version_segment():
try:
p1 = subprocess.Popen(["ruby", "-v"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["sed", "s/ (.*//"], stdin=p1.stdout, stdout=subprocess.PIPE)
version = p2.communicate()[0].rstrip()
version = p2.communicate()[0].decode("utf-8").rstrip()
if os.environ.has_key("GEM_HOME"):
gem = os.environ["GEM_HOME"].split("@")
if len(gem) > 1:
Expand Down
7 changes: 4 additions & 3 deletions segments/svn.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import subprocess

def add_svn_segment():
is_svn = subprocess.Popen(['svn', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
is_svn_output = is_svn.communicate()[1].strip()
is_svn = subprocess.Popen(['svn', 'status'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
is_svn_output = is_svn.communicate()[1].decode("utf-8").strip()
if len(is_svn_output) != 0:
return

Expand All @@ -11,7 +12,7 @@ def add_svn_segment():
stderr=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-c', '^[ACDIMR\\!\\~]'],
stdin=p1.stdout, stdout=subprocess.PIPE)
output = p2.communicate()[0].strip()
output = p2.communicate()[0].decode("utf-8").strip()
if len(output) > 0 and int(output) > 0:
changes = output.strip()
powerline.append(' %s ' % changes, Color.SVN_CHANGES_FG, Color.SVN_CHANGES_BG)
Expand Down

0 comments on commit 90e1cfb

Please sign in to comment.