1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Handle application configuration.
21
22 This module is global and can be used by all modules. Before accessing
23 configurations, the read function should be called. To save the current
24 configuration back to file, call the write method.
25 """
26
27
28 import sys
29 from ConfigParser import ConfigParser
30 from ConfigParser import DEFAULTSECT
31 import os.path
32
33
34
35 WINDOW_WIDTH = "window_width"
36 WINDOW_HEIGHT = "window_height"
37 WINDOW_XPOS = "window xpos"
38 WINDOW_YPOS = "window ypos"
39 WINDOW_MAXIMIZED = "window_maximized"
40 SHOW_SIDEBAR = "show_sidebar"
41 SHOW_LEGEND = "show_legend"
42 SIDEBAR_WIDTH = "sidebar_width"
43 RECENT_FILES = "recent_files"
44 OPEN_RECENT_AT_STARTUP = "open_recent_at_startup"
45 BALLOON_ON_HOVER = "balloon_on_hover"
46 WEEK_START = "week_start"
47 USE_WIDE_DATE_RANGE = "use_wide_date_range"
48 USE_INERTIAL_SCROLLING = "use_inertial_scrolling"
49 DEFAULTS = {
50 WINDOW_WIDTH: "900",
51 WINDOW_HEIGHT: "500",
52 WINDOW_XPOS: "-1",
53 WINDOW_YPOS: "-1",
54 WINDOW_MAXIMIZED: "False",
55 SHOW_SIDEBAR: "True",
56 SIDEBAR_WIDTH: "200",
57 SHOW_LEGEND: "True",
58 OPEN_RECENT_AT_STARTUP: "True",
59 RECENT_FILES: "",
60 BALLOON_ON_HOVER: "True",
61 WEEK_START: "monday",
62 USE_WIDE_DATE_RANGE: "False",
63 USE_INERTIAL_SCROLLING : "False"
64 }
65
66 MAX_NBR_OF_RECENT_FILES_SAVED = 5
67 ENCODING = "utf-8"
68
69
74
75
77 """
78 Provide read and write access to application configuration settings.
79
80 Built as a wrapper around ConfigParser: Properties exist to read and write
81 values but ConfigParser does the actual reading and writing of the
82 configuration file.
83 """
84
86 self.path = path
87 self.config_parser = ConfigParser(DEFAULTS)
88
90 """Read settings from file specified in constructor."""
91 self.config_parser.read(self.path)
92
94 """
95 Write settings to file specified in constructor and raise IOError if
96 failed.
97 """
98 f = open(self.path, "w")
99 try:
100 self.config_parser.write(f)
101 finally:
102 f.close()
103
108 width, height = size
109 self.config_parser.set(DEFAULTSECT, WINDOW_WIDTH, str(width))
110 self.config_parser.set(DEFAULTSECT, WINDOW_HEIGHT, str(height))
111 window_size = property(get_window_size, set_window_size)
112
114 width, height = self.get_window_size()
115
116
117 xpos = max(-width + 100,
118 self.config_parser.getint(DEFAULTSECT, WINDOW_XPOS))
119
120 ypos = max(0, self.config_parser.getint(DEFAULTSECT, WINDOW_YPOS))
121 return (xpos, ypos)
122
124 xpos, ypos = pos
125 self.config_parser.set(DEFAULTSECT, WINDOW_XPOS, str(xpos))
126 self.config_parser.set(DEFAULTSECT, WINDOW_YPOS, str(ypos))
127 window_pos = property(get_window_pos, set_window_pos)
128
133 window_maximized = property(get_window_maximized, set_window_maximized)
134
139 show_sidebar = property(get_show_sidebar, set_show_sidebar)
140
142 return self.config_parser.getboolean(DEFAULTSECT, SHOW_LEGEND)
145 show_legend = property(get_show_legend, set_show_legend)
146
151 sidebar_width = property(get_sidebar_width, set_sidebar_width)
152
154 ro = self.config_parser.get(DEFAULTSECT, RECENT_FILES).decode(ENCODING).split(",")
155
156
157 ro_filtered = [x for x in ro if x]
158 return ro_filtered
159 recently_opened = property(get_recently_opened)
160
162 if path in [":tutorial:"]:
163
164 return
165 if isinstance(path, str):
166
167
168 path = path.decode(sys.getfilesystemencoding())
169 abs_path = os.path.abspath(path)
170 current = self.recently_opened
171
172 if abs_path in current:
173 current.remove(abs_path)
174 current.insert(0, abs_path)
175 self.config_parser.set(DEFAULTSECT, RECENT_FILES,
176 (",".join(current[:MAX_NBR_OF_RECENT_FILES_SAVED])).encode(ENCODING))
177
182 open_recent_at_startup = property(get_open_recent_at_startup,
183 set_open_recent_at_startup)
184
189 balloon_on_hover = property(get_balloon_on_hover, set_balloon_on_hover)
190
197 week_start = property(get_week_start, set_week_start)
198
203 use_wide_date_range = property(get_use_wide_date_range, set_use_wide_date_range)
204
209 use_inertial_scrolling = property(get_use_inertial_scrolling, set_use_inertial_scrolling)
210