-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathunifiedIO.py
More file actions
301 lines (224 loc) · 7.57 KB
/
Copy pathunifiedIO.py
File metadata and controls
301 lines (224 loc) · 7.57 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from PYME.IO.FileUtils import nameUtils
import os
import sys
from io import BytesIO
from contextlib import contextmanager
import tempfile
import re
try: # py3
from urllib.parse import quote, urlencode
except ImportError: # py2
from urllib import quote, urlencode
import logging
logger = logging.getLogger(__name__)
alpha_regex = re.compile(r'^[\w/\.\-]+$')
win_regex = re.compile(r'^[\w/(\\)\.\-]+$') #permit backslashes for windows paths
def check_name(name, win=False):
"""
Check if filename / url is OK to use with, e.g. clusterIO.
Parameters
----------
name: str
The filename or url to query
win: bool
is name a windows path
Returns
-------
ok: bool
True if filename/url is fully compatible
"""
if win:
return bool(win_regex.match(name))
else:
return bool(alpha_regex.match(name))
#return name == fix_name(name)
def check_uri(name):
return check_name(name.split('://')[1])
def assert_name_ok(name):
"""
Raise if name contains reserved/invalid characters for use with, e.g. clusterIO.
Parameters
----------
name: str or bytes
The filename or url to query
"""
try:
assert check_name(name) == True
except AssertionError:
raise AssertionError('Name "%s" is invalid. Names must only include alphanumeric characters and underscore' % name)
def check_path(name):
if sys.platform.startswith('win32'):
return check_name(os.path.splitdrive(name)[-1],win=True)
else:
return check_name(name)
def assert_path_ok(name):
assert check_path(name) == True
def assert_uri_ok(name):
"""
Raise if name contains reserved/invalid characters for use with, e.g. clusterIO.
Parameters
----------
name: str or bytes
The filename or url to query
"""
try:
assert check_uri(name) == True
except AssertionError:
raise AssertionError('Name "%s" is invalid. Names must only include alphanumeric characters and underscore' % name)
def assert_uri_path_ok(name):
"""
combination of assert_path_ok and assert_uri_ok - runs assert_uri_ok if we have a URI, assert_name_ok if we have a name
"""
if is_cluster_uri(name):
assert_uri_ok(name)
else:
assert_path_ok(name)
def fix_name(name):
"""
Cleans filename / url for use with, e.g. clusterIO, by replacing spaces with underscores and removing all
percent-encoded characters other than ':' and '/'.
Parameters
----------
name: str
The filename or url to query
Returns
-------
fixed_name: str
The cleaned file name
"""
return re.sub('%..', '', quote(name.replace(' ', '_')).replace('%3A', ':'))
def verbose_fix_name(name):
"""
Wrapper for fix_name which sacrifices performance in order to complain.
Parameters
----------
name: str
The filename or url to query
Returns
-------
fixed_name: str
The cleaned file name
"""
try:
assert_name_ok(name)
except AssertionError as e:
logger.error(str(e))
return fix_name(name)
def is_cluster_uri(url):
"""
Checks whether the supplied uri/filename is a pyme-cluster uri
Parameters
----------
url
Returns
-------
bool
"""
return (url.startswith('pyme-cluster') or url.startswith('PYME-CLUSTER'))
def split_cluster_url(url):
if not is_cluster_uri(url):
raise RuntimeError('Not a cluster URL')
clusterfilter = url.split('://')[1].split('/')[0]
sequenceName = url.split('://%s/' % clusterfilter)[1]
return sequenceName, clusterfilter
def dirname(url):
try:
return os.path.dirname(split_cluster_url(url)[0])
except RuntimeError:
return os.path.dirname(nameUtils.getFullExistingFilename(url))
@contextmanager
def local_or_temp_filename(url):
"""
Gets a local filename for a given url.
The purpose is to let us load files from the cluster using IO libraries (e.g. pytables, tifffile) which need a
filename, not a file handle.
Does the following (in order).
* checks to see if the url refers to a local file, if so return the filename
* if the url points to a cluster file, check to see if it happens to be stored locally. If so, return the local path
* download file to a temporary file and return the filename of that temporary file.
NB: This should be used as a context manager (ie in a with statement) so that the temporary file gets cleaned up properly
Parameters
----------
url : basestring
Returns
-------
filename : basestring
Notes
-----
TODO - there is potential for optimization by using memmaps rather than files on disk.
"""
filename = nameUtils.getFullExistingFilename(url)
if os.path.exists(filename):
yield filename
elif is_cluster_uri(url):
from .import clusterIO
sequenceName, clusterfilter = split_cluster_url(filename)
localpath = clusterIO.get_local_path(sequenceName, clusterfilter)
if localpath:
yield localpath
else:
ext = os.path.splitext(sequenceName)[-1]
with tempfile.NamedTemporaryFile(mode='w+b', suffix=ext) as outf:
s = clusterIO.get_file(sequenceName, clusterfilter)
outf.write(s)
outf.flush()
yield outf.name
else:
raise IOError('Path "%s" could not be found' % url)
def openFile(filename, mode='rb'):
filename = nameUtils.getFullExistingFilename(filename)
if os.path.exists(filename):
return open(filename, mode)
elif is_cluster_uri(filename):
#TODO - add short-circuiting for local files
from . import clusterIO
sequenceName, clusterfilter = split_cluster_url(filename)
s = clusterIO.get_file(sequenceName, clusterfilter)
return BytesIO(s)
else:
raise IOError('File does not exist or URI not understood: %s' % filename)
def read(filename):
'''
Read a file from disk or the cluster.
NOTE: filename is expected to be sanitized / trusted, this should not be called
with user data from a web endpoint.
Parameters
----------
filename
Returns
-------
'''
filename = nameUtils.getFullExistingFilename(filename)
if os.path.exists(filename):
with open(filename, 'rb') as f:
s = f.read()
return s
elif is_cluster_uri(filename):
from . import clusterIO
sequenceName, clusterfilter = split_cluster_url(filename)
s = clusterIO.get_file(sequenceName, clusterfilter)
return s
else:
raise IOError('File does not exist or URI not understood: %s' % filename)
def safe_read(filename):
'''
Read in a web-endpoint safe manner (i.e. disallow local file access)
Currently just a wrapper around clusterIO.get_file, but also as a
forwards-compatible stub if we expand unifiedIO to support more general HTTP
or, e.g. OMERO URIs.
'''
if is_cluster_uri(filename):
from . import clusterIO
sequenceName, clusterfilter = split_cluster_url(filename)
s = clusterIO.get_file(sequenceName, clusterfilter)
return s
else:
raise IOError('URI not understood: %s' % filename)
def write(filename, data):
if is_cluster_uri(filename):
from . import clusterIO
sequenceName, clusterfilter = split_cluster_url(filename)
clusterIO.put_file(sequenceName, data, serverfilter=clusterfilter)
else:
with open(filename, 'wb') as f:
f.write(data)