1 import os.path
2 import logging
3 import gobject
4 log = logging.getLogger("Vfs")
5
6 try:
7 import gnomevfs
8 except ImportError:
9 from gnome import gnomevfs
10
11 import conduit.utils.Singleton as Singleton
12
13
14
15
17 """
18 (weakly) checks if a uri is valid by looking for a scheme seperator
19 """
20 assert type(uri) == str
21 return uri[0] != "/" and uri.find("://") != -1
22
24 """
25 Joins multiple uri components. Performs safely if the first
26 argument contains a uri scheme
27 """
28 assert type(first) == str
29 return os.path.join(first,*rest)
30
31
32
33
34
35
36
38 """
39 Returns the relative path fromURI --> toURI
40 """
41 assert type(fromURI) == str
42 assert type(toURI) == str
43 rel = toURI.replace(fromURI,"")
44
45 if rel[0] == os.sep:
46 return rel[1:]
47 else:
48 return rel
49
51 """
52 Opens a gnomevfs or xdg compatible uri.
53 """
54 assert type(uri) == str
55 APP = "xdg-open"
56 os.spawnlp(os.P_NOWAIT, APP, APP, uri)
57
59 """
60 @returns: The local path (/foo/bar) for the given URI. Throws a
61 RuntimeError (wtf??) if the uri is not a local one
62 """
63 assert type(uri) == str
64 return gnomevfs.get_local_path_from_uri(uri)
65
76
78 """
79 @returns: True if the specified uri is on a removable volume, like a USB key
80 or removable/mountable disk.
81 """
82 assert type(uri) == str
83 scheme = gnomevfs.get_uri_scheme(uri)
84 if scheme == "file":
85
86
87 try:
88 path = uri_to_local_path(uri)
89 return VolumeMonitor().volume_is_removable(path)
90 except Exception, e:
91 log.warn("Could not determine if uri on removable volume: %s (%s)" % (uri, e))
92 return False
93 return False
94
95
97 """
98 @returns: The filesystem that uri is stored on or None if it cannot
99 be determined
100 """
101 assert type(uri) == str
102 scheme = gnomevfs.get_uri_scheme(uri)
103 if scheme == "file":
104 try:
105 path = uri_to_local_path(uri)
106 return VolumeMonitor().volume_get_fstype(path)
107 except RuntimeError:
108 log.warn("Could not get local path from URI")
109 return None
110 except AttributeError:
111 log.warn("Could not determine volume for path")
112 return None
113 return None
114
116 """
117 Standardizes the format of the uri
118 @param uri:an absolute or relative stringified uri. It might have scheme.
119 """
120 assert type(uri) == str
121 return gnomevfs.make_uri_canonical(uri)
122
124 """
125 Escapes a uri, replacing only special characters that would not be found in
126 paths or host names.
127 (so '/', '&', '=', ':' and '@' will not be escaped by this function)
128 """
129 assert type(uri) == str
130
131
132 import urllib
133 return urllib.quote(uri,safe='/&=:@')
134
136 """
137 Replace "%xx" escapes by their single-character equivalent.
138 """
139 assert type(uri) == str
140 import urllib
141 return urllib.unquote(uri)
142
144 """
145 Returns the protocol (file, smb, etc) for a URI
146 """
147 assert type(uri) == str
148 if uri.rfind("://")==-1:
149 return ""
150 protocol = uri[:uri.index("://")+3]
151 return protocol.lower()
152
154 """
155 Method to return the filename of a file. Could use GnomeVFS for this
156 is it wasnt so slow
157 """
158 assert type(uri) == str
159 return uri.split(os.sep)[-1]
160
162 """
163 Returns filename,file_extension
164 """
165 assert type(uri) == str
166 return os.path.splitext(uri_get_filename(uri))
167
169 """
170 Removes illegal characters in uri that cannot be stored on the
171 given filesystem - particuarly fat and ntfs types
172 """
173 assert type(uri) == str
174 import string
175
176 ILLEGAL_CHARS = {
177 "vfat" : "\\:*?\"<>|",
178 "ntfs" : "\\:*?\"<>|"
179 }
180
181 illegal = ILLEGAL_CHARS.get(filesystem,None)
182 if illegal:
183
184 idx = uri.rfind("://")
185 if idx == -1:
186 start = 0
187 else:
188 start = idx + 3
189
190
191 return uri[0:start]+uri[start:].translate(string.maketrans(
192 illegal,
193 " "*len(illegal)
194 )
195 )
196 return uri
197
199 """
200 @returns: True if the uri is a folder and not a file
201 """
202 assert type(uri) == str
203 info = gnomevfs.get_file_info(uri)
204 return info.type == gnomevfs.FILE_TYPE_DIRECTORY
205
212
214 """
215 @returns: True if the uri exists
216 """
217 assert type(uri) == str
218 try:
219 return gnomevfs.exists(gnomevfs.URI(uri)) == 1
220 except Exception, err:
221 log.warn("Error checking if location exists")
222 return False
223
225 """
226 Makes a directory with the default permissions. Does not catch any
227 error
228 """
229 assert type(uri) == str
230 gnomevfs.make_directory(
231 uri,
232 gnomevfs.PERM_USER_ALL | gnomevfs.PERM_GROUP_READ | gnomevfs.PERM_GROUP_EXEC | gnomevfs.PERM_OTHER_READ | gnomevfs.PERM_OTHER_EXEC
233 )
234
236 """
237 Because gnomevfs.make_dir does not perform as mkdir -p this function
238 is required to make a heirarchy of directories.
239
240 @param uri: A directory that does not exist
241 @type uri: str
242 """
243 assert type(uri) == str
244 exists = False
245 dirs = []
246
247 directory = gnomevfs.URI(uri)
248 while not exists:
249 dirs.append(directory)
250 directory = directory.parent
251 exists = gnomevfs.exists(directory)
252
253 dirs.reverse()
254 for d in dirs:
255 log.debug("Making directory %s" % d)
256 uri_make_directory(str(d))
257