Last active
December 19, 2015 14:58
-
-
Save kjmkznr/5972503 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- conding: utf-8 -*- | |
import pygtk | |
pygtk.require('2.0') | |
import pynotify | |
import sys | |
import gtk | |
import os | |
import string | |
import time | |
import threading | |
from optparse import OptionParser | |
from datetime import datetime, date, timedelta | |
# Options | |
parser = OptionParser() | |
parser.set_defaults(work_time=25, play_time=5, break_time=20) | |
parser.add_option("-w", "--work-time", type="int", dest="work_time", help="how much time to work, in minutes") | |
parser.add_option("-p", "--play-time", type="int", dest="play_time", help="how much time to break, in minutes") | |
parser.add_option("-b", "--break-time", type="int", dest="break_time", help="how long the longer break is, in minutes") | |
(option, args) = parser.parse_args() | |
work_time = option.work_time * 60 | |
play_time = option.play_time * 60 | |
break_time = option.break_time * 60 | |
def get_conf_dir(name): | |
fpath = os.getenv('XDG_CONFIG_HOME') | |
if not fpath: | |
fpath = os.path.join(os.getenv('HOME'), '.config') | |
sys.stderr.write("error: the environment variable \"XDG_CONFIG_HOME\" is not set\nDefaulting to " + fpath + "\n") | |
return os.path.join(fpath, name) | |
def get_cache_dir(name): | |
fpath = os.getenv('XDG_CACHE_HOME') | |
if not fpath: | |
fpath = os.path.join(os.getenv('HOME'), '.cache') | |
sys.stderr.write("error: the environment variable \"XDG_CACHE_HOME\" is not set\nDefaulting to " + fpath + "\n") | |
return os.path.join(fpath, name) | |
class Pomodoro(): | |
ICON_BLUE = """<?xml version="1.0" encoding="utf-8" standalone="no"?> | |
<svg width="100" height="100" version="1.0" xmlns="http://www.w3.org/2000/svg"> | |
<circle cx="50" cy="50" r="50" fill="blue" /> | |
</svg> | |
""" | |
ICON_YELLOW = """<?xml version="1.0" encoding="utf-8" standalone="no"?> | |
<svg width="100" height="100" version="1.0" xmlns="http://www.w3.org/2000/svg"> | |
<circle cx="50" cy="50" r="50" fill="yellow" /> | |
</svg> | |
""" | |
ICON_RED = """<?xml version="1.0" encoding="utf-8" standalone="no"?> | |
<svg width="100" height="100" version="1.0" xmlns="http://www.w3.org/2000/svg"> | |
<circle cx="50" cy="50" r="50" fill="red" /> | |
</svg> | |
""" | |
def __init__(self, name = 'pomodoro'): | |
self.name = name | |
self.config_dir = get_conf_dir(self.name) | |
self.cache_dir = get_cache_dir(self.name) | |
self.icon_work = self.get_icon('work', self.ICON_BLUE) | |
self.icon_play = self.get_icon('play', self.ICON_YELLOW) | |
self.icon_break = self.get_icon('break', self.ICON_RED) | |
# Setup context menu | |
self.menu = gtk.Menu() | |
self.quit = gtk.MenuItem("Quit") | |
self.menu.append(self.quit) | |
self.quit.connect('activate', self.destroy) | |
self.quit.show() | |
# Setup Status Icon | |
self.statusicon = gtk.StatusIcon() | |
self.statusicon.connect('popup-menu', self.popup_menu) | |
# Starting pomodoro rotation | |
worker = threading.Thread(target=self.start) | |
worker.setDaemon(True) | |
worker.start() | |
def popup_menu(self, status, button, time): | |
self.menu.popup(None, None, None, button, time) | |
def destroy(self, widget, data=None): | |
self.notification("Done.", "You're done, hurray! Good job.", self.icon_break) | |
gtk.main_quit() | |
def get_icon(self, icon_name, default_icon_data): | |
icon = os.path.join(self.cache_dir, icon_name + '.svg') | |
if not os.path.exists(icon): | |
if not os.path.isdir(self.cache_dir): | |
os.makedirs(self.cache_dir) | |
f = open(icon, 'w') | |
f.write(default_icon_data) | |
f.close() | |
return icon | |
def notification(self, title, message, icon): | |
n = pynotify.Notification(title, message, icon) | |
n.show() | |
def get_datetime(self, sec = None): | |
if sec is None: | |
ret = datetime.now() | |
else: | |
ret = (datetime.now() + timedelta(seconds = sec)) | |
return ret.strftime("%Y/%m/%d %H:%M:%S") | |
def work(self): | |
self.show_status_icon(self.icon_work, work_time) | |
title = "GET TO WORK!" | |
message = "for %s minutes" % option.work_time | |
self.notification(title, message, self.icon_work) | |
time.sleep(work_time) | |
def play(self): | |
self.show_status_icon(self.icon_play, play_time) | |
title = "For %s minutes," % option.play_time | |
message = "Take a well-deserved break, dear." | |
self.notification(title, message, self.icon_play) | |
time.sleep(play_time) | |
def longbreak(self): | |
self.show_status_icon(self.icon_break, break_time) | |
title = "Take a long break." | |
message = "It's time for a %s break. So chill for a bit, but be back soon!" % option.break_time | |
self.notification(title, message, self.icon_break) | |
time.sleep(break_time) | |
def show_status_icon(self, icon, diff_sec = None): | |
self.statusicon.set_from_file(icon) | |
if diff_sec is None: | |
self.statusicon.set_tooltip("Started at " + self.get_datetime()) | |
else: | |
self.statusicon.set_tooltip("Started at %s\nWill end at %s" % (self.get_datetime(), self.get_datetime(diff_sec))) | |
def start(self): | |
while True: | |
self.work() | |
self.play() | |
self.work() | |
self.play() | |
self.work() | |
self.play() | |
self.work() | |
self.longbreak() | |
if __name__ == '__main__': | |
pynotify.init("pomodoro") | |
pomodoro = Pomodoro(); | |
gtk.gdk.threads_init() | |
gtk.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment