<commit_before># vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import os
import sys
import re

from powerline.lib.shell import run_cmd


def _fetch_battery_info(pl):
	try:
		import dbus
	except ImportError:
		pl.debug('Not using DBUS+UPower as dbus is not available')
	else:
		try:
			bus = dbus.SystemBus()
		except Exception as e:
			pl.exception('Failed to connect to system bus: {0}', str(e))
		else:
			interface = 'org.freedesktop.UPower'
			try:
				up = bus.get_object(interface, '/org/freedesktop/UPower')
			except dbus.exceptions.DBusException as e:
				if getattr(e, '_dbus_error_name', '').endswith('ServiceUnknown'):
					pl.debug('Not using DBUS+UPower as UPower is not available via dbus')
				else:
					pl.exception('Failed to get UPower service with dbus: {0}', str(e))
			else:
				devinterface = 'org.freedesktop.DBus.Properties'
				devtype_name = interface + '.Device'
				for devpath in up.EnumerateDevices(dbus_interface=interface):
					dev = bus.get_object(interface, devpath)
					devget = lambda what: dev.Get(
						devtype_name,
						what,
						dbus_interface=devinterface
					)
					if int(devget('Type')) != 2:
						pl.debug('Not using DBUS+UPower with {0}: invalid type', devpath)
						continue
					if not bool(devget('IsPresent')):
						pl.debug('Not using DBUS+UPower with {0}: not present', devpath)
						continue
					if not bool(devget('PowerSupply')):
						pl.debug('Not using DBUS+UPower with {0}: not a power supply', devpath)
						continue
					pl.debug('Using DBUS+UPower with {0}', devpath)
					return lambda pl: (
						float(
							dbus.Interface(dev, dbus_interface=devinterface).Get(
								devtype_name,
								'Percentage'
							),
						),
						dbus.Interface(dev, dbus_interface=devinterface).Get(
							devtype_name,
							'State'
						) == 1
					)
				pl.debug('Not using DBUS+UPower as no batteries were found')

	if os.path.isdir('/sys/class/power_supply'):
		online_path_verified = None
		linux_supplier_fmt = '/sys/class/power_supply/{0}/capacity'
		linux_ac_fmt = '/sys/class/power_supply/{0}/online'
		for linux_supplier in os.listdir('/sys/class/power_supply'):
			cap_path = linux_supplier_fmt.format(linux_supplier)
			online_path = linux_ac_fmt.format(linux_supplier)
			if linux_supplier.startswith('AC') and os.path.exists(online_path):
				pl.debug('Using /sys/class/power_supply with AC {0}', online_path)
				online_path_verified = online_path
			elif linux_supplier.startswith('BAT') and os.path.exists(cap_path):
				pl.debug('Using /sys/class/power_supply with battery {0}', linux_supplier)

				def _get_battery_status(pl):
					_ac_powered = None
					with open(cap_path, 'r') as f:
						_capacity = int(float(f.readline().split()[0]))
					if online_path_verified:
						with open(online_path_verified, 'r') as f:
							_ac_powered = int(f.readline()) == 1
					return _capacity, _ac_powered
				return _get_battery_status
			pl.debug('Not using /sys/class/power_supply as no batteries were found')
	else:
		pl.debug('Not using /sys/class/power_supply: no directory')

	try:
		from shutil import which  # Python-3.3 and later
	except ImportError:
		pl.info('Using dumb “which” which only checks for file in /usr/bin')
		which = lambda f: (lambda fp: os.path.exists(fp) and fp)(os.path.join('/usr/bin', f))<commit_msg>[Battery - Linux] Look at battery status instead of AC online-ness<commit_after># vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import os
import sys
import re

from powerline.lib.shell import run_cmd


def _fetch_battery_info(pl):
	try:
		import dbus
	except ImportError:
		pl.debug('Not using DBUS+UPower as dbus is not available')
	else:
		try:
			bus = dbus.SystemBus()
		except Exception as e:
			pl.exception('Failed to connect to system bus: {0}', str(e))
		else:
			interface = 'org.freedesktop.UPower'
			try:
				up = bus.get_object(interface, '/org/freedesktop/UPower')
			except dbus.exceptions.DBusException as e:
				if getattr(e, '_dbus_error_name', '').endswith('ServiceUnknown'):
					pl.debug('Not using DBUS+UPower as UPower is not available via dbus')
				else:
					pl.exception('Failed to get UPower service with dbus: {0}', str(e))
			else:
				devinterface = 'org.freedesktop.DBus.Properties'
				devtype_name = interface + '.Device'
				for devpath in up.EnumerateDevices(dbus_interface=interface):
					dev = bus.get_object(interface, devpath)
					devget = lambda what: dev.Get(
						devtype_name,
						what,
						dbus_interface=devinterface
					)
					if int(devget('Type')) != 2:
						pl.debug('Not using DBUS+UPower with {0}: invalid type', devpath)
						continue
					if not bool(devget('IsPresent')):
						pl.debug('Not using DBUS+UPower with {0}: not present', devpath)
						continue
					if not bool(devget('PowerSupply')):
						pl.debug('Not using DBUS+UPower with {0}: not a power supply', devpath)
						continue
					pl.debug('Using DBUS+UPower with {0}', devpath)
					return lambda pl: (
						float(
							dbus.Interface(dev, dbus_interface=devinterface).Get(
								devtype_name,
								'Percentage'
							),
						),
						dbus.Interface(dev, dbus_interface=devinterface).Get(
							devtype_name,
							'State'
						) == 1
					)
				pl.debug('Not using DBUS+UPower as no batteries were found')

	if os.path.isdir('/sys/class/power_supply'):
		linux_capacity_fmt = '/sys/class/power_supply/{0}/capacity'
		linux_status_fmt = '/sys/class/power_supply/{0}/status'
		for linux_supplier in os.listdir('/sys/class/power_supply'):
			cap_path = linux_capacity_fmt.format(linux_supplier)
			status_path = linux_status_fmt.format(linux_supplier)
			if not os.path.exists(cap_path):
				continue
			pl.debug('Using /sys/class/power_supply with battery {0}', linux_supplier)
			def _get_battery_status(pl):
				with open(cap_path, 'r') as f:
					_capacity = int(float(f.readline().split()[0]))
				try:
					with open(status_path, 'r') as f:
						_ac_powered = (f.readline().strip() != 'Discharging')
				except IOError:
					_ac_powered = None
				return _capacity, _ac_powered
			return _get_battery_status
			pl.debug('Not using /sys/class/power_supply as no batteries were found')
	else:
		pl.debug('Not using /sys/class/power_supply: no directory')

	try:
		from shutil import which  # Python-3.3 and later
	except ImportError:
		pl.info('Using dumb “which” which only checks for file in /usr/bin')
		which = lambda f: (lambda fp: os.path.exists(fp) and fp)(os.path.join('/usr/bin', f))
{'fraction_non_alphanumeric': 0.12075242718446602, 'fraction_numerical': 0.0048543689320388345, 'mean_word_length': 3.5848400556328235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3845999', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2218, 'n_words': 547}