From 575f682a61023f6f43e62039dfc2a2299d6db533 Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Tue, 17 May 2016 13:54:42 +0300 Subject: [PATCH 1/7] Using `utcnow` helps to avoid timezone issues. On timer start timezone is provided to `datetime.now()` call, but on refresh is not. That cause a bug with wrong time difference (it include timezone diff). Didn't dig deep enough to figure out why there is inconsistency with timezones so just pin timezone info as it's not useful anyway. --- i3pystatus/pomodoro.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index 2ccd5a1..2791cb6 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -6,7 +6,6 @@ from datetime import datetime, timedelta from i3pystatus import IntervalModule - class Pomodoro(IntervalModule): """ @@ -50,27 +49,27 @@ class Pomodoro(IntervalModule): self.time = None def run(self): - if self.time and datetime.now() >= self.time: + if self.time and datetime.utcnow() >= self.time: if self.state == 'running': self.state = 'break' if self.breaks == self.short_break_count: - self.time = datetime.now() + \ + self.time = datetime.utcnow() + \ timedelta(seconds=self.long_break_duration) self.breaks = 0 else: - self.time = datetime.now() + \ + self.time = datetime.utcnow() + \ timedelta(seconds=self.break_duration) self.breaks += 1 text = 'Go for a break!' else: self.state = 'running' - self.time = datetime.now() + \ + self.time = datetime.utcnow() + \ timedelta(seconds=self.pomodoro_duration) text = 'Back to work!' self._alarm(text) if self.state == 'running' or self.state == 'break': - min, sec = divmod((self.time - datetime.now()).total_seconds(), 60) + min, sec = divmod((self.time - datetime.utcnow()).total_seconds(), 60) text = '{:02}:{:02}'.format(int(min), int(sec)) color = self.color_running if self.state == 'running' else self.color_break else: @@ -82,7 +81,7 @@ class Pomodoro(IntervalModule): sdict = { 'time': text, - 'current_pomodoro': self.breaks, + 'current_pomodoro': self.breaks + 1, 'total_pomodoro': self.short_break_count + 1, } self.data = sdict @@ -93,7 +92,7 @@ class Pomodoro(IntervalModule): def start(self): self.state = 'running' - self.time = datetime.now() + timedelta(seconds=self.pomodoro_duration) + self.time = datetime.utcnow() + timedelta(seconds=self.pomodoro_duration) self.breaks = 0 def stop(self): From 7ec7739055e397fdf17cd8833665739df5704380 Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Tue, 17 May 2016 14:01:37 +0300 Subject: [PATCH 2/7] Refactor Use integer values for states instead of strings. Feels more optimal. --- i3pystatus/pomodoro.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index 2791cb6..907cbf6 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -6,6 +6,10 @@ from datetime import datetime, timedelta from i3pystatus import IntervalModule +STOPPED = 0 +RUNNING = 1 +BREAK = 2 + class Pomodoro(IntervalModule): """ @@ -44,14 +48,14 @@ class Pomodoro(IntervalModule): def init(self): # state could be either running/break or stopped - self.state = 'stopped' + self.state = STOPPED self.breaks = 0 self.time = None def run(self): if self.time and datetime.utcnow() >= self.time: - if self.state == 'running': - self.state = 'break' + if self.state == RUNNING: + self.state = BREAK if self.breaks == self.short_break_count: self.time = datetime.utcnow() + \ timedelta(seconds=self.long_break_duration) @@ -62,19 +66,19 @@ class Pomodoro(IntervalModule): self.breaks += 1 text = 'Go for a break!' else: - self.state = 'running' + self.state = RUNNING self.time = datetime.utcnow() + \ timedelta(seconds=self.pomodoro_duration) text = 'Back to work!' self._alarm(text) - if self.state == 'running' or self.state == 'break': + if self.state == RUNNING or self.state == BREAK: min, sec = divmod((self.time - datetime.utcnow()).total_seconds(), 60) text = '{:02}:{:02}'.format(int(min), int(sec)) - color = self.color_running if self.state == 'running' else self.color_break + color = self.color_running if self.state == RUNNING else self.color_break else: self.output = { - 'full_text': 'Stopped', + 'full_text': 'Start pomodoro', 'color': self.color_stopped } return @@ -91,12 +95,12 @@ class Pomodoro(IntervalModule): } def start(self): - self.state = 'running' + self.state = RUNNING self.time = datetime.utcnow() + timedelta(seconds=self.pomodoro_duration) self.breaks = 0 def stop(self): - self.state = 'stopped' + self.state = STOPPED self.time = None def _alarm(self, text): From d4f209bfe98e86104242da4a0c6c3b6ced6cbd99 Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Tue, 17 May 2016 14:07:41 +0300 Subject: [PATCH 3/7] Refactor. --- i3pystatus/pomodoro.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index 907cbf6..ed13860 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -75,22 +75,21 @@ class Pomodoro(IntervalModule): if self.state == RUNNING or self.state == BREAK: min, sec = divmod((self.time - datetime.utcnow()).total_seconds(), 60) text = '{:02}:{:02}'.format(int(min), int(sec)) - color = self.color_running if self.state == RUNNING else self.color_break - else: - self.output = { - 'full_text': 'Start pomodoro', - 'color': self.color_stopped + sdict = { + 'time': text, + 'current_pomodoro': self.breaks + 1, + 'total_pomodoro': self.short_break_count + 1, } - return + + color = self.color_running if self.state == RUNNING else self.color_break + text = self.format.format(**sdict) + else: + text = 'Start pomodoro', + color = self.color_stopped - sdict = { - 'time': text, - 'current_pomodoro': self.breaks + 1, - 'total_pomodoro': self.short_break_count + 1, - } - self.data = sdict + self.output = { - 'full_text': self.format.format(**sdict), + 'full_text': text, 'color': color } From ae3721ce87c598715a976e66762b2b5a6f8e9ff3 Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Tue, 17 May 2016 14:11:26 +0300 Subject: [PATCH 4/7] Flake8 fixes --- i3pystatus/pomodoro.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index ed13860..5310777 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -1,15 +1,13 @@ -import os import subprocess -import locale - from datetime import datetime, timedelta - from i3pystatus import IntervalModule + STOPPED = 0 RUNNING = 1 BREAK = 2 + class Pomodoro(IntervalModule): """ @@ -77,17 +75,16 @@ class Pomodoro(IntervalModule): text = '{:02}:{:02}'.format(int(min), int(sec)) sdict = { 'time': text, - 'current_pomodoro': self.breaks + 1, - 'total_pomodoro': self.short_break_count + 1, + 'current_pomodoro': self.breaks + 1, + 'total_pomodoro': self.short_break_count + 1, } - + color = self.color_running if self.state == RUNNING else self.color_break - text = self.format.format(**sdict) + text = self.format.format(**sdict) else: text = 'Start pomodoro', color = self.color_stopped - self.output = { 'full_text': text, 'color': color @@ -108,4 +105,5 @@ class Pomodoro(IntervalModule): text]) subprocess.Popen(['aplay', self.sound, - '-q'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + '-q'], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) From 565e4a66d178675059f1384bde376887acb517d4 Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Wed, 18 May 2016 14:51:54 +0300 Subject: [PATCH 5/7] Change pomodoro counting logic --- i3pystatus/pomodoro.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index 5310777..411b656 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -47,7 +47,8 @@ class Pomodoro(IntervalModule): def init(self): # state could be either running/break or stopped self.state = STOPPED - self.breaks = 0 + self.current_pomodoro = 0 + self.total_pomodoro = self.short_break_count + 1 # and 1 long break self.time = None def run(self): @@ -57,17 +58,16 @@ class Pomodoro(IntervalModule): if self.breaks == self.short_break_count: self.time = datetime.utcnow() + \ timedelta(seconds=self.long_break_duration) - self.breaks = 0 else: self.time = datetime.utcnow() + \ timedelta(seconds=self.break_duration) - self.breaks += 1 text = 'Go for a break!' else: self.state = RUNNING self.time = datetime.utcnow() + \ timedelta(seconds=self.pomodoro_duration) text = 'Back to work!' + self.current_pomodoro = (self.current_pomodoro + 1) % self.total_pomodoro self._alarm(text) if self.state == RUNNING or self.state == BREAK: @@ -75,8 +75,8 @@ class Pomodoro(IntervalModule): text = '{:02}:{:02}'.format(int(min), int(sec)) sdict = { 'time': text, - 'current_pomodoro': self.breaks + 1, - 'total_pomodoro': self.short_break_count + 1, + 'current_pomodoro': self.current_pomodoro + 1, + 'total_pomodoro': self.total_pomodoro, } color = self.color_running if self.state == RUNNING else self.color_break @@ -93,7 +93,7 @@ class Pomodoro(IntervalModule): def start(self): self.state = RUNNING self.time = datetime.utcnow() + timedelta(seconds=self.pomodoro_duration) - self.breaks = 0 + self.current_pomodoro = 0 def stop(self): self.state = STOPPED From cea50cbe5e0b16758c5eada3a16d121d2880c6ce Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Wed, 18 May 2016 14:53:59 +0300 Subject: [PATCH 6/7] Fix PEP8 issue --- i3pystatus/pomodoro.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index 411b656..a291bd2 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -74,9 +74,9 @@ class Pomodoro(IntervalModule): min, sec = divmod((self.time - datetime.utcnow()).total_seconds(), 60) text = '{:02}:{:02}'.format(int(min), int(sec)) sdict = { - 'time': text, - 'current_pomodoro': self.current_pomodoro + 1, - 'total_pomodoro': self.total_pomodoro, + 'time': text, + 'current_pomodoro': self.current_pomodoro + 1, + 'total_pomodoro': self.total_pomodoro } color = self.color_running if self.state == RUNNING else self.color_break From ccb688c404a0c7ad7bbab582f2f8b342b7add7ff Mon Sep 17 00:00:00 2001 From: Sergei Turukin Date: Wed, 18 May 2016 15:49:20 +0300 Subject: [PATCH 7/7] Quick fix --- i3pystatus/pomodoro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/pomodoro.py b/i3pystatus/pomodoro.py index a291bd2..9aec310 100644 --- a/i3pystatus/pomodoro.py +++ b/i3pystatus/pomodoro.py @@ -55,7 +55,7 @@ class Pomodoro(IntervalModule): if self.time and datetime.utcnow() >= self.time: if self.state == RUNNING: self.state = BREAK - if self.breaks == self.short_break_count: + if self.current_pomodoro == self.short_break_count: self.time = datetime.utcnow() + \ timedelta(seconds=self.long_break_duration) else: