-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
163 lines (127 loc) · 4.05 KB
/
helpers.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
import subprocess
import json
from os import path
from toggl import api, exceptions
import pathlib
CACHE_FILE_PATH = path.join(path.dirname(path.abspath(__file__)), "projects.json")
def show_message_dialog(title, text, type):
supported_dialog = ("error", "question", "warning", "info")
dialog_bash = [
"zenity",
f"--{type}",
f"--title={title}",
f"--text={text}",
"--icon-name=dialog-warning",
"--width=300",
"--height=200",
"--html",
]
reponse = subprocess.run(dialog_bash)
return True if reponse.returncode == 0 else False
def cache_projects():
try:
projects = api.Project.objects.all()
projects = [project.name for project in projects if project.active]
content = {"current_project": None, "projects": projects}
with open(CACHE_FILE_PATH, "w+") as f:
json.dump(content, f)
return projects
except exceptions.TogglConfigException as e:
show_message_dialog(
"ERROR!",
"There is no authentication configuration found!\n\nRun 'toggl me' to configure the toggl.",
"error",
)
return projects
def update_current_project(project):
if not path.exists(CACHE_FILE_PATH):
return
try:
with open(CACHE_FILE_PATH, "r+") as f:
content = json.load(f)
content["current_project"] = project
f.seek(0)
json.dump(content, f)
f.truncate()
except:
pass
def get_cached_projects():
try:
with open(CACHE_FILE_PATH) as f:
content = json.load(f)
current_project, projects = content["current_project"], content["projects"]
if current_project and current_project in projects:
projects.remove(current_project)
projects.insert(0, current_project)
return projects
except:
return cache_projects()
def get_all_projects():
return get_cached_projects() if path.exists(CACHE_FILE_PATH) else cache_projects()
def show_select_project_dialog():
all_projects = get_all_projects()
dialog_bash = [
"zenity",
"--list",
"--title=Choose Project",
"--column=Project Name",
"--width=400",
"--height=400",
]
for project in all_projects:
dialog_bash.append(project)
reponse = subprocess.run(dialog_bash, capture_output=True, text=True)
return reponse.stdout.replace("\n", "")
def show_notification(title="", description=""):
notify_send_bash_command = [
"notify-send",
title,
description,
]
subprocess.run(notify_send_bash_command)
def start_time_track(project_name=""):
start_time_bash_command = [
"toggl",
"start",
"--project",
project_name,
]
response = subprocess.run(start_time_bash_command)
if response.returncode != 0:
show_notification(
"ERROR!",
f"Invalid project name!",
)
return
show_notification(
"Time tracking started",
f"Time tracking started for the project: '{project_name}'",
)
def fetch_current_time_entry():
try:
return api.TimeEntry.objects.current()
except exceptions.TogglConfigException as e:
show_message_dialog(
"ERROR!",
"""There is no authentication configuration found!\n\nRun 'toggl me' to configure the toggl.""",
"error",
)
def show_input_dialog(title="", text=""):
dialog_bash = [
"zenity",
"--entry",
f"--title={title}",
f"--text={text}",
"--width=400",
"--height=200",
]
return subprocess.run(dialog_bash, capture_output=True, text=True)
def stop_current_time_entry(time_entry, description=" "):
if not time_entry.is_running:
return
time_entry.description = description
time_entry.stop_and_save()
show_notification(
"Time tracking stopped",
f"Time tracking stopped for the project: '{time_entry.project.name}'",
)