-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.py
192 lines (157 loc) · 5.12 KB
/
settings.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
import configparser
import os
import arrow
import pytz
from arrow.parser import ParserError
from src.utils import safe_input as input
weekday_dict = {
"monday": 0,
"tuesday": 1,
"wednesday": 2,
"thursday": 3,
"friday": 4,
"saturday": 5,
"sunday": 6,
}
zones = pytz.all_timezones # list of usable timezones
def confirm_input(user_input):
print("I got the following: ")
if type(user_input) == list:
for i in user_input:
print(i)
else:
print(user_input)
confirm = input("Is this accurate? ")
if "y" in confirm.lower():
return True
else:
return False
def get_credentials_directory(location=None):
"""
Gets credentials directory from user input
"""
if not location:
try:
credentials = os.getenv("HOME") + "/etc/keys/google-api/"
return get_credentials_directory(credentials)
except TypeError:
pass
elif os.path.exists(os.path.join(location + "/credentials.json")):
credentials = location
if confirm_input(credentials):
return credentials
else:
return get_credentials_directory()
elif "credentials.json" in location:
credentials = os.path.dirname(location)
if confirm_input(credentials):
return credentials
else:
return get_credentials_directory()
loc = input("Path to credentials.json: ")
return get_credentials_directory(location=loc)
def get_timezone(zones: list):
"""
Gets timezone from user input
"""
user_input = input(
'What timezone do you reside in? (Format: "US/Pacific", "US/Eastern", etc): '
)
smaller_zones = []
if user_input not in zones:
for zone in zones:
if user_input.lower() in zone.lower():
smaller_zones.append(zone)
if len(smaller_zones):
print("Zones that match that input are: ")
for zone in smaller_zones:
print(zone)
return get_timezone(smaller_zones)
else:
if confirm_input(user_input):
return user_input
else:
return get_timezone()
def get_work_hours():
"""
Gets work hours from user input
"""
user_reply = input("What are your work hours? (Format: 9:00 AM-5:00 PM) ")
user_reply_list = user_reply.split("-")
for i in user_reply_list:
try:
arrow.get(i, "H:mm A")
except ParserError:
print("Invalid Output")
return get_work_hours()
if confirm_input(user_reply_list):
return user_reply_list
else:
return get_work_hours()
def get_weekends():
"""
Gets weekends from user input
"""
user_reply = input("What days of the week are your weekends? ")
days = weekday_dict.keys()
ret = []
for day in days:
if day.lower() in user_reply.lower():
ret.append(day)
if confirm_input(ret):
return ret
else:
return get_weekends()
def set_weekend(parser, days: list):
for day in days:
parser.set("Weekend Days", day.lower(), "True")
def setup_settings():
"""
Create settings.ini from user input
"""
if not os.path.exists("settings.ini"):
parser = configparser.ConfigParser(interpolation=None)
parser.add_section("Settings")
parser.add_section("Weekend Days")
parser.add_section("Calendars")
for k, v in weekday_dict.items():
parser.set("Weekend Days", k, "False")
parser.set("Settings", "credentials_dir", get_credentials_directory())
weekend_days = get_weekends()
set_weekend(parser, weekend_days)
parser.set("Settings", "timezone", get_timezone(zones))
work_hours = get_work_hours()
parser.set("Settings", "start_work", work_hours[0])
parser.set("Settings", "end_work", work_hours[1])
with open("settings.ini", "w") as ini:
parser.write(ini)
setup_settings()
parser = configparser.ConfigParser(interpolation=None)
parser.read("settings.ini")
CREDENTIALS_DIR = parser.get("Settings", "credentials_dir")
TIMEZONE = parser.get("Settings", "timezone")
START_WORK = parser.get("Settings", "start_work")
END_WORK = parser.get("Settings", "end_work")
def get_weekend_num():
weekend = []
for i in parser["Weekend Days"]:
if parser.getboolean("Weekend Days", i):
weekend.append(weekday_dict[i])
return weekend
weekend_num = get_weekend_num()
calendars_imported = len(
parser.options("Calendars")
) # returns 0 (False) if calendars not imported
def set_calendar(cal_name, cal_id):
parser.set("Calendars", cal_name, cal_id)
with open("settings.ini", "w") as config:
parser.write(config)
def list_imported_calendars():
for cal, id in parser.items("Calendars"):
print(cal, ":", id)
return parser.items("Calendars")
def get_imported_calendar_by_name(name: str):
for cal, id in parser.items("Calendars"):
if name.lower() == cal.lower() and name.lower() != "primary":
return id
return name # print("Calendar with name " + name + " not found")