forked from artynusov/MacTimeLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_prefs.py
98 lines (60 loc) · 2.36 KB
/
user_prefs.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
import logging
from ConfigParser import ConfigParser, NoOptionError
import settings
from common.functional import LazyObject
logger = logging.getLogger('user_prefs')
class types(object):
str = 'get'
bool = 'getboolean'
int = 'getint'
float = 'getfloat'
class UserPrefs(object):
defaults = dict(
logDateTimeFormat = ("at %H:%M", types.str),
showWorkTill = (True, types.bool),
logEditCommand = ('open -a TextEdit "%s"', types.str),
showHelpMessageOnStart = (True, types.bool),
dateFormat = ('%m-%d-%Y %H:%M', types.str),
projectSeparator = ('::', types.str),
timeFormat = ('%H:%M', types.str),
workEndTime = ('06:00', types.str),
workDayLength = (3600 * 8, types.int),
timerInterval = (1, types.int),
showDateTime = (False, types.bool),
selectedProject = ('Default', types.str),
soundOnNotification = (False, types.bool),
notificationRepeatTime = (10, types.int),
notificationTime = (40, types.int),
showNotification = (False, types.bool),
startPlaceholder = ("__start__", types.str),
)
root_section = 'root'
def __init__(self):
self.config = ConfigParser()
self.config.read(settings.USER_PREFS_PATH)
if not self.config.has_section(self.root_section):
self.config.add_section(self.root_section)
def __getattr__(self, name):
if name not in self.defaults:
raise ValueError("No such prefernce '{0}'".format(name))
default, type_name = self.defaults[name]
try:
try:
value = getattr(self.config, type_name)(
self.root_section, name)
except (TypeError, ValueError):
logger.warn("Unable to cast type for '{0}', using default value".
format(name, value))
value = default
except NoOptionError:
value = default
return value
def __setattr__(self, name, value):
if name in self.defaults:
self.config.set(self.root_section, name, unicode(value))
else:
super(UserPrefs, self).__setattr__(name, value)
def save(self):
with open(settings.USER_PREFS_PATH, 'wb') as configfile:
self.config.write(configfile)
userPrefs = LazyObject(UserPrefs)