-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
214 lines (165 loc) · 6.94 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
210
211
212
213
import argparse
import configparser
import os
import subprocess
import datetime
CONFIG_FILE = 'configs'
NOTIFIER_FILE = 'notifier.sh'
TASK_DURATION = 30
DAEMON_SH = 'daemon.sh'
def notify(header, message, sound):
# os.execl(NOTIFIER_FILE, header, message, sound)
subprocess.call(['./notifier.sh', header, message, sound])
def get_all_projects():
parser = configparser.ConfigParser()
parser.read(CONFIG_FILE)
return parser.items('projects')
def get_current_project():
"""Get the current project by its name and location"""
parser = configparser.ConfigParser()
parser.read(CONFIG_FILE)
name = parser.get('lastproject', 'name')
try:
return { 'name': name, 'loc': parser.get('projects', name) }
except configparser.InterpolationError:
print('No project {0} found. Please select a new project.'.format(name))
return None
def start_daemon(hrs, min):
"""Start an at queue at the given hour and minute.
Keyword arguments:
hrs -- hours (no default)
min -- minute (no default)
"""
# user should start atd
# subprocess.call(['sudo', 'atd'])
time = '{0:0>2}:{1:0>2}'.format(hrs,min)
subprocess.call(['at', time, '-f', DAEMON_SH])
def set_config(section, key, value):
"""Set new config key value in configuration file."""
parser = configparser.RawConfigParser()
parser.read(CONFIG_FILE)
with open(CONFIG_FILE, 'w') as config_file:
parser.set(section, key, value)
parser.write(config_file)
def set_current_project(project):
CURRENT_PROJECT = project
set_config('lastproject', 'name', project[0])
def set_timout_min(min):
set_config('lastproject', 'timeout', min)
def parse_configs(header, key):
parser = configparser.ConfigParser(allow_no_value=True)
parser.read('configs')
return parser.get(header, key)
def log(head, log):
pass
def checkProcessRunning(program):
import psutil
return program in (p.name() for p in psutil.process_iter())
def printCurrentTime():
now = datetime.datetime.now()
return "{0}:{1}".format(now.hour, now.minute)
class SetTimeout(argparse.Action):
"""Sets a new timeout in minutes. When certain minutes/hours are completed then
you are notified about the timeout and the project pauses until you resume.
Currently timeout is only set in minutes.
"""
def __call__(self, parser, namespace, values, option_string=None):
parser = configparser.ConfigParser()
parser.write()
now = datetime.datetime.now()
val = int(values[0]) + now.minute
hours = int(val / 60) + now.hour
min = (val % 60)
print()
class DoProject(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None):
super().__init__(option_strings, dest, nargs, const, default, type, choices, required, help, metavar)
def __call__(self, parser, namespace, values, option_string=None):
if values == 'v':
self.show(values)
elif values == 'r':
self.resume()
elif values == 'p':
self.pause()
elif values == 's':
self.stop()
def list_all_sub_dirs(path=''):
root = parse_configs('directories','root')
print(root)
for subdirs in os.listdir(root):
print(subdirs)
def show(self, values):
# shows all the projects and the current project
print( '{0:15} {1}'.format('Name', 'Location'))
print( 55 * '-')
projects = get_all_projects()
for proj in projects:
print("{0:15} --> {1} ".format(proj[0], proj[1]))
print( 55 * '-')
print("Current Project: {0} ".format(CURRENT_PROJECT['name']))
chosen = input("Choose Project: ")
found = False
for proj in projects:
if chosen == proj[0]:
set_current_project(proj)
print ('Current Project Selected is ' + chosen)
found = True
if not found:
try:
a = int(chosen)
if a > len(projects) or a < 1:
raise ValueError
else:
set_current_project(projects[int(chosen) - 1])
except ValueError:
print('Cant understand what you mean! Nothing will change.')
def resume(self):
# resumes the current project if it has been stopped or paused
# todo: resume the timer
# start atd
# put at using timeout to call this script wth timeout call
# todo: log project is resuming
notify('Resuming {0} project'.format(CURRENT_PROJECT['name']), printCurrentTime(), 'resume')
# start_daemon()
# log
print('resume')
def pause(self):
# pauses the current project timer if it had been resumed
notify('Pausing {0} project'.format(CURRENT_PROJECT['name']), printCurrentTime(), 'pause')
# log
print('pause')
# cancel daemon -> get remaining time -> wait
def stop(self):
# stops the current project timer if it had been resumed
notify('Stopping {0}'.format(CURRENT_PROJECT['name']), printCurrentTime(), 'stopping')
# log
print('stopped')
class LogForProject(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None):
super().__init__(option_strings, dest, nargs, const, default, type, choices, required, help, metavar)
def __call__(self, parser, namespace, values, option_string=None):
if not values: return
if not CURRENT_PROJECT:
print('No current project specified. Specify a project first.')
str = ' '.join(values)
print('LogForProject: ' + ' '.join(values))
class SetLogLevel(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
LOG_LEVEL = values[0]
print(LOG_LEVEL)
# select a current project or none
CURRENT_PROJECT = get_current_project()
if not CURRENT_PROJECT:
CURRENT_PROJECT['name'] = 'No Current Project Selected'
# 0=stopped, 1=started, 2=paused, 3=timeout
CURRENT_STATE = 0
LOG_LEVEL = 0
if not checkProcessRunning('atd'):
print('atd not running. Please sudo run atd program.')
parser = argparse.ArgumentParser(prog='Logger', description='Log your activities')
# options to show, resume, pause, stop the current project
parser.add_argument('-c', choices=['v', 'r', 'p', 's'], action=DoProject, help='v - show/select projects\n r - resume project\n p - pause project\n s - stop project (done for the day, doing another project)')
parser.add_argument('-t', nargs=1, action=SetTimeout, help='set timout for the project.')
parser.add_argument('-l', nargs=1, action=SetLogLevel, help='set log level.')
parser.add_argument('LOG', nargs='*', action=LogForProject, help='log some information on the project')
parser.parse_args();