-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
209 lines (169 loc) · 7.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/which python3
'''
This Script provide a GUI front-end to the TimeWarror CLI application
Author: Ben Mason
'''
__author__ = "Ben Mason"
__copyright__ = "Copyright 2022"
__version__ = "2.0.0"
__email__ = "locutus@the-collective.net"
__status__ = "Production"
import logging
from datetime import datetime
import PySimpleGUI as sg
from twguiapi import TwButtonLogic, TwCalendarClass
import config
def validate_date(date_text: str) -> bool:
''' Validate Date is correct format '''
return_val = False
try:
datetime.strptime(date_text, '%Y-%m-%d')
except ValueError:
return_val = True
return return_val
def validate_time(time_text: str) -> bool:
''' Validate Time is correct format '''
return_val = False
try:
datetime.strptime(time_text, '%H:%M')
except ValueError:
return_val = True
return return_val
def input_error_check(event, values) -> int:
''' Validate inoput values and return an error number of error detected '''
error_val = 0
if values['date'] != '':
if validate_date(values['date']):
sg.popup('Invalid date please use format "YYYY-MM-DD" data entered:', \
values['date'])
error_val = 1
if values['starttime'] != '':
if validate_time(values['starttime']):
sg.popup('Invalid date please use format "HH:MM" data entered:', \
values['starttime'])
error_val = 2
if values['stoptime'] != '':
if validate_time(values['stoptime']):
sg.popup('Invalid date please use format "HH:MM" data entered:', \
values['stoptime'])
error_val = 3
if values['duration'] != '':
if validate_time(values['duration']):
sg.popup('Invalid date please use format "HH:MM" data entered:', \
values['duration'])
error_val = 4
if values['stoptime'] != '' and values['starttime'] != '' and event == 'Modify':
sg.popup('Can only change start or end time, clear one of the fields')
error_val = 5
if (event in ('Track', 'Start')) and values['taskdesc'] == '':
sg.popup('Task Name Can not be Empty')
error_val = 6
return error_val
def main():
''' Main Function '''
table_data = []
logging.basicConfig(level=config.LOGGING_LEVEL, format=config.LOGGING_FORMAT)
if config.ICALBUDDY_ENABLE is True:
twbuttonlogic = TwCalendarClass()
else:
twbuttonlogic = TwButtonLogic()
input_tfields = [ "date", "starttime", "stoptime", "taskdesc", "duration" ]
timew_summary_columns = ['Tag', 'Duration']
#
# Load inital tracked time data
twbuttonlogic.collect_tasks_list()
table_data, tag_len = twbuttonlogic.return_task_table()
logging.debug("tag_len: %s", tag_len)
# if empty table set correct column and rows
table_data.append([" "*27,""])
active_timer = twbuttonlogic.get_active_timer()
#
# Define the window's contents
sg.theme(config.THEME)
layout = [
[ sg.Text("Task:", size=(5, 1), font=config.GLOBAL_FONT), \
sg.Input(key="taskdesc", size=(35,1), font=config.GLOBAL_FONT) ], \
[ sg.Frame(layout=[
[ sg.Text("Start Time:", size=(8, 1), font=config.GLOBAL_FONT), \
sg.Input(key="starttime", size=(12,1), font=config.GLOBAL_FONT), \
sg.Text("EX: 15:00", font=config.GLOBAL_FONT) ],
[ sg.Text("Stop time:", size=(8, 1), font=config.GLOBAL_FONT), \
sg.Input(key="stoptime", size=(12,1), font=config.GLOBAL_FONT), \
sg.Text("EX: 15:00", font=config.GLOBAL_FONT) ],
[ sg.Text("Duration:", size=(8, 1), font=config.GLOBAL_FONT), \
sg.Input(key="duration", size=(12,1), font=config.GLOBAL_FONT), \
sg.Text("EX: 0:20", font=config.GLOBAL_FONT) ],
[ sg.Text("Date:", size=(8, 1), font=config.GLOBAL_FONT), \
sg.Input(key="date", size=(12,1), font=config.GLOBAL_FONT), \
sg.Text("EX: 2020-10-01", size=(16,1), font=config.GLOBAL_FONT) ]
], title='Date') ],
# Buttons
[ sg.Button('Start', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), \
sg.Button('Stop', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), \
sg.Button('Modify', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), \
sg.Button('Track', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), ],
[ sg.Button('Continue', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), \
sg.Button('Delete', size=(config.BUTTON_SIZE, 1),font=config.GLOBAL_FONT), \
sg.Button('Details', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), \
sg.Button('Rename', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT) ], \
[ sg.Button('Refresh', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), ],
# Calendar Buttons inserted here if enabled
# Text Boxes
[ sg.Text("Current Tracking:", size=(13,1), font=config.GLOBAL_FONT),
sg.Input(key="curr_tracking", size=(27,1), default_text=active_timer, \
font=config.GLOBAL_FONT) ],
[ sg.MLine(key="cliout", size=(40,8), font=config.GLOBAL_FONT) ],
[ sg.Text(size=(40,1), key='status_result', font=config.GLOBAL_FONT) ],
[ sg.Text("Todays Tasks", font=config.GLOBAL_FONT) ],
[ sg.Table(values=table_data, headings=timew_summary_columns, max_col_width=30,
display_row_numbers=False,
justification='left',
num_rows=20,
key='timew_table',
tooltip='Todays Data',
font=config.GLOBAL_FONT)]
]
if config.ICALBUDDY_ENABLE:
calendar_buttons = [ sg.Button('Start Meeting', font=config.GLOBAL_FONT), \
sg.Button('Fix Start', size=(config.BUTTON_SIZE, 1), font=config.GLOBAL_FONT), \
sg.Button('Calendar Track', font=config.GLOBAL_FONT)]
layout.insert(4, calendar_buttons)
window = sg.Window('Timewarrior Tracking', layout)
#
####### Event Loop
while True:
logging.debug("****** Start Main Loop ******")
logging.debug("table_data: %s", table_data)
#
# Read Button triggers
event, values = window.read()
# Clean up and Close
if event in (sg.WINDOW_CLOSED, 'Quit'):
break
#
# Input Validation
if input_error_check(event, values) > 0:
continue
#
# Button Logic
result, result_display = twbuttonlogic.button_logic(event, values)
logging.debug("button_logic result: %s", result)
#
# Update list of tracked time for today
table_data, tag_len = twbuttonlogic.return_task_table()
window['timew_table'].update(values=table_data)
active_timer = twbuttonlogic.get_active_timer()
window['curr_tracking'].update(active_timer)
#
# Return results and Status
window['status_result'].update(result_display)
window['cliout'].update(str(result, config.ENCODING))
#
# clear input fields
for i in input_tfields:
window[i].update('')
window.close()
return 0
####### Start Main Function #############
if __name__ == "__main__":
main()