SHARE
TWEET

xacpi.py

jcomeau_ictx Jan 17th, 2014 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. '''
  3. tray icon battery indicator
  4.  
  5. adapted from from http://ubuntuforums.org/showthread.php?t=1153951
  6. any original code Copyright (c) 2013 jc.unternet.net
  7. licensing GPL3 or later
  8. '''
  9. import sys, os, wx, subprocess, re, time
  10. PNG = wx.BITMAP_TYPE_PNG
  11. DEFAULT_ICON = 'icons/unplugged_50.png'
  12. ACPI_PATTERN = '^Battery 0:\s+(\w+), (\d+)%'
  13. NAMES = {
  14.  'Full': 'plugged',
  15.  'Charging': 'plugged',
  16.  'Discharging': 'unplugged',
  17. }
  18. UPDATE_TIME = 5  # seconds
  19. MILLISECONDS = 1000  # multiplier for seconds
  20. UPDATER = wx.NewEventType()  # returns unique (to this app) ID
  21. class Icon(wx.Frame):
  22.  def __init__(self, parent, ID, title):
  23.   wx.Frame.__init__(self, parent, ID, title, wx.DefaultPosition, wx.DefaultSize)
  24.   self.updateIcon()
  25.   # bind frame onQuit to window close
  26.   self.Bind(wx.EVT_CLOSE, self.onQuit)
  27.   # bind frame onUp to mouseup on taskbar icon
  28.   self.icon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.onUp)
  29.   width, height = self.GetSize()
  30.   print >>sys.stderr, 'frame size: (%d, %d)' % (width, height)
  31.   # full-size copy of icon image is shown in center of frame (not really)
  32.   image = wx.StaticBitmap(self,
  33.    id = -1,
  34.    bitmap = wx.Bitmap(self.iconfile, PNG),
  35.    pos = (width / 2, height / 2))
  36.   self.timer = wx.Timer(self, UPDATER)
  37.   self.timer.Start(UPDATE_TIME * MILLISECONDS)
  38.   wx.EVT_TIMER(self, UPDATER, self.updateIcon)
  39.   debug('initialization complete')
  40.  def onQuit(self, event):
  41.   self.timer.Stop()  # maybe not necessary but can't hurt
  42.   self.icon.Destroy()
  43.   self.Destroy()
  44.  def onUp(self, event):
  45.   iconized = self.IsIconized()
  46.   if not iconized:
  47.    self.Iconize()
  48.    self.Hide()
  49.   else:
  50.    self.Restore()
  51.    self.Show()
  52.  def updateIcon(self, event = None):
  53.   acpistring = acpi()
  54.   debug('acpi: %s' % acpistring)
  55.   status = re.compile(ACPI_PATTERN).match(acpistring)
  56.   debug('status: %s' % status)
  57.   self.iconfile = icon_name(*status.groups()) if status else DEFAULT_ICON
  58.   icon_image = wx.Icon(name = self.iconfile, type = PNG)
  59.   if not hasattr(self, 'icon'):
  60.    self.icon = wx.TaskBarIcon()
  61.   self.icon.SetIcon(icon = icon_image, tooltip = "Battery 0 State")
  62. class IconApp(wx.App):
  63.  def OnInit(self):
  64.   self.command = sys.argv[0]
  65.   if os.path.islink(self.command):
  66.    self.command = os.readlink(self.command)
  67.   # change to source directory to give us access to icons
  68.   os.chdir(os.path.dirname(self.command))
  69.   frame = Icon(None, -1, "Battery 0 State")
  70.   frame.Show()  # shows just momentarily before we iconize it
  71.   self.SetTopWindow(frame)
  72.   frame.Iconize()
  73.   return True
  74. def debug(message = None):
  75.  if __debug__:
  76.   if message:
  77.    print >>sys.stderr, message
  78.   return True
  79. def acpi():
  80.  output = subprocess.Popen(['acpi'], stdout = subprocess.PIPE).communicate()[0]
  81.  return output
  82. def icon_name(word, number):
  83.  return 'icons/%s_%s.png' % (NAMES.get(word, 'unplugged'), number)
  84. if __name__ == '__main__':
  85.  IconApp(0).MainLoop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top