From 7c65ee97037b2fbbfeae8d34d0579fc168819104 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 27 May 2014 05:43:55 +0400 Subject: [PATCH] Catch NotImplementedError when importing module Fixes #883 --- powerline/segments/common.py | 40 ++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/powerline/segments/common.py b/powerline/segments/common.py index d77c4aec..5c22416c 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -1105,25 +1105,29 @@ class NowPlayingSegment(object): now_playing = NowPlayingSegment() -if os.path.exists('/sys/class/power_supply/'): - _linux_bat_fmt = '/sys/class/power_supply/{0}/capacity' - _linux_bat = 'BAT0' - if not os.path.exists(_linux_bat_fmt.format(_linux_bat)): - _linux_bat = 'BAT1' - if not os.path.exists(_linux_bat_fmt.format(_linux_bat)): - raise NotImplementedError - def _get_capacity(pl): - with open(_linux_bat_fmt.format(_linux_bat), 'r') as f: - return int(float(f.readline().split()[0])) -elif os.path.exists('/usr/bin/pmset'): - def _get_capacity(pl): - import re - battery_summary = run_cmd(pl, ['pmset', '-g', 'batt']) - battery_percent = re.search(r'(\d+)%', battery_summary).group(1) - return int(battery_percent) -else: - def _get_capacity(pl): +try: + if os.path.exists('/sys/class/power_supply/'): + _linux_bat_fmt = '/sys/class/power_supply/{0}/capacity' + _linux_bat = 'BAT0' + if not os.path.exists(_linux_bat_fmt.format(_linux_bat)): + _linux_bat = 'BAT1' + if not os.path.exists(_linux_bat_fmt.format(_linux_bat)): + raise NotImplementedError + def _get_capacity(pl): + with open(_linux_bat_fmt.format(_linux_bat), 'r') as f: + return int(float(f.readline().split()[0])) + else: raise NotImplementedError +except NotImplementedError: + if os.path.exists('/usr/bin/pmset'): + def _get_capacity(pl): + import re + battery_summary = run_cmd(pl, ['pmset', '-g', 'batt']) + battery_percent = re.search(r'(\d+)%', battery_summary).group(1) + return int(battery_percent) + else: + def _get_capacity(pl): + raise NotImplementedError def battery(pl, format='{capacity:3.0%}', steps=5, gamify=False, full_heart='♥', empty_heart='♥'):