()
return power_info
Returns information about the current power state.

  This method is used to determine the machine's current power source,
  battery life percentage and time remaining. Intended for use with power
  assertions to ensure we're creating them at an opportune time. This is done
  by using the command 'pmset -g ps' which returns something like:

  Currently drawing from 'Battery Power'
   -InternalBattery-0 100%; discharging; 3:46 remaining

  or

  Currently drawing from 'AC Power'
   -InternalBattery-0 100%; charged; 0:00 remaining

  or (Mac Pro desktop):

  Currently drawing from 'AC Power'

  Returns:
    power_info: dict, information about the power state:
        {'ac_power': bool, (True if on AC power)
         'battery_percent': int, (percentage of battery life left)
         'battery_state': string (unknown, charging, discharging, or charged),
         'minutes_remaining': int (time left until fully charged or discharged)}
        Note: If regular expression matching does not identify power_info
        values, they will be defaulted to '-1'.

  Raises:
    GmacpyutilException: command failed to execute.
Returns information about the current power state.
Returns information about the current power state .
def GetPowerState():
  """Returns information about the current power state.

  This method is used to determine the machine's current power source,
  battery life percentage and time remaining. Intended for use with power
  assertions to ensure we're creating them at an opportune time. This is done
  by using the command 'pmset -g ps' which returns something like:

  Currently drawing from 'Battery Power'
   -InternalBattery-0 100%; discharging; 3:46 remaining

  or

  Currently drawing from 'AC Power'
   -InternalBattery-0 100%; charged; 0:00 remaining

  or (Mac Pro desktop):

  Currently drawing from 'AC Power'

  Returns:
    power_info: dict, information about the power state:
        {'ac_power': bool, (True if on AC power)
         'battery_percent': int, (percentage of battery life left)
         'battery_state': string (unknown, charging, discharging, or charged),
         'minutes_remaining': int (time left until fully charged or discharged)}
        Note: If regular expression matching does not identify power_info
        values, they will be defaulted to '-1'.

  Raises:
    GmacpyutilException: command failed to execute.
  """
  power_info = {}
  power_info['ac_power'] = '-1'
  power_info['battery_percent'] = '-1'
  power_info['battery_state'] = '-1'
  power_info['minutes_remaining'] = '-1'

  cmd = ['/usr/bin/pmset', '-g', 'ps']
  # pylint: disable=unpacking-non-sequence
  stdout, stderr, returncode = RunProcess(cmd)
  # pylint: enable=unpacking-non-sequence

  lines = stdout.splitlines()
  # ["Currently drawing from 'AC Power'",
  # '-InternalBattery-0\t100%; charged; 0:00 remaining']

  if returncode or not lines:
    raise GmacpyutilException(
        'pmset error (exit %d): %s' % (returncode, stderr))

  power_source = re.match(
      r'Currently drawing from \'(?P<power_source>\w+)', lines[0])

  if power_source:
    if power_source.group('power_source') == 'AC':
      power_info['ac_power'] = True

    if power_source.group('power_source') == 'Battery':
      power_info['ac_power'] = False

  if len(lines) > 1:  # sanity check for machine without batteries
    battery_info = re.match(
        r'.*\t'
        r'(?P<percent>\d+)%; '
        r'(?P<state>\w+); '
        r'(?P<hours>\d+):(?P<mins>\d+) remaining', lines[1])
    if battery_info:  # ensure named parameters matched
      power_info['battery_percent'] = int(battery_info.group('percent'))
      power_info['battery_state'] = battery_info.group('state')
      power_info['minutes_remaining'] = (
          60 * int(
              battery_info.group('hours')) + int(battery_info.group('mins')))
  return power_info
def GetPowerState ( ) : power_info = { } power_info [ 'ac_power' ] = '-1' power_info [ 'battery_percent' ] = '-1' power_info [ 'battery_state' ] = '-1' power_info [ 'minutes_remaining' ] = '-1' cmd = [ '/usr/bin/pmset' , '-g' , 'ps' ] # pylint: disable=unpacking-non-sequence stdout , stderr , returncode = RunProcess ( cmd ) # pylint: enable=unpacking-non-sequence lines = stdout . splitlines ( ) # ["Currently drawing from 'AC Power'", # '-InternalBattery-0\t100%; charged; 0:00 remaining'] if returncode or not lines : raise GmacpyutilException ( 'pmset error (exit %d): %s' % ( returncode , stderr ) ) power_source = re . match ( r'Currently drawing from \'(?P<power_source>\w+)' , lines [ 0 ] ) if power_source : if power_source . group ( 'power_source' ) == 'AC' : power_info [ 'ac_power' ] = True if power_source . group ( 'power_source' ) == 'Battery' : power_info [ 'ac_power' ] = False if len ( lines ) > 1 : # sanity check for machine without batteries battery_info = re . match ( r'.*\t' r'(?P<percent>\d+)%; ' r'(?P<state>\w+); ' r'(?P<hours>\d+):(?P<mins>\d+) remaining' , lines [ 1 ] ) if battery_info : # ensure named parameters matched power_info [ 'battery_percent' ] = int ( battery_info . group ( 'percent' ) ) power_info [ 'battery_state' ] = battery_info . group ( 'state' ) power_info [ 'minutes_remaining' ] = ( 60 * int ( battery_info . group ( 'hours' ) ) + int ( battery_info . group ( 'mins' ) ) ) return power_info