Merge lp:~ralsina/ubuntu-sso-client/log-better into lp:ubuntu-sso-client
- log-better
- Merge into trunk
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | Natalia Bidart | ||||
Approved revision: | 772 | ||||
Merged at revision: | 764 | ||||
Proposed branch: | lp:~ralsina/ubuntu-sso-client/log-better | ||||
Merge into: | lp:ubuntu-sso-client | ||||
Diff against target: |
413 lines (+347/-5) 7 files modified
ubuntu_sso/logger.py (+2/-3) ubuntu_sso/utils/ui.py (+2/-2) ubuntu_sso/xdg_base_directory/__init__.py (+40/-0) ubuntu_sso/xdg_base_directory/tests/__init__.py (+16/-0) ubuntu_sso/xdg_base_directory/tests/test_common.py (+56/-0) ubuntu_sso/xdg_base_directory/tests/test_windows.py (+112/-0) ubuntu_sso/xdg_base_directory/windows.py (+119/-0) |
||||
To merge this branch: | bzr merge lp:~ralsina/ubuntu-sso-client/log-better | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Natalia Bidart (community) | Approve | ||
Review via email: mp+72573@code.launchpad.net |
Commit message
Move the windows xdg implementation from u1-client to sso so we can use it in all our projects.
Description of the change
Move the windows xdg implementation from u1-client to sso so we can use it in all our projects.
- 767. By Roberto Alsina
-
added the windows-specific tests
- 768. By Roberto Alsina
-
add __init__
- 769. By Roberto Alsina
-
forgot one
Natalia Bidart (nataliabidart) wrote : | # |
I'm getting all these failures when running ./run-tests:
ubuntu_
121: [E1101, get_data_dir] Module 'ubuntu_
ubuntu_
24: [C0103] Invalid name "load_config_paths" (should match (([A-Z_
25: [C0103] Invalid name "save_config_path" (should match (([A-Z_
26: [C0103] Invalid name "xdg_cache_home" (should match (([A-Z_
27: [C0103] Invalid name "xdg_data_home" (should match (([A-Z_
30: [C0103] Invalid name "load_config_paths" (should match (([A-Z_
31: [C0103] Invalid name "save_config_path" (should match (([A-Z_
32: [C0103] Invalid name "xdg_cache_home" (should match (([A-Z_
33: [C0103] Invalid name "xdg_data_home" (should match (([A-Z_
35: [C0103] Invalid name "ubuntuone_log_dir" (should match (([A-Z_
ubuntu_
1: [C0111] Missing docstring
ubuntu_
28: [W0621, get_special_
23: [C0111, get_special_
30: [F0401, get_special_
45: [C0103] Invalid name "special_folders" (should match (([A-Z_
47: [C0103] Invalid name "home_path" (should match (([A-Z_
48: [C0103] Invalid name "app_local_
49: [C0103] Invalid name "app_global_
52: [C0103] Invalid name "xdg_data_home" (should match (([A-Z_
62: [C0103] Invalid name "xdg_data_dirs" (should match (([A-Z_
66: [C0103] Invalid name "xdg_cache_home" (should match (([A-Z_
70: [C0103] Invalid name "xdg_config_home" (should match (([A-Z_
81: [C0103] Invalid name "xdg_config_dirs" (should match (([A-Z_
83: [C0103] Invalid name "xdg_data_dirs" (should match (([A-Z_
84: [C0103] Invalid name "xdg_config_dirs" (should match (([A-Z_
You may wanna disable C0103 in ubuntu_
Natalia Bidart (nataliabidart) wrote : | # |
Another style fix:
* this import from logging.handlers import RotatingFileHandler should be separated by a blank line from the stdlib imports.
- 770. By Roberto Alsina
-
style fix
- 771. By Roberto Alsina
-
Lint fixes
Roberto Alsina (ralsina) wrote : | # |
Pushed with all the lint fixes (and hey, it was missing making xdg_data_dirs visible)
- 772. By Roberto Alsina
-
publish missing member, lint fixes
Natalia Bidart (nataliabidart) wrote : | # |
Looks good!
Preview Diff
1 | === modified file 'ubuntu_sso/logger.py' | |||
2 | --- ubuntu_sso/logger.py 2011-01-11 19:13:19 +0000 | |||
3 | +++ ubuntu_sso/logger.py 2011-08-23 15:43:28 +0000 | |||
4 | @@ -25,12 +25,11 @@ | |||
5 | 25 | import os | 25 | import os |
6 | 26 | import sys | 26 | import sys |
7 | 27 | 27 | ||
8 | 28 | import xdg.BaseDirectory | ||
9 | 29 | |||
10 | 30 | from logging.handlers import RotatingFileHandler | 28 | from logging.handlers import RotatingFileHandler |
11 | 31 | 29 | ||
12 | 30 | from ubuntu_sso import xdg_base_directory | ||
13 | 32 | 31 | ||
15 | 33 | LOGFOLDER = os.path.join(xdg.BaseDirectory.xdg_cache_home, 'sso') | 32 | LOGFOLDER = os.path.join(xdg_base_directory.xdg_cache_home, 'sso') |
16 | 34 | # create log folder if it doesn't exists | 33 | # create log folder if it doesn't exists |
17 | 35 | if not os.path.exists(LOGFOLDER): | 34 | if not os.path.exists(LOGFOLDER): |
18 | 36 | os.makedirs(LOGFOLDER) | 35 | os.makedirs(LOGFOLDER) |
19 | 37 | 36 | ||
20 | === modified file 'ubuntu_sso/utils/ui.py' | |||
21 | --- ubuntu_sso/utils/ui.py 2011-07-28 22:50:33 +0000 | |||
22 | +++ ubuntu_sso/utils/ui.py 2011-08-23 15:43:28 +0000 | |||
23 | @@ -20,10 +20,10 @@ | |||
24 | 20 | 20 | ||
25 | 21 | import os | 21 | import os |
26 | 22 | import re | 22 | import re |
27 | 23 | import xdg | ||
28 | 24 | import gettext | 23 | import gettext |
29 | 25 | 24 | ||
30 | 26 | from ubuntu_sso.logger import setup_logging | 25 | from ubuntu_sso.logger import setup_logging |
31 | 26 | from ubuntu_sso import xdg_base_directory | ||
32 | 27 | 27 | ||
33 | 28 | logger = setup_logging('ubuntu_sso.utils.ui') | 28 | logger = setup_logging('ubuntu_sso.utils.ui') |
34 | 29 | 29 | ||
35 | @@ -118,7 +118,7 @@ | |||
36 | 118 | return result | 118 | return result |
37 | 119 | 119 | ||
38 | 120 | # no local data dir, looking within system data dirs | 120 | # no local data dir, looking within system data dirs |
40 | 121 | data_dirs = xdg.BaseDirectory.xdg_data_dirs | 121 | data_dirs = xdg_base_directory.xdg_data_dirs |
41 | 122 | for path in data_dirs: | 122 | for path in data_dirs: |
42 | 123 | result = os.path.join(path, 'ubuntu-sso-client', 'data') | 123 | result = os.path.join(path, 'ubuntu-sso-client', 'data') |
43 | 124 | result = os.path.abspath(result) | 124 | result = os.path.abspath(result) |
44 | 125 | 125 | ||
45 | === added directory 'ubuntu_sso/xdg_base_directory' | |||
46 | === added file 'ubuntu_sso/xdg_base_directory/__init__.py' | |||
47 | --- ubuntu_sso/xdg_base_directory/__init__.py 1970-01-01 00:00:00 +0000 | |||
48 | +++ ubuntu_sso/xdg_base_directory/__init__.py 2011-08-23 15:43:28 +0000 | |||
49 | @@ -0,0 +1,40 @@ | |||
50 | 1 | # Author: Natalia B. Bidart <natalia.bidart@canonical.com> | ||
51 | 2 | # | ||
52 | 3 | # Copyright 2011 Canonical Ltd. | ||
53 | 4 | # | ||
54 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
55 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
56 | 7 | # by the Free Software Foundation. | ||
57 | 8 | # | ||
58 | 9 | # This program is distributed in the hope that it will be useful, but | ||
59 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
60 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
61 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
62 | 13 | # | ||
63 | 14 | # You should have received a copy of the GNU General Public License along | ||
64 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
65 | 16 | |||
66 | 17 | """XDG multiplatform.""" | ||
67 | 18 | |||
68 | 19 | import os | ||
69 | 20 | import sys | ||
70 | 21 | |||
71 | 22 | # pylint: disable=C0103 | ||
72 | 23 | if sys.platform == "win32": | ||
73 | 24 | from ubuntu_sso.xdg_base_directory import windows | ||
74 | 25 | load_config_paths = windows.load_config_paths | ||
75 | 26 | save_config_path = windows.save_config_path | ||
76 | 27 | xdg_cache_home = windows.xdg_cache_home | ||
77 | 28 | xdg_data_home = windows.xdg_data_home | ||
78 | 29 | xdg_data_dirs = windows.xdg_data_dirs | ||
79 | 30 | else: | ||
80 | 31 | import xdg.BaseDirectory | ||
81 | 32 | load_config_paths = xdg.BaseDirectory.load_config_paths | ||
82 | 33 | save_config_path = xdg.BaseDirectory.save_config_path | ||
83 | 34 | xdg_cache_home = xdg.BaseDirectory.xdg_cache_home | ||
84 | 35 | xdg_data_home = xdg.BaseDirectory.xdg_data_home | ||
85 | 36 | xdg_data_dirs = xdg.BaseDirectory.xdg_data_dirs | ||
86 | 37 | |||
87 | 38 | ubuntuone_log_dir = os.path.join(xdg_cache_home, 'ubuntuone', 'log') | ||
88 | 39 | if not os.path.exists(ubuntuone_log_dir): | ||
89 | 40 | os.makedirs(ubuntuone_log_dir) | ||
90 | 0 | 41 | ||
91 | === added directory 'ubuntu_sso/xdg_base_directory/tests' | |||
92 | === added file 'ubuntu_sso/xdg_base_directory/tests/__init__.py' | |||
93 | --- ubuntu_sso/xdg_base_directory/tests/__init__.py 1970-01-01 00:00:00 +0000 | |||
94 | +++ ubuntu_sso/xdg_base_directory/tests/__init__.py 2011-08-23 15:43:28 +0000 | |||
95 | @@ -0,0 +1,16 @@ | |||
96 | 1 | # ubuntu_sso - Ubuntu Single Sign On client support for desktop apps | ||
97 | 2 | # | ||
98 | 3 | # Copyright 2009-2010 Canonical Ltd. | ||
99 | 4 | # | ||
100 | 5 | # This program is free software: you can redistribute it and/or modify it | ||
101 | 6 | # under the terms of the GNU General Public License version 3, as published | ||
102 | 7 | # by the Free Software Foundation. | ||
103 | 8 | # | ||
104 | 9 | # This program is distributed in the hope that it will be useful, but | ||
105 | 10 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
106 | 11 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
107 | 12 | # PURPOSE. See the GNU General Public License for more details. | ||
108 | 13 | # | ||
109 | 14 | # You should have received a copy of the GNU General Public License along | ||
110 | 15 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
111 | 16 | """XDG tests.""" | ||
112 | 0 | 17 | ||
113 | === added file 'ubuntu_sso/xdg_base_directory/tests/test_common.py' | |||
114 | --- ubuntu_sso/xdg_base_directory/tests/test_common.py 1970-01-01 00:00:00 +0000 | |||
115 | +++ ubuntu_sso/xdg_base_directory/tests/test_common.py 2011-08-23 15:43:28 +0000 | |||
116 | @@ -0,0 +1,56 @@ | |||
117 | 1 | # -*- coding: utf-8 -*- | ||
118 | 2 | # | ||
119 | 3 | # Authors: Natalia B. Bidart <natalia.bidart@canonical.com> | ||
120 | 4 | # | ||
121 | 5 | # Copyright 2011 Canonical Ltd. | ||
122 | 6 | # | ||
123 | 7 | # This program is free software: you can redistribute it and/or modify it | ||
124 | 8 | # under the terms of the GNU General Public License version 3, as published | ||
125 | 9 | # by the Free Software Foundation. | ||
126 | 10 | # | ||
127 | 11 | # This program is distributed in the hope that it will be useful, but | ||
128 | 12 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
129 | 13 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
130 | 14 | # PURPOSE. See the GNU General Public License for more details. | ||
131 | 15 | # | ||
132 | 16 | # You should have received a copy of the GNU General Public License along | ||
133 | 17 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
134 | 18 | |||
135 | 19 | """Platform independent tests for the XDG constants.""" | ||
136 | 20 | |||
137 | 21 | import os | ||
138 | 22 | |||
139 | 23 | from twisted.trial.unittest import TestCase | ||
140 | 24 | |||
141 | 25 | from ubuntu_sso import xdg_base_directory | ||
142 | 26 | |||
143 | 27 | |||
144 | 28 | class TestBaseDirectory(TestCase): | ||
145 | 29 | """Tests for the BaseDirectory module.""" | ||
146 | 30 | |||
147 | 31 | def test_ubuntuone_log_dir(self): | ||
148 | 32 | """The ubuntuone_log_dir is correct.""" | ||
149 | 33 | expected = os.path.join(xdg_base_directory.xdg_cache_home, | ||
150 | 34 | 'ubuntuone', 'log') | ||
151 | 35 | self.assertEqual(expected, xdg_base_directory.ubuntuone_log_dir) | ||
152 | 36 | self.assertTrue(os.path.exists(expected)) | ||
153 | 37 | |||
154 | 38 | def test_xdg_cache_home_is_bytes(self): | ||
155 | 39 | """The returned path is bytes.""" | ||
156 | 40 | actual = xdg_base_directory.xdg_cache_home | ||
157 | 41 | self.assertIsInstance(actual, str) | ||
158 | 42 | |||
159 | 43 | def test_xdg_data_home_is_bytes(self): | ||
160 | 44 | """The returned path is bytes.""" | ||
161 | 45 | actual = xdg_base_directory.xdg_data_home | ||
162 | 46 | self.assertIsInstance(actual, str) | ||
163 | 47 | |||
164 | 48 | def test_load_config_paths_filter(self): | ||
165 | 49 | """Since those folders don't exist, this should be empty.""" | ||
166 | 50 | self.assertEqual(list(xdg_base_directory.load_config_paths("x")), []) | ||
167 | 51 | |||
168 | 52 | def test_save_config_path(self): | ||
169 | 53 | """The path should end with xdg_config/x (respecting the separator).""" | ||
170 | 54 | self.patch(os, "makedirs", lambda *args: None) | ||
171 | 55 | result = xdg_base_directory.save_config_path("x") | ||
172 | 56 | self.assertEqual(result.split(os.sep)[-2:], ['xdg_config', 'x']) | ||
173 | 0 | 57 | ||
174 | === added file 'ubuntu_sso/xdg_base_directory/tests/test_windows.py' | |||
175 | --- ubuntu_sso/xdg_base_directory/tests/test_windows.py 1970-01-01 00:00:00 +0000 | |||
176 | +++ ubuntu_sso/xdg_base_directory/tests/test_windows.py 2011-08-23 15:43:28 +0000 | |||
177 | @@ -0,0 +1,112 @@ | |||
178 | 1 | # -*- coding: utf-8 -*- | ||
179 | 2 | # | ||
180 | 3 | # Authors: Natalia B. Bidart <natalia.bidart@canonical.com> | ||
181 | 4 | # | ||
182 | 5 | # Copyright 2011 Canonical Ltd. | ||
183 | 6 | # | ||
184 | 7 | # This program is free software: you can redistribute it and/or modify it | ||
185 | 8 | # under the terms of the GNU General Public License version 3, as published | ||
186 | 9 | # by the Free Software Foundation. | ||
187 | 10 | # | ||
188 | 11 | # This program is distributed in the hope that it will be useful, but | ||
189 | 12 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
190 | 13 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
191 | 14 | # PURPOSE. See the GNU General Public License for more details. | ||
192 | 15 | # | ||
193 | 16 | # You should have received a copy of the GNU General Public License along | ||
194 | 17 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
195 | 18 | |||
196 | 19 | """Windows-specific tests for the XDG constants.""" | ||
197 | 20 | |||
198 | 21 | import os | ||
199 | 22 | import sys | ||
200 | 23 | |||
201 | 24 | from twisted.trial.unittest import TestCase | ||
202 | 25 | from ubuntuone.devtools.testcase import skipIfOS | ||
203 | 26 | |||
204 | 27 | |||
205 | 28 | # pylint: disable=E1101, E0611, F0401 | ||
206 | 29 | if sys.platform == "win32": | ||
207 | 30 | import win32com.shell | ||
208 | 31 | from ubuntu_sso.xdg_base_directory.windows import ( | ||
209 | 32 | get_config_dirs, | ||
210 | 33 | get_data_dirs, | ||
211 | 34 | get_special_folders, | ||
212 | 35 | ) | ||
213 | 36 | |||
214 | 37 | |||
215 | 38 | # pylint: disable=C0103 | ||
216 | 39 | class FakeShellConModule(object): | ||
217 | 40 | """Override CSIDL_ constants.""" | ||
218 | 41 | CSIDL_PROFILE = 0 | ||
219 | 42 | CSIDL_LOCAL_APPDATA = 1 | ||
220 | 43 | CSIDL_COMMON_APPDATA = 2 | ||
221 | 44 | |||
222 | 45 | |||
223 | 46 | class FakeShellModule(object): | ||
224 | 47 | |||
225 | 48 | """Fake Shell Module.""" | ||
226 | 49 | |||
227 | 50 | def __init__(self): | ||
228 | 51 | """Set the proper mapping between CSIDL_ consts.""" | ||
229 | 52 | self.values = { | ||
230 | 53 | 0: u'c:\\path\\to\\users\\home', | ||
231 | 54 | 1: u'c:\\path\\to\\users\\home\\appData\\local', | ||
232 | 55 | 2: u'c:\\programData', | ||
233 | 56 | } | ||
234 | 57 | |||
235 | 58 | def SHGetFolderPath(self, dummy0, shellconValue, dummy2, dummy3): | ||
236 | 59 | """Override SHGetFolderPath functionality.""" | ||
237 | 60 | return self.values[shellconValue] | ||
238 | 61 | |||
239 | 62 | |||
240 | 63 | @skipIfOS('linux2', 'Windows-specific tests.') | ||
241 | 64 | class TestBaseDirectoryWindows(TestCase): | ||
242 | 65 | """Tests for the BaseDirectory module.""" | ||
243 | 66 | |||
244 | 67 | def test_get_special_folders(self): | ||
245 | 68 | """Make sure we can import the platform module.""" | ||
246 | 69 | |||
247 | 70 | shellModule = FakeShellModule() | ||
248 | 71 | self.patch(win32com.shell, "shell", shellModule) | ||
249 | 72 | self.patch(win32com.shell, "shellcon", FakeShellConModule()) | ||
250 | 73 | special_folders = get_special_folders() | ||
251 | 74 | self.assertTrue('Personal' in special_folders) | ||
252 | 75 | self.assertTrue('Local AppData' in special_folders) | ||
253 | 76 | self.assertTrue('AppData' in special_folders) | ||
254 | 77 | self.assertTrue('Common AppData' in special_folders) | ||
255 | 78 | |||
256 | 79 | self.assertTrue(special_folders['Personal'] == \ | ||
257 | 80 | shellModule.values[FakeShellConModule.CSIDL_PROFILE]) | ||
258 | 81 | self.assertTrue(special_folders['Local AppData'] == \ | ||
259 | 82 | shellModule.values[FakeShellConModule.CSIDL_LOCAL_APPDATA]) | ||
260 | 83 | self.assertTrue(special_folders['Local AppData'].startswith( | ||
261 | 84 | special_folders['AppData'])) | ||
262 | 85 | self.assertTrue(special_folders['Common AppData'] == \ | ||
263 | 86 | shellModule.values[FakeShellConModule.CSIDL_COMMON_APPDATA]) | ||
264 | 87 | |||
265 | 88 | # Can't use assert_syncdaemon_path | ||
266 | 89 | for val in special_folders.itervalues(): | ||
267 | 90 | self.assertIsInstance(val, str) | ||
268 | 91 | val.decode('utf8') | ||
269 | 92 | # Should not raise exceptions | ||
270 | 93 | |||
271 | 94 | def test_get_data_dirs(self): | ||
272 | 95 | """Check thet get_data_dirs uses pathsep correctly.""" | ||
273 | 96 | bad_sep = filter(lambda x: x not in os.pathsep, ":;") | ||
274 | 97 | dir_list = ["A", "B", bad_sep, "C"] | ||
275 | 98 | self.patch(os, "environ", | ||
276 | 99 | dict(XDG_DATA_DIRS=os.pathsep.join( | ||
277 | 100 | dir_list))) | ||
278 | 101 | dirs = get_data_dirs() | ||
279 | 102 | self.assertEqual(dirs, dir_list) | ||
280 | 103 | |||
281 | 104 | def test_get_config_dirs(self): | ||
282 | 105 | """Check thet get_data_dirs uses pathsep correctly.""" | ||
283 | 106 | bad_sep = filter(lambda x: x not in os.pathsep, ":;") | ||
284 | 107 | dir_list = ["A", "B", bad_sep, "C"] | ||
285 | 108 | self.patch(os, "environ", | ||
286 | 109 | dict(XDG_CONFIG_DIRS=os.pathsep.join( | ||
287 | 110 | dir_list))) | ||
288 | 111 | dirs = get_config_dirs()[1:] | ||
289 | 112 | self.assertEqual(dirs, dir_list) | ||
290 | 0 | 113 | ||
291 | === added file 'ubuntu_sso/xdg_base_directory/windows.py' | |||
292 | --- ubuntu_sso/xdg_base_directory/windows.py 1970-01-01 00:00:00 +0000 | |||
293 | +++ ubuntu_sso/xdg_base_directory/windows.py 2011-08-23 15:43:28 +0000 | |||
294 | @@ -0,0 +1,119 @@ | |||
295 | 1 | # Authors: Manuel de la Pena <manuel@canonical.com> | ||
296 | 2 | # Diego Sarmentero <diego.sarmentero@canonical.com> | ||
297 | 3 | # | ||
298 | 4 | # Copyright 2011 Canonical Ltd. | ||
299 | 5 | # | ||
300 | 6 | # This program is free software: you can redistribute it and/or modify it | ||
301 | 7 | # under the terms of the GNU General Public License version 3, as published | ||
302 | 8 | # by the Free Software Foundation. | ||
303 | 9 | # | ||
304 | 10 | # This program is distributed in the hope that it will be useful, but | ||
305 | 11 | # WITHOUT ANY WARRANTY; without even the implied warranties of | ||
306 | 12 | # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
307 | 13 | # PURPOSE. See the GNU General Public License for more details. | ||
308 | 14 | # | ||
309 | 15 | # You should have received a copy of the GNU General Public License along | ||
310 | 16 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
311 | 17 | |||
312 | 18 | """XDG helpers for windows.""" | ||
313 | 19 | |||
314 | 20 | import os | ||
315 | 21 | |||
316 | 22 | |||
317 | 23 | # pylint: disable=C0103 | ||
318 | 24 | def get_special_folders(): | ||
319 | 25 | """ Routine to grab all the Windows Special Folders locations. | ||
320 | 26 | |||
321 | 27 | If successful, returns dictionary | ||
322 | 28 | of shell folder locations indexed on Windows keyword for each; | ||
323 | 29 | otherwise, returns an empty dictionary. | ||
324 | 30 | """ | ||
325 | 31 | # pylint: disable=W0621, F0401, E0611 | ||
326 | 32 | special_folders = {} | ||
327 | 33 | |||
328 | 34 | from win32com.shell import shell, shellcon | ||
329 | 35 | # CSIDL_LOCAL_APPDATA = C:\Users\<username>\AppData\Local | ||
330 | 36 | # CSIDL_PROFILE = C:\Users\<username> | ||
331 | 37 | # CSIDL_COMMON_APPDATA = C:\ProgramData | ||
332 | 38 | # More information on these at | ||
333 | 39 | # http://msdn.microsoft.com/en-us/library/bb762494(v=vs.85).aspx | ||
334 | 40 | get_path = lambda name: shell.SHGetFolderPath( | ||
335 | 41 | 0, getattr(shellcon, name), None, 0).encode('utf8') | ||
336 | 42 | special_folders['Personal'] = get_path("CSIDL_PROFILE") | ||
337 | 43 | special_folders['Local AppData'] = get_path("CSIDL_LOCAL_APPDATA") | ||
338 | 44 | special_folders['AppData'] = os.path.dirname( | ||
339 | 45 | special_folders['Local AppData']) | ||
340 | 46 | special_folders['Common AppData'] = get_path("CSIDL_COMMON_APPDATA") | ||
341 | 47 | return special_folders | ||
342 | 48 | |||
343 | 49 | special_folders = get_special_folders() | ||
344 | 50 | |||
345 | 51 | home_path = special_folders['Personal'] | ||
346 | 52 | app_local_data_path = special_folders['Local AppData'] | ||
347 | 53 | app_global_data_path = special_folders['Common AppData'] | ||
348 | 54 | |||
349 | 55 | # use the non roaming app data | ||
350 | 56 | xdg_data_home = os.environ.get('XDG_DATA_HOME', | ||
351 | 57 | os.path.join(app_local_data_path, 'xdg')) | ||
352 | 58 | |||
353 | 59 | |||
354 | 60 | def get_data_dirs(): | ||
355 | 61 | """Returns XDG data directories.""" | ||
356 | 62 | return os.environ.get('XDG_DATA_DIRS', | ||
357 | 63 | '{0}{1}{2}'.format(app_local_data_path, os.pathsep, | ||
358 | 64 | app_global_data_path)).split(os.pathsep) | ||
359 | 65 | |||
360 | 66 | xdg_data_dirs = get_data_dirs() | ||
361 | 67 | |||
362 | 68 | # we will return the roaming data wich is as close as we get in windows | ||
363 | 69 | # regarding caching. | ||
364 | 70 | xdg_cache_home = os.environ.get('XDG_CACHE_HOME', | ||
365 | 71 | os.path.join(xdg_data_home, 'cache')) | ||
366 | 72 | |||
367 | 73 | # point to the not roaming app data for the user | ||
368 | 74 | xdg_config_home = os.environ.get('XDG_CONFIG_HOME', | ||
369 | 75 | app_local_data_path) | ||
370 | 76 | |||
371 | 77 | |||
372 | 78 | def get_config_dirs(): | ||
373 | 79 | """Return XDG config directories.""" | ||
374 | 80 | return [xdg_config_home] + \ | ||
375 | 81 | os.environ.get('XDG_CONFIG_DIRS', | ||
376 | 82 | app_global_data_path, | ||
377 | 83 | ).split(os.pathsep) | ||
378 | 84 | |||
379 | 85 | xdg_config_dirs = get_config_dirs() | ||
380 | 86 | |||
381 | 87 | xdg_data_dirs = filter(lambda x: x, xdg_data_dirs) | ||
382 | 88 | xdg_config_dirs = filter(lambda x: x, xdg_config_dirs) | ||
383 | 89 | |||
384 | 90 | |||
385 | 91 | def load_config_paths(*resource): | ||
386 | 92 | """Iterator of configuration paths. | ||
387 | 93 | |||
388 | 94 | Return an iterator which gives each directory named 'resource' in | ||
389 | 95 | the configuration search path. Information provided by earlier | ||
390 | 96 | directories should take precedence over later ones (ie, the user's | ||
391 | 97 | config dir comes first). | ||
392 | 98 | """ | ||
393 | 99 | resource = os.path.join(*resource) | ||
394 | 100 | for config_dir in xdg_config_dirs: | ||
395 | 101 | path = os.path.join(config_dir, resource) | ||
396 | 102 | if os.path.exists(path): | ||
397 | 103 | yield path | ||
398 | 104 | |||
399 | 105 | |||
400 | 106 | def save_config_path(*resource): | ||
401 | 107 | """Path to save configuration. | ||
402 | 108 | |||
403 | 109 | Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path. | ||
404 | 110 | 'resource' should normally be the name of your application. Use this | ||
405 | 111 | when SAVING configuration settings. Use the xdg_config_dirs variable | ||
406 | 112 | for loading. | ||
407 | 113 | """ | ||
408 | 114 | resource = os.path.join(*resource) | ||
409 | 115 | assert not resource.startswith('/') | ||
410 | 116 | path = os.path.join(xdg_config_home, resource) | ||
411 | 117 | if not os.path.isdir(path): | ||
412 | 118 | os.makedirs(path, 0700) | ||
413 | 119 | return path |
So, since we're adding a new multiplatform module, we need to come up with something like this:
ubuntu_sso/ base_directory/
__ init__. py
test_ common. py <- multiplatform tests
test_ windows. py
main/
foo/
xdg_
__init__.py
windows.py
tests/