From 0fcec20db1bc974e098debdf2986ead27cf3cd7d Mon Sep 17 00:00:00 2001 From: Adrien Lemaire Date: Mon, 15 Apr 2013 18:51:09 +0900 Subject: adding new pomodoro plugin --- examples/pomodoro.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/pomodoro.py (limited to 'examples/pomodoro.py') diff --git a/examples/pomodoro.py b/examples/pomodoro.py new file mode 100644 index 0000000..86ffdb6 --- /dev/null +++ b/examples/pomodoro.py @@ -0,0 +1,65 @@ +""" +:Requirements: libnotify, python-gobject +""" +from datetime import timedelta +from gi.repository import Notify +from time import time + +POMO = 1500 # 60 * 25 +SHORT_REST = 300 # 60 * 5 +LONG_REST = 1800 # 60 * 30 +NB_REST = 4 + + +class Py3status: + """ + Pomodoro method + """ + pomo = POMO + rest = SHORT_REST + nb_rest = NB_REST + + def pomodoro(self, json, i3status_config): + """ + This function will update a timer in the i3status bar for 25 minutes, + then print a red "Break time" message for 5 minutes. + """ + if not self.pomo: + self.rest -= 1 + timer = self.rest + full_text = 'Break time' + color = 'color_bad' + else: + self.pomo -= 1 + timer = self.pomo + full_text = 'Pomodoro' + color = 'color_degraded' + + if not timer: + # Pomodoro or Rest is finished, send notification + Notify.init('Pomodoro') + notification = Notify.Notification.new ( + 'Attention:', + '{} is finished'.format(full_text), + 'dialog-information', + ) + notification.show() + if not self.rest: + # Rest is finished, reinitialize data + self.nb_rest -= 1 + self.pomo = POMO + self.rest = SHORT_REST + if not self.nb_rest: + # Number of small rest reached, go for long rest + self.rest = LONG_REST + self.nb_rest = NB_REST + + response = { + 'cached_until' : time(), + 'color': i3status_config[color], + 'full_text': '{}: {}'.format( + full_text, str(timedelta(seconds=timer))[2:]), + 'name': 'pomodoro', + } + + return (0, response) -- cgit v1.3 From 402d199c397405cb60c5a254cfd87608945f506c Mon Sep 17 00:00:00 2001 From: Adrien Lemaire Date: Mon, 15 Apr 2013 18:54:17 +0900 Subject: separate code in send_notification method --- examples/pomodoro.py | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'examples/pomodoro.py') diff --git a/examples/pomodoro.py b/examples/pomodoro.py index 86ffdb6..83c6643 100644 --- a/examples/pomodoro.py +++ b/examples/pomodoro.py @@ -1,6 +1,3 @@ -""" -:Requirements: libnotify, python-gobject -""" from datetime import timedelta from gi.repository import Notify from time import time @@ -27,39 +24,48 @@ class Py3status: if not self.pomo: self.rest -= 1 timer = self.rest - full_text = 'Break time' + self.full_text = 'Break time' color = 'color_bad' else: self.pomo -= 1 timer = self.pomo - full_text = 'Pomodoro' + self.full_text = 'Pomodoro' color = 'color_degraded' if not timer: # Pomodoro or Rest is finished, send notification - Notify.init('Pomodoro') - notification = Notify.Notification.new ( - 'Attention:', - '{} is finished'.format(full_text), - 'dialog-information', - ) - notification.show() - if not self.rest: - # Rest is finished, reinitialize data - self.nb_rest -= 1 - self.pomo = POMO - self.rest = SHORT_REST - if not self.nb_rest: - # Number of small rest reached, go for long rest - self.rest = LONG_REST - self.nb_rest = NB_REST + self.send_notification() response = { 'cached_until' : time(), 'color': i3status_config[color], - 'full_text': '{}: {}'.format( - full_text, str(timedelta(seconds=timer))[2:]), + 'self.full_text': '{}: {}'.format( + self.full_text, str(timedelta(seconds=timer))[2:]), 'name': 'pomodoro', } return (0, response) + + def send_notification(self): + """ + :Requirements: libnotify, python-gobject + """ + + Notify.init('Pomodoro') + notification = Notify.Notification.new ( + 'Attention:', + '{} is finished'.format(self.full_text), + 'dialog-information', + ) + notification.show() + + if not self.rest: + # Rest is finished, reinitialize data + self.nb_rest -= 1 + self.pomo = POMO + self.rest = SHORT_REST + + if not self.nb_rest: + # Number of small rest reached, go for long rest + self.rest = LONG_REST + self.nb_rest = NB_REST -- cgit v1.3 From 0ab72f49ecf89c3a73a8de4207612985749ceb3f Mon Sep 17 00:00:00 2001 From: Adrien Lemaire Date: Mon, 15 Apr 2013 19:09:25 +0900 Subject: define watchdog values to listen, and their action --- examples/pomodoro.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'examples/pomodoro.py') diff --git a/examples/pomodoro.py b/examples/pomodoro.py index 83c6643..397af44 100644 --- a/examples/pomodoro.py +++ b/examples/pomodoro.py @@ -15,12 +15,27 @@ class Py3status: pomo = POMO rest = SHORT_REST nb_rest = NB_REST + default_response = { + 'full_text': '', + 'name': 'pomodoro', + } def pomodoro(self, json, i3status_config): """ This function will update a timer in the i3status bar for 25 minutes, then print a red "Break time" message for 5 minutes. """ + status = self.get_status() + + if status == 'stop': + self.pomo = POMO + self.rest = SHORT_REST + self.nb_rest = NB_REST + return self.default_response + elif status == 'pause': + return self.default_response + + # Else run normally if not self.pomo: self.rest -= 1 timer = self.rest @@ -36,16 +51,28 @@ class Py3status: # Pomodoro or Rest is finished, send notification self.send_notification() - response = { + response = self.default_response.copy() + response.update({ 'cached_until' : time(), 'color': i3status_config[color], - 'self.full_text': '{}: {}'.format( + 'full_text': '{}: {}'.format( self.full_text, str(timedelta(seconds=timer))[2:]), - 'name': 'pomodoro', - } + }) return (0, response) + def get_status(self): + """ + Read the watchdog file and get the status value. Value is modifying via + i3 key mapping. + + Value can be: + * start + * pause + * stop (default) + """ + pass + def send_notification(self): """ :Requirements: libnotify, python-gobject -- cgit v1.3 From 01372d6e638dc2bfa206be09fc3082c03f1fc56f Mon Sep 17 00:00:00 2001 From: Adrien Lemaire Date: Mon, 15 Apr 2013 19:20:49 +0900 Subject: reading log file for value. Do I really need watchdog ? --- examples/pomodoro.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'examples/pomodoro.py') diff --git a/examples/pomodoro.py b/examples/pomodoro.py index 397af44..143adf5 100644 --- a/examples/pomodoro.py +++ b/examples/pomodoro.py @@ -1,11 +1,17 @@ from datetime import timedelta from gi.repository import Notify +import os.path from time import time +#from time import sleep +#from watchdog.observers import Observer +#from watchdog.events import LoggingEventHandler + POMO = 1500 # 60 * 25 SHORT_REST = 300 # 60 * 5 LONG_REST = 1800 # 60 * 30 NB_REST = 4 +WATCHDOG_FILE = os.path.join(os.path.expanduser('~'), '.i3', 'watchdog.log') class Py3status: @@ -71,7 +77,10 @@ class Py3status: * pause * stop (default) """ - pass + with open(WATCHDOG_FILE, 'r') as f: + status = f.read() + + return status def send_notification(self): """ @@ -96,3 +105,16 @@ class Py3status: # Number of small rest reached, go for long rest self.rest = LONG_REST self.nb_rest = NB_REST + + +#if __name__ == "__main__": + #event_handler = LoggingEventHandler() + #observer = Observer() + #observer.schedule(event_handler, path=WATCHDOG_FILE, recursive=True) + #observer.start() + #try: + #while True: + #sleep(1) + #except KeyboardInterrupt: + #observer.stop() + #observer.join() -- cgit v1.3 From 6b526bdc46cebb7d5f3df7d06eb2d723888ab854 Mon Sep 17 00:00:00 2001 From: Adrien Lemaire Date: Mon, 15 Apr 2013 20:40:27 +0900 Subject: plugin now working with keys to start/stop/pause --- examples/pomodoro.py | 77 ++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 35 deletions(-) (limited to 'examples/pomodoro.py') diff --git a/examples/pomodoro.py b/examples/pomodoro.py index 143adf5..7af996b 100644 --- a/examples/pomodoro.py +++ b/examples/pomodoro.py @@ -1,17 +1,31 @@ +""" +REQUIREMENTS +------------ +* libnotify +* python-gobject + + +INSTALLATION +------------ +Please add the following lines in your ~/.i3/config file:: + + bindsym $mod+minus exec echo "stop" > ~/.i3/pomodoro.log + bindsym $mod+equal exec echo "pause" > ~/.i3/pomodoro.log + bindsym $mod+plus exec echo "start" > ~/.i3/pomodoro.log + +""" from datetime import timedelta from gi.repository import Notify import os.path from time import time -#from time import sleep -#from watchdog.observers import Observer -#from watchdog.events import LoggingEventHandler -POMO = 1500 # 60 * 25 -SHORT_REST = 300 # 60 * 5 +LOG_FILE = os.path.join(os.path.expanduser('~'), '.i3', 'pomodoro.log') LONG_REST = 1800 # 60 * 30 NB_REST = 4 -WATCHDOG_FILE = os.path.join(os.path.expanduser('~'), '.i3', 'watchdog.log') +POMO = 1500 # 60 * 25 +POSITION = 0 +SHORT_REST = 300 # 60 * 5 class Py3status: @@ -21,10 +35,14 @@ class Py3status: pomo = POMO rest = SHORT_REST nb_rest = NB_REST - default_response = { - 'full_text': '', - 'name': 'pomodoro', - } + + @property + def response(self): + return { + 'cached_until' : time(), + 'full_text': '', + 'name': 'pomodoro', + } def pomodoro(self, json, i3status_config): """ @@ -37,39 +55,41 @@ class Py3status: self.pomo = POMO self.rest = SHORT_REST self.nb_rest = NB_REST - return self.default_response - elif status == 'pause': - return self.default_response + return (POSITION, self.response) # Else run normally if not self.pomo: - self.rest -= 1 + if status != 'pause': + self.rest -= 1 timer = self.rest self.full_text = 'Break time' color = 'color_bad' else: - self.pomo -= 1 + if status != 'pause': + self.pomo -= 1 timer = self.pomo self.full_text = 'Pomodoro' color = 'color_degraded' + if status == 'pause': + color = 'color_good' + if not timer: # Pomodoro or Rest is finished, send notification self.send_notification() - response = self.default_response.copy() + response = self.response response.update({ - 'cached_until' : time(), 'color': i3status_config[color], 'full_text': '{}: {}'.format( self.full_text, str(timedelta(seconds=timer))[2:]), }) - return (0, response) + return (POSITION, response) def get_status(self): """ - Read the watchdog file and get the status value. Value is modifying via + Read the log file and get the status value. Value is modifying via i3 key mapping. Value can be: @@ -77,14 +97,14 @@ class Py3status: * pause * stop (default) """ - with open(WATCHDOG_FILE, 'r') as f: + with open(LOG_FILE, 'r') as f: status = f.read() - return status + return status.rstrip('\n') def send_notification(self): """ - :Requirements: libnotify, python-gobject + Print a visual message """ Notify.init('Pomodoro') @@ -105,16 +125,3 @@ class Py3status: # Number of small rest reached, go for long rest self.rest = LONG_REST self.nb_rest = NB_REST - - -#if __name__ == "__main__": - #event_handler = LoggingEventHandler() - #observer = Observer() - #observer.schedule(event_handler, path=WATCHDOG_FILE, recursive=True) - #observer.start() - #try: - #while True: - #sleep(1) - #except KeyboardInterrupt: - #observer.stop() - #observer.join() -- cgit v1.3 From 949a27abafde47942a9e0354f28a3270a94640a2 Mon Sep 17 00:00:00 2001 From: Adrien Lemaire Date: Mon, 15 Apr 2013 22:56:02 +0900 Subject: handle case log file does not exists --- examples/pomodoro.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'examples/pomodoro.py') diff --git a/examples/pomodoro.py b/examples/pomodoro.py index 7af996b..2094448 100644 --- a/examples/pomodoro.py +++ b/examples/pomodoro.py @@ -97,6 +97,9 @@ class Py3status: * pause * stop (default) """ + if not os.path.isfile(LOG_FILE): + return 'stop' + with open(LOG_FILE, 'r') as f: status = f.read() -- cgit v1.3