Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from time import time, ctime
- from gi.repository import Gtk
- from gi.repository import AppIndicator3 as Indicator
- from datetime import datetime, date
- MY_FILENAME = '/home/anotherghost/workhours.log'
- MY_INDICATOR_ID = 'work-hours-indicator'
- STOP_ICON_NAME = 'indicator-messages'
- START_ICON_NAME = 'indicator-messages-new'
- FMT = "%a %b %d %H:%M:%S %Y"
- class MyIndicator(object):
- def __init__(self, filename):
- self.indicator = Indicator.Indicator.new(
- MY_INDICATOR_ID,
- STOP_ICON_NAME,
- Indicator.IndicatorCategory.APPLICATION_STATUS)
- self.menu = Gtk.Menu()
- start_item = Gtk.MenuItem('Start')
- start_item.connect('activate', self.start_activated, filename)
- self.menu.append(start_item)
- start_item.show()
- stop_item = Gtk.MenuItem('Stop')
- stop_item.connect('activate', self.stop_activated, filename)
- self.menu.append(stop_item)
- stop_item.show()
- showall_item = Gtk.MenuItem('Show all')
- showall_item.connect('activate', self.showall, filename)
- self.menu.append(showall_item)
- showall_item.show()
- self.indicator.set_menu(self.menu)
- self.indicator.set_attention_icon(START_ICON_NAME)
- with open(MY_FILENAME,'r') as fn:
- try:
- fn.seek(-70, 2)
- except (IndexError, IOError):
- fn.seek(max(-len(fn.read().decode()),-69),2)
- self.last = fn.readlines()[-1].decode()
- if len(self.last) > 1 and len(self.last) < 30:
- self.last = self.last.split(" ")
- self.last[-1] = self.last[-1][:4]
- self.last = " ".join(self.last)
- self.start_time = datetime.strptime(self.last, FMT)
- self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
- self.has_started = True
- self.end_time = datetime.now()
- else:
- self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
- self.has_started = False
- self.start_time = datetime.now()
- self.end_time = datetime.now()
- def main(self):
- Gtk.main()
- def start_activated (self, menu_item, filename):
- if not self.has_started:
- myfile = open(filename, 'a')
- self.start_time = datetime.now()
- myfile.write('%s' % self.start_time.strftime(FMT))
- self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
- self.has_started = True
- myfile.close()
- def stop_activated (self, menu_item, filename):
- if self.has_started:
- myfile = open(filename, 'r+')
- myfile.seek(-1, 2)
- if myfile.read(1) == '\n':
- myfile.seek(-1, 2)
- self.end_time = datetime.now()
- self.elapsed_time = self.end_time - self.start_time
- myfile.write('\t%s\t' % self.end_time.strftime(FMT))
- myfile.write('%s\n' % round(float(self.elapsed_time.seconds)/3600,3) )
- self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
- self.has_started = False
- myfile.close()
- def showall (self, menu_item, filename):
- myfile = open('op.log', 'a')
- myfile.write('has_started: %s\n' % self.has_started)
- myfile.write('start_time: %s\n' % self.start_time.strftime(FMT))
- myfile.write('end_time: %s\n' % self.end_time.strftime(FMT))
- if __name__ == '__main__':
- indicator = MyIndicator(MY_FILENAME)
- indicator.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement