11#!/usr/bin/python3
2- # v0.3 .0
2+ # v0.4 .0
33
44import sys
55import tkinter as tk
1515addcron_path = f"{ temppath } /addcron.sh"
1616sound_path = f"{ temppath } /sound.wav"
1717
18+ def write_work_mode (mode ):
19+ with open (workmode_path ,"w" ) as file_write :
20+ file_write .write (mode )
21+ file_write .close ()
22+ subprocess .call (['sh' ,addcron_path ,mode ,temppath [:- 1 ]])
23+ print (f"{ mode } work mode" )
24+
25+ def read_work_mode ():
26+ with open (workmode_path ,"r" ) as file_read :
27+ mode = file_read .readlines ()[0 ]
28+ return mode
29+ file_read .close ()
30+
1831def check_next ():
1932 current_crontab = subprocess .check_output (['crontab' ,'-l' ])
2033 current_crontab = current_crontab .decode ('utf-8' )
@@ -26,11 +39,10 @@ def check_next():
2639 current_time = datetime .datetime .now ()
2740 current_time = current_time .minute
2841 correct_entry = correct_entry .split ()[0 ]
29- first_time = correct_entry .split (',' )[0 ]
30- first_time = int (first_time )
31- second_time = correct_entry .split (',' )[1 ]
32- second_time = int (second_time )
33- if current_time > first_time and current_time > second_time :
42+ first_time = int (correct_entry .split (',' )[0 ])
43+ second_time = int (correct_entry .split (',' )[1 ])
44+
45+ if current_time > first_time and current_time >= second_time :
3446 return 60 - current_time + first_time
3547 elif current_time >= first_time and current_time < second_time :
3648 return second_time - current_time
@@ -42,32 +54,19 @@ def check_next():
4254arg_count = len (sys .argv )
4355if arg_count > 1 :
4456 if sys .argv [1 ] == "set" :
45- print ("set work mode" )
46- with open (workmode_path ,"w" ) as file_write :
47- file_write .write ("set" )
48- file_write .close ()
49- subprocess .call (['sh' ,addcron_path ,'set' ,temppath [:- 1 ]])
57+ write_work_mode ("set" )
5058 elif sys .argv [1 ] == "unset" :
51- print ("unset work mode" )
52- with open (workmode_path ,"w" ) as file_write :
53- file_write .write ("unset" )
54- file_write .close ()
55- subprocess .call (['sh' ,addcron_path ,'unset' ,temppath [:- 1 ]])
59+ write_work_mode ("unset" )
5660 elif sys .argv [1 ] == "get" :
57- with open (workmode_path ,"r" ) as file_read :
58- print (f"current work mode: { file_read .readlines ()[0 ]} " )
59- file_read .close ()
61+ print (f"Current mode is { read_work_mode ()} " )
6062 elif sys .argv [1 ] == "next" :
6163 try :
6264 next = check_next ()
6365 print (f"Next reminder is in { next } minutes." )
6466 except :
6567 print ("Please enable work mode to check next reminder" )
6668 elif sys .argv [1 ] == "update" :
67- mode = ""
68- with open (workmode_path ,"r" ) as file_read :
69- mode = file_read .readlines ()[0 ]
70- if mode == "set" :
69+ if read_work_mode () == "set" :
7170 subprocess .call (['sh' ,addcron_path , 'unset' ,temppath [:- 1 ]])
7271 subprocess .call (['sh' ,addcron_path , 'set' ,temppath [:- 1 ]])
7372 next = check_next ()
@@ -78,24 +77,21 @@ def check_next():
7877 print ("Unknown command, please try again" )
7978 exit ()
8079else :
81- mode = ""
82- with open (workmode_path ,"r" ) as file_read :
83- mode = file_read .readlines ()[0 ]
84- if mode == "set" :
85- os_type = subprocess .check_output (['uname' ])
86- os_type = os_type .decode ('utf-8' ).strip ("\n " )
87- process = None
88- if os_type == "Darwin" :
89- process = subprocess .Popen (['afplay' ,sound_path ])
90- elif os_type == "Linux" :
91- process = subprocess .Popen (['aplay' ,sound_path ])
92- else :
93- print ("Your OS is not yet supported." )
94- root = tk .Tk ()
95- root .geometry ("400x100" )
96- root .attributes ("-topmost" , True )
97- root .eval ('tk::PlaceWindow . center' )
98- root .title ("TAKE A BREAK!" )
99- root .mainloop ()
100- if process :
101- process .terminate ()
80+ if read_work_mode () == "set" :
81+ os_type = subprocess .check_output (['uname' ])
82+ os_type = os_type .decode ('utf-8' ).strip ("\n " )
83+ process = None
84+ if os_type == "Darwin" :
85+ process = subprocess .Popen (['afplay' ,sound_path ])
86+ elif os_type == "Linux" :
87+ process = subprocess .Popen (['aplay' ,sound_path ])
88+ else :
89+ print ("Your OS is not yet supported." )
90+ root = tk .Tk ()
91+ root .geometry ("400x100" )
92+ root .attributes ("-topmost" , True )
93+ root .eval ('tk::PlaceWindow . center' )
94+ root .title ("TAKE A BREAK!" )
95+ root .mainloop ()
96+ if process :
97+ process .terminate ()
0 commit comments