parameters: ()
return_statement: return _common.sbattery(percent, secsleft, power_plugged)
docstring: Return battery information.
      Implementation note: it appears /sys/class/power_supply/BAT0/
      directory structure may vary and provide files with the same
      meaning but under different names, see:
      https://github.com/giampaolo/psutil/issues/966
docstring_summary: Return battery information.
      Implementation note: it appears /sys/class/power_supply/BAT0/
      directory structure may vary and provide files with the same
      meaning but under different names, see:
      https://github.com/giampaolo/psutil/issues/966
docstring_tokens: Return battery information . Implementation note : it appears / sys / class / power_supply / BAT0 / directory structure may vary and provide files with the same meaning but under different names see : https : // github . com / giampaolo / psutil / issues / 966
function: def sensors_battery():
      """Return battery information.
      Implementation note: it appears /sys/class/power_supply/BAT0/
      directory structure may vary and provide files with the same
      meaning but under different names, see:
      https://github.com/giampaolo/psutil/issues/966
      """
      null = object()
  
      def multi_cat(*paths):
          """Attempt to read the content of multiple files which may
          not exist. If none of them exist return None.
          """
          for path in paths:
              ret = cat(path, fallback=null)
              if ret != null:
                  return int(ret) if ret.isdigit() else ret
          return None
  
      bats = [x for x in os.listdir(POWER_SUPPLY_PATH) if x.startswith('BAT')]
      if not bats:
          return None
      # Get the first available battery. Usually this is "BAT0", except
      # some rare exceptions:
      # https://github.com/giampaolo/psutil/issues/1238
      root = os.path.join(POWER_SUPPLY_PATH, sorted(bats)[0])
  
      # Base metrics.
      energy_now = multi_cat(
          root + "/energy_now",
          root + "/charge_now")
      power_now = multi_cat(
          root + "/power_now",
          root + "/current_now")
      energy_full = multi_cat(
          root + "/energy_full",
          root + "/charge_full")
      if energy_now is None or power_now is None:
          return None
  
      # Percent. If we have energy_full the percentage will be more
      # accurate compared to reading /capacity file (float vs. int).
      if energy_full is not None:
          try:
              percent = 100.0 * energy_now / energy_full
          except ZeroDivisionError:
              percent = 0.0
      else:
          percent = int(cat(root + "/capacity", fallback=-1))
          if percent == -1:
              return None
  
      # Is AC power cable plugged in?
      # Note: AC0 is not always available and sometimes (e.g. CentOS7)
      # it's called "AC".
      power_plugged = None
      online = multi_cat(
          os.path.join(POWER_SUPPLY_PATH, "AC0/online"),
          os.path.join(POWER_SUPPLY_PATH, "AC/online"))
      if online is not None:
          power_plugged = online == 1
      else:
          status = cat(root + "/status", fallback="", binary=False).lower()
          if status == "discharging":
              power_plugged = False
          elif status in ("charging", "full"):
              power_plugged = True
  
      # Seconds left.
      # Note to self: we may also calculate the charging ETA as per:
      # https://github.com/thialfihar/dotfiles/blob/
      #     013937745fd9050c30146290e8f963d65c0179e6/bin/battery.py#L55
      if power_plugged:
          secsleft = _common.POWER_TIME_UNLIMITED
      else:
          try:
              secsleft = int(energy_now / power_now * 3600)
          except ZeroDivisionError:
              secsleft = _common.POWER_TIME_UNKNOWN
  
      return _common.sbattery(percent, secsleft, power_plugged)
function_tokens: def sensors_battery ( ) : null = object ( ) def multi_cat ( * paths ) : """Attempt to read the content of multiple files which may
          not exist. If none of them exist return None.
          """ for path in paths : ret = cat ( path , fallback = null ) if ret != null : return int ( ret ) if ret . isdigit ( ) else ret return None bats = [ x for x in os . listdir ( POWER_SUPPLY_PATH ) if x . startswith ( 'BAT' ) ] if not bats : return None # Get the first available battery. Usually this is "BAT0", except # some rare exceptions: # https://github.com/giampaolo/psutil/issues/1238 root = os . path . join ( POWER_SUPPLY_PATH , sorted ( bats ) [ 0 ] ) # Base metrics. energy_now = multi_cat ( root + "/energy_now" , root + "/charge_now" ) power_now = multi_cat ( root + "/power_now" , root + "/current_now" ) energy_full = multi_cat ( root + "/energy_full" , root + "/charge_full" ) if energy_now is None or power_now is None : return None # Percent. If we have energy_full the percentage will be more # accurate compared to reading /capacity file (float vs. int). if energy_full is not None : try : percent = 100.0 * energy_now / energy_full except ZeroDivisionError : percent = 0.0 else : percent = int ( cat ( root + "/capacity" , fallback = - 1 ) ) if percent == - 1 : return None # Is AC power cable plugged in? # Note: AC0 is not always available and sometimes (e.g. CentOS7) # it's called "AC". power_plugged = None online = multi_cat ( os . path . join ( POWER_SUPPLY_PATH , "AC0/online" ) , os . path . join ( POWER_SUPPLY_PATH , "AC/online" ) ) if online is not None : power_plugged = online == 1 else : status = cat ( root + "/status" , fallback = "" , binary = False ) . lower ( ) if status == "discharging" : power_plugged = False elif status in ( "charging" , "full" ) : power_plugged = True # Seconds left. # Note to self: we may also calculate the charging ETA as per: # https://github.com/thialfihar/dotfiles/blob/ #     013937745fd9050c30146290e8f963d65c0179e6/bin/battery.py#L55 if power_plugged : secsleft = _common . POWER_TIME_UNLIMITED else : try : secsleft = int ( energy_now / power_now * 3600 ) except ZeroDivisionError : secsleft = _common . POWER_TIME_UNKNOWN return _common . sbattery ( percent , secsleft , power_plugged )