-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.py
68 lines (53 loc) · 2.37 KB
/
config.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
import logging
import os
from configparser import SafeConfigParser, NoSectionError, NoOptionError
from os.path import expanduser
class Config:
CONFIG_DIR = expanduser('~') + '/.config/bamboohr'
CONFIG_FILE = CONFIG_DIR + "/google-calendar-sync.conf"
BAMBOO_SECTION = 'bamboohr'
GCAL_SECTION = 'google-calendar'
config = SafeConfigParser()
def __init__(self):
self._read_config()
def _read_config(self):
if not os.path.exists(self.CONFIG_DIR):
logging.debug('Creating config directory')
os.makedirs(self.CONFIG_DIR)
self.config.read(self.CONFIG_FILE)
for section in [self.BAMBOO_SECTION, self.GCAL_SECTION]:
if section not in self.config.sections():
self.config.add_section(section)
try:
self.bamboo = self.config.items(self.BAMBOO_SECTION)
# Check all the mandatory things
c = self.get_config()
c[self.BAMBOO_SECTION]['company']
c[self.BAMBOO_SECTION]['token']
c[self.BAMBOO_SECTION]['employee_id']
c[self.GCAL_SECTION]['calendar_id']
except (NoSectionError, NoOptionError, KeyError):
self._input_config()
def _input_config(self):
self.config.set(self.BAMBOO_SECTION, 'company', input('BambooHR Company: '))
self.config.set(self.BAMBOO_SECTION, 'employee_id', input('Employee ID: '))
token = raw_input('BambooHR Access Token (leave blank to use username/password): ')
if token:
self.config.set(self.BAMBOO_SECTION, 'token', token)
else:
logging.debug('No token entered - using username/password')
self.config.set(self.BAMBOO_SECTION, 'user', raw_input('BambooHR username: '))
calendar_id = raw_input("Google Calendar ID (use 'personal' for the your calendar): ") or 'personal'
self.config.set(self.GCAL_SECTION, 'calendar_id',
calendar_id)
self._save()
def save_token(self, token):
self.config.set(self.BAMBOO_SECTION, 'token', token)
self._save()
def _save(self):
with open(self.CONFIG_FILE, 'w', 0o600) as configfile:
self.config.write(configfile)
def get_config(self):
return {s: dict(self.config.items(s)) for s in self.config.sections()}
if __name__ == '__main__':
print(Config().get_config())