Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- '''
- tray icon battery indicator
- adapted from from http://ubuntuforums.org/showthread.php?t=1153951
- any original code Copyright (c) 2013 jc.unternet.net
- licensing GPL3 or later
- '''
- import sys, os, wx, subprocess, re, time
- PNG = wx.BITMAP_TYPE_PNG
- DEFAULT_ICON = 'icons/unplugged_50.png'
- ACPI_PATTERN = '^Battery 0:\s+(\w+), (\d+)%'
- NAMES = {
- 'Full': 'plugged',
- 'Charging': 'plugged',
- 'Discharging': 'unplugged',
- }
- UPDATE_TIME = 5 # seconds
- MILLISECONDS = 1000 # multiplier for seconds
- UPDATER = wx.NewEventType() # returns unique (to this app) ID
- class Icon(wx.Frame):
- def __init__(self, parent, ID, title):
- wx.Frame.__init__(self, parent, ID, title, wx.DefaultPosition, wx.DefaultSize)
- self.updateIcon()
- # bind frame onQuit to window close
- self.Bind(wx.EVT_CLOSE, self.onQuit)
- # bind frame onUp to mouseup on taskbar icon
- self.icon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.onUp)
- width, height = self.GetSize()
- print >>sys.stderr, 'frame size: (%d, %d)' % (width, height)
- # full-size copy of icon image is shown in center of frame (not really)
- image = wx.StaticBitmap(self,
- id = -1,
- bitmap = wx.Bitmap(self.iconfile, PNG),
- pos = (width / 2, height / 2))
- self.timer = wx.Timer(self, UPDATER)
- self.timer.Start(UPDATE_TIME * MILLISECONDS)
- wx.EVT_TIMER(self, UPDATER, self.updateIcon)
- debug('initialization complete')
- def onQuit(self, event):
- self.timer.Stop() # maybe not necessary but can't hurt
- self.icon.Destroy()
- self.Destroy()
- def onUp(self, event):
- iconized = self.IsIconized()
- if not iconized:
- self.Iconize()
- self.Hide()
- else:
- self.Restore()
- self.Show()
- def updateIcon(self, event = None):
- acpistring = acpi()
- debug('acpi: %s' % acpistring)
- status = re.compile(ACPI_PATTERN).match(acpistring)
- debug('status: %s' % status)
- self.iconfile = icon_name(*status.groups()) if status else DEFAULT_ICON
- icon_image = wx.Icon(name = self.iconfile, type = PNG)
- if not hasattr(self, 'icon'):
- self.icon = wx.TaskBarIcon()
- self.icon.SetIcon(icon = icon_image, tooltip = "Battery 0 State")
- class IconApp(wx.App):
- def OnInit(self):
- self.command = sys.argv[0]
- if os.path.islink(self.command):
- self.command = os.readlink(self.command)
- # change to source directory to give us access to icons
- os.chdir(os.path.dirname(self.command))
- frame = Icon(None, -1, "Battery 0 State")
- frame.Show() # shows just momentarily before we iconize it
- self.SetTopWindow(frame)
- frame.Iconize()
- return True
- def debug(message = None):
- if __debug__:
- if message:
- print >>sys.stderr, message
- return True
- def acpi():
- output = subprocess.Popen(['acpi'], stdout = subprocess.PIPE).communicate()[0]
- return output
- def icon_name(word, number):
- return 'icons/%s_%s.png' % (NAMES.get(word, 'unplugged'), number)
- if __name__ == '__main__':
- IconApp(0).MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement