11 |
|
|
12 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
13 |
|
|
14 |
|
import os |
15 |
import os.path |
import os.path |
16 |
from string import split, join |
from tempfile import mktemp |
17 |
|
|
18 |
|
from string import join |
19 |
|
|
20 |
|
from Thuban import _ |
21 |
|
|
22 |
def relative_filename_common(dir, absname, sep): |
def relative_filename_common(dir, absname, sep): |
23 |
"""Return a filename relative to dir for the absolute file name absname. |
"""Return a filename relative to dir for the absolute file name absname. |
29 |
|
|
30 |
# split the filenames into their components. remove the first item |
# split the filenames into their components. remove the first item |
31 |
# since it will be always empty because both names are absolute. |
# since it will be always empty because both names are absolute. |
32 |
dir_parts = split(dir, sep)[1:] |
dir_parts = dir.split(sep)[1:] |
33 |
absname_parts = split(absname, sep)[1:] |
absname_parts = absname.split(sep)[1:] |
34 |
|
|
35 |
# count the number of common parts at the start of dir_parts and |
# count the number of common parts at the start of dir_parts and |
36 |
# absname_parts |
# absname_parts |
84 |
return absname |
return absname |
85 |
|
|
86 |
if not posixpath.isabs(dir): |
if not posixpath.isabs(dir): |
87 |
raise TypeError("first argument must be an absolute filename") |
raise TypeError(_("first argument must be an absolute filename")) |
88 |
|
|
89 |
dir = posixpath.normpath(dir) |
dir = posixpath.normpath(dir) |
90 |
absname = posixpath.normpath(absname) |
absname = posixpath.normpath(absname) |
135 |
#print dir_drive, dir_rest |
#print dir_drive, dir_rest |
136 |
#print absname_drive, absname_rest |
#print absname_drive, absname_rest |
137 |
if not dir_drive or not absname_drive: |
if not dir_drive or not absname_drive: |
138 |
raise TypeError("Both parameters must have a drive letter") |
raise TypeError(_("Both parameters must have a drive letter")) |
139 |
|
|
140 |
if not ntpath.isabs(dir_rest): |
if not ntpath.isabs(dir_rest): |
141 |
raise TypeError("first argument must be an absolute filename") |
raise TypeError(_("first argument must be an absolute filename")) |
142 |
|
|
143 |
# handle some special cases |
# handle some special cases |
144 |
if not ntpath.isabs(absname_rest): |
if not ntpath.isabs(absname_rest): |
152 |
# relative name |
# relative name |
153 |
return relative_filename_common(dir_rest, absname_rest, "\\") |
return relative_filename_common(dir_rest, absname_rest, "\\") |
154 |
|
|
155 |
|
def get_application_dir(): |
156 |
|
"""Determine the path to the .thuban directory. Create the directory |
157 |
|
if it doesn't exist. |
158 |
|
|
159 |
|
Under posix systems use the os.expanduser() method. |
160 |
|
Under Win32 try to read the "Explorer/Shell Folders/" value "AppData". |
161 |
|
""" |
162 |
|
|
163 |
|
if os.name == 'posix': |
164 |
|
dir = os.path.expanduser("~/.thuban") |
165 |
|
if not os.path.isdir(dir): |
166 |
|
os.mkdir(dir) |
167 |
|
return dir |
168 |
|
elif os.name == 'nt': |
169 |
|
regkey = 1 |
170 |
|
try: |
171 |
|
import _winreg as wreg |
172 |
|
except ImportError: |
173 |
|
regkey = 0 |
174 |
|
|
175 |
|
if regkey: |
176 |
|
try: |
177 |
|
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
178 |
|
"Software\\Microsoft\\Windows\\CurrentVersion\\"\ |
179 |
|
"Explorer\\Shell Folders") |
180 |
|
dir = wreg.QueryValueEx(key, "AppData")[0] |
181 |
|
dir = os.path.join(dir, "thuban") |
182 |
|
except: |
183 |
|
regkey = 0 |
184 |
|
|
185 |
|
if not regkey: |
186 |
|
# The fallback. This should result in something like the |
187 |
|
# user directory ... |
188 |
|
guess = os.path.dirname( |
189 |
|
os.path.dirname(os.path.dirname(mktemp())) |
190 |
|
) |
191 |
|
dir = os.path.join(guess, "thuban") |
192 |
|
if not os.path.isdir(dir): |
193 |
|
os.mkdir(dir) |
194 |
|
|
195 |
|
return dir |
196 |
|
|
197 |
|
else: |
198 |
|
raise RuntimeError(_("No implementation of get_application_dir" |
199 |
|
" available for platform") + os.name) |
200 |
|
|
201 |
# bind the appropriate version of relative_filename for the platform |
# bind the appropriate version of relative_filename for the platform |
202 |
# we're currently running on. |
# we're currently running on. |
205 |
elif os.name == "nt": |
elif os.name == "nt": |
206 |
relative_filename = relative_filename_nt |
relative_filename = relative_filename_nt |
207 |
else: |
else: |
208 |
raise RuntimeError("No implementation of relative_filename" |
raise RuntimeError(_("No implementation of relative_filename" |
209 |
" available for platform" + os.name) |
" available for platform") + os.name) |
210 |
|
|
211 |
__test__ = {"relative_filename_posix": relative_filename_posix, |
__test__ = {"relative_filename_posix": relative_filename_posix, |
212 |
"relative_filename_nt": relative_filename_nt} |
"relative_filename_nt": relative_filename_nt} |