Advertisement
Guest User

inout-indicator

a guest
Jul 13th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from time import time, ctime
  4. from gi.repository import Gtk
  5. from gi.repository import AppIndicator3 as Indicator
  6. from datetime import datetime, date
  7.  
  8.  
  9. MY_FILENAME = '/home/anotherghost/workhours.log'
  10. MY_INDICATOR_ID = 'work-hours-indicator'
  11. STOP_ICON_NAME = 'indicator-messages'
  12. START_ICON_NAME = 'indicator-messages-new'
  13. FMT = "%a %b %d %H:%M:%S %Y"
  14.  
  15. class MyIndicator(object):
  16.  
  17.     def __init__(self, filename):
  18.         self.indicator = Indicator.Indicator.new(
  19.                                 MY_INDICATOR_ID,
  20.                                 STOP_ICON_NAME,
  21.                                 Indicator.IndicatorCategory.APPLICATION_STATUS)
  22.  
  23.         self.menu = Gtk.Menu()
  24.  
  25.         start_item = Gtk.MenuItem('Start')
  26.         start_item.connect('activate', self.start_activated, filename)
  27.         self.menu.append(start_item)
  28.         start_item.show()
  29.  
  30.         stop_item = Gtk.MenuItem('Stop')
  31.         stop_item.connect('activate', self.stop_activated, filename)
  32.         self.menu.append(stop_item)
  33.         stop_item.show()
  34.  
  35.     showall_item = Gtk.MenuItem('Show all')
  36.         showall_item.connect('activate', self.showall, filename)
  37.         self.menu.append(showall_item)
  38.         showall_item.show()
  39.  
  40.         self.indicator.set_menu(self.menu)
  41.     self.indicator.set_attention_icon(START_ICON_NAME)
  42.  
  43.     with open(MY_FILENAME,'r') as fn:
  44.         try:
  45.             fn.seek(-70, 2)
  46.         except (IndexError, IOError):
  47.             fn.seek(max(-len(fn.read().decode()),-69),2)
  48.         self.last = fn.readlines()[-1].decode()
  49.         if len(self.last) > 1 and len(self.last) < 30:
  50.             self.last = self.last.split(" ")
  51.             self.last[-1] = self.last[-1][:4]
  52.             self.last = " ".join(self.last)
  53.             self.start_time = datetime.strptime(self.last, FMT)
  54.             self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
  55.             self.has_started = True
  56.             self.end_time = datetime.now()
  57.         else:
  58.                 self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
  59.                 self.has_started = False
  60.             self.start_time = datetime.now()
  61.             self.end_time = datetime.now()
  62.  
  63.     def main(self):
  64.         Gtk.main()
  65.  
  66.     def start_activated (self, menu_item, filename):
  67.         if not self.has_started:
  68.             myfile = open(filename, 'a')
  69.         self.start_time = datetime.now()
  70.         myfile.write('%s' % self.start_time.strftime(FMT))
  71.             self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
  72.             self.has_started = True
  73.         myfile.close()
  74.  
  75.     def stop_activated (self, menu_item, filename):
  76.         if self.has_started:
  77.             myfile = open(filename, 'r+')
  78.         myfile.seek(-1, 2)
  79.         if myfile.read(1) == '\n':
  80.         myfile.seek(-1, 2)
  81.         self.end_time = datetime.now()
  82.         self.elapsed_time = self.end_time - self.start_time
  83.         myfile.write('\t%s\t' % self.end_time.strftime(FMT))
  84.         myfile.write('%s\n' % round(float(self.elapsed_time.seconds)/3600,3) )
  85.             self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
  86.             self.has_started = False
  87.         myfile.close()
  88.  
  89.     def showall (self, menu_item, filename):
  90.         myfile = open('op.log', 'a')
  91.     myfile.write('has_started: %s\n' % self.has_started)
  92.     myfile.write('start_time: %s\n' % self.start_time.strftime(FMT))
  93.     myfile.write('end_time: %s\n' % self.end_time.strftime(FMT))
  94.  
  95.  
  96. if __name__ == '__main__':
  97.     indicator = MyIndicator(MY_FILENAME)
  98.     indicator.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement