Skip to content

Instantly share code, notes, and snippets.

@nicolargo
Created February 2, 2017 21:06
Show Gist options
  • Save nicolargo/292c0c6c9ea935c08b5163549e473c20 to your computer and use it in GitHub Desktop.
Save nicolargo/292c0c6c9ea935c08b5163549e473c20 to your computer and use it in GitHub Desktop.
import os
POWER_SUPPLY_PATH = "/sys/class/power_supply"
_DEFAULT = object()
def open_binary(fname, **kwargs):
return open(fname, "rb", **kwargs)
def cat(fname, fallback=_DEFAULT, binary=True):
"""Return file content."""
try:
f = open_binary(fname) if binary else open_text(fname)
except IOError:
if fallback != _DEFAULT:
return fallback
raise
else:
with f:
return f.read().strip()
def sensors_battery():
root = os.path.join(POWER_SUPPLY_PATH, "BAT0")
if not os.path.exists(root):
return None
power_plugged = cat(os.path.join(POWER_SUPPLY_PATH, "AC0/online"),
fallback=b"0") == b"1"
if os.path.exists(root + "/energy_now"):
remaining_capacity = int(cat(root + "/energy_now"))
elif os.path.exists(root + "/charge_now"):
remaining_capacity = int(cat(root + "/charge_now"))
else:
return None
if os.path.exists(root + "/power_now"):
present_rate = int(cat(root + "/power_now"))
elif os.path.exists(root + "/power_now"):
present_rate = int(cat(root + "/curent_now"))
else:
return None
if os.path.exists(root + "/capacity"):
percent = int(cat(root + "/capacity"))
else:
if os.path.exists(root + "/charge_full"):
max_capacity = int(cat(root + "/charge_full"))
elif os.path.exists(root + "/energy_full"):
max_capacity = int(cat(root + "/energy_full"))
else:
return None
percent = 100.0 * remaining_capacity / max_capacity
if power_plugged:
secsleft = _common.POWER_TIME_UNLIMITED
else:
try:
secsleft = int(remaining_capacity / present_rate * 3600)
except ZeroDivisionError:
secsleft = _common.POWER_TIME_UNKNOWN
return _common.sbattery(percent, secsleft, power_plugged)
print sensors_battery()
@JessicaLawson
Copy link

Hello
Happy to meet you, my name is Miss Jessica Lawson, it is my pressure to meet you here  today through this site, i will like you to write to me so that i can be able to tell you more about me and the reason of my contact with you. here is my private email address, please write to me here so that i can tell you more anout me and also to send my picture  to you ( [email protected] )  thanks

Yours Miss Jessica Lawson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment