/usr/lib/python2.7/dist-packages/neo/test/iotest/tools.py is in python-neo 0.3.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | # -*- coding: utf-8 -*-
'''
Common tools that are useful for neo.io object tests
'''
# needed for python 3 compatibility
from __future__ import absolute_import
import logging
import os
import shutil
import tempfile
try:
from urllib import urlretrieve # Py2
except ImportError:
from urllib.request import urlretrieve # Py3
from neo.core import Block, Segment
from neo.test.iotest.generate_datasets import generate_from_supported_objects
def can_use_network():
'''
Return True if network access is allowed
'''
if os.environ.get('NOSETESTS_NO_NETWORK', False):
return False
if os.environ.get('TRAVIS') == 'true':
return False
return True
def make_all_directories(filename, localdir):
'''
Make the directories needed to store test files
'''
# handle case of multiple filenames
if not hasattr(filename, 'lower'):
for ifilename in filename:
make_all_directories(ifilename, localdir)
return
fullpath = os.path.join(localdir, os.path.dirname(filename))
if os.path.dirname(filename) != '' and not os.path.exists(fullpath):
if not os.path.exists(os.path.dirname(fullpath)):
make_all_directories(os.path.dirname(filename), localdir)
os.mkdir(fullpath)
def download_test_file(filename, localdir, url):
'''
Download a test file from a server if it isn't already available.
filename is the name of the file.
localdir is the local directory to store the file in.
url is the remote url that the file should be downloaded from.
'''
# handle case of multiple filenames
if not hasattr(filename, 'lower'):
for ifilename in filename:
download_test_file(ifilename, localdir, url)
return
localfile = os.path.join(localdir, filename)
distantfile = url + '/' + filename
if not os.path.exists(localfile):
logging.info('Downloading %s here %s', distantfile, localfile)
urlretrieve(distantfile, localfile)
def create_local_temp_dir(name, directory=None):
'''
Create a directory for storing temporary files needed for testing neo
If directory is None or not specified, automatically create the directory
in {tempdir}/files_for_testing_neo on linux/unix/mac or
{tempdir}\files_for_testing_neo on windows, where {tempdir} is the system
temporary directory returned by tempfile.gettempdir().
'''
if directory is None:
directory = os.path.join(tempfile.gettempdir(),
'files_for_testing_neo')
if not os.path.exists(directory):
os.mkdir(directory)
directory = os.path.join(directory, name)
if not os.path.exists(directory):
os.mkdir(directory)
return directory
def close_object_safe(obj):
'''
Close an object safely, ignoring errors
For some io types, like HDF5IO, the file should be closed before being
opened again in a test. Call this after the test is done to make sure
the file is closed.
'''
try:
obj.close()
except:
pass
def cleanup_test_file(mode, path, directory=None):
'''
Remove test files or directories safely. mode is the mode of the io class,
either 'file' or 'directory'. It can also be an io class object, or any
other object with a 'mode' attribute. If that is the case, use the
'mode' attribute from the object.
If directory is not None and path is not an absolute path already,
use the file from the given directory.
'''
if directory is not None and not os.path.isabs(path):
path = os.path.join(directory, path)
if hasattr(mode, 'mode'):
mode = mode.mode
if mode == 'file':
if os.path.exists(path):
os.remove(path)
elif mode == 'dir':
if os.path.exists(path):
shutil.rmtree(path)
def create_generic_io_object(ioclass, filename=None, directory=None,
return_path=False, clean=False):
'''
Create an io object in a generic way that can work with both
file-based and directory-based io objects
If filename is None, create a filename.
If return_path is True, also return the full path to the file.
If directory is not None and path is not an absolute path already,
use the file from the given directory.
If return_path is True, return the full path of the file along with
the io object. return reader, path. Default is False.
If clean is True, try to delete existing versions of the file
before creating the io object. Default is False.
'''
# create a filename if none is provided
if filename is None:
filename = 'Generated0_%s' % ioclass.__name__
if (ioclass.mode == 'file' and len(ioclass.extensions) >= 1):
filename += '.' + ioclass.extensions[0]
# if a directory is provided add it
if directory is not None and not os.path.isabs(filename):
filename = os.path.join(directory, filename)
if clean:
cleanup_test_file(ioclass, filename)
try:
# actually create the object
if ioclass.mode == 'file':
ioobj = ioclass(filename=filename)
elif ioclass.mode == 'dir':
ioobj = ioclass(dirname=filename)
else:
ioobj = None
except:
print(filename)
raise
# return the full path if requested, otherwise don't
if return_path:
return ioobj, filename
return ioobj
def iter_generic_io_objects(ioclass, filenames, directory=None,
return_path=False, clean=False):
'''
Return an iterable over the io objects created from a list of filenames.
The objects are automatically cleaned up afterwards.
If directory is not None and path is not an absolute path already,
use the file from the given directory.
If return_path is True, yield the full path of the file along with
the io object. yield reader, path. Default is False.
If clean is True, try to delete existing versions of the file
before creating the io object. Default is False.
'''
for filename in filenames:
ioobj, path = create_generic_io_object(ioclass, filename=filename,
directory=directory,
return_path=True,
clean=clean)
if ioobj is None:
continue
if return_path:
yield ioobj, path
else:
yield ioobj
close_object_safe(ioobj)
def create_generic_reader(ioobj, target=None, readall=False):
'''
Create a function that can read the target object from a file.
If target is None, use the first supported_objects from ioobj
If target is False, use the 'read' method.
If target is the Block or Segment class, use read_block or read_segment,
respectively.
If target is a string, use 'read_'+target.
If readall is True, use the read_all_ method instead of the read_ method.
Default is False.
'''
if target is None:
target = ioobj.supported_objects[0].__name__
if target == Block:
if readall:
return ioobj.read_all_blocks
return ioobj.read_block
elif target == Segment:
if readall:
return ioobj.read_all_segments
return ioobj.read_segment
elif not target:
if readall:
raise ValueError('readall cannot be True if target is False')
return ioobj.read
elif hasattr(target, 'lower'):
if readall:
return getattr(ioobj, 'read_all_%ss' % target.lower())
return getattr(ioobj, 'read_%s' % target.lower())
def iter_generic_readers(ioclass, filenames, directory=None, target=None,
return_path=False, return_ioobj=False,
clean=False, readall=False):
'''
Iterate over functions that can read the target object from a list of
filenames.
If target is None, use the first supported_objects from ioobj
If target is False, use the 'read' method.
If target is the Block or Segment class, use read_block or read_segment,
respectively.
If target is a string, use 'read_'+target.
If directory is not None and path is not an absolute path already,
use the file from the given directory.
If return_path is True, return the full path of the file along with
the reader object. return reader, path.
If return_ioobj is True, return the io object as well as the reader.
return reader, ioobj. Default is False.
If both return_path and return_ioobj is True,
return reader, path, ioobj. Default is False.
If clean is True, try to delete existing versions of the file
before creating the io object. Default is False.
If readall is True, use the read_all_ method instead of the read_ method.
Default is False.
'''
for ioobj, path in iter_generic_io_objects(ioclass=ioclass,
filenames=filenames,
directory=directory,
return_path=True,
clean=clean):
res = create_generic_reader(ioobj, target=target, readall=readall)
if not return_path and not return_ioobj:
yield res
else:
res = (res, )
if return_path:
res = res + (path,)
if return_ioobj:
res = res + (ioobj,)
yield res
def create_generic_writer(ioobj, target=None):
'''
Create a function that can write the target object to a file using the
neo io object ioobj.
If target is None, use the first supported_objects from ioobj
If target is False, use the 'write' method.
If target is the Block or Segment class, use write_block or write_segment,
respectively.
If target is a string, use 'write_'+target.
'''
if target is None:
target = ioobj.supported_objects[0].__name__
if target == Block:
return ioobj.write_block
elif target == Segment:
return ioobj.write_segment
elif not target:
return ioobj.write
elif hasattr(target, 'lower'):
return getattr(ioobj, 'write_' + target.lower())
def read_generic(ioobj, target=None, cascade=True, lazy=False, readall=False,
return_reader=False):
'''
Read the target object from a file using the given neo io object ioobj.
If target is None, use the first supported_objects from ioobj
If target is False, use the 'write' method.
If target is the Block or Segment class, use write_block or write_segment,
respectively.
If target is a string, use 'write_'+target.
The cascade and lazy parameters are passed to the reader. Defaults
are True and False, respectively.
If readall is True, use the read_all_ method instead of the read_ method.
Default is False.
If return_reader is True, yield the io reader function as well as the
object. yield obj, reader. Default is False.
'''
obj_reader = create_generic_reader(ioobj, target=target, readall=readall)
obj = obj_reader(cascade=cascade, lazy=lazy)
if return_reader:
return obj, obj_reader
return obj
def iter_read_objects(ioclass, filenames, directory=None, target=None,
return_path=False, return_ioobj=False,
return_reader=False, clean=False, readall=False,
cascade=True, lazy=False):
'''
Iterate over objects read from a list of filenames.
If target is None, use the first supported_objects from ioobj
If target is False, use the 'read' method.
If target is the Block or Segment class, use read_block or read_segment,
respectively.
If target is a string, use 'read_'+target.
If directory is not None and path is not an absolute path already,
use the file from the given directory.
If return_path is True, yield the full path of the file along with
the object. yield obj, path.
If return_ioobj is True, yield the io object as well as the object.
yield obj, ioobj. Default is False.
If return_reader is True, yield the io reader function as well as the
object. yield obj, reader. Default is False.
If some combination of return_path, return_ioobj, and return_reader
is True, they are yielded in the order: obj, path, ioobj, reader.
If clean is True, try to delete existing versions of the file
before creating the io object. Default is False.
The cascade and lazy parameters are passed to the reader. Defaults
are True and False, respectively.
If readall is True, use the read_all_ method instead of the read_ method.
Default is False.
'''
for obj_reader, path, ioobj in iter_generic_readers(ioclass, filenames,
directory=directory,
target=target,
return_path=True,
return_ioobj=True,
clean=clean,
readall=readall):
obj = obj_reader(cascade=cascade, lazy=lazy)
if not return_path and not return_ioobj and not return_reader:
yield obj
else:
obj = (obj, )
if return_path:
obj = obj + (path,)
if return_ioobj:
obj = obj + (ioobj,)
if return_reader:
obj = obj + (obj_reader,)
yield obj
def write_generic(ioobj, target=None, obj=None, return_writer=False):
'''
Write the target object to a file using the given neo io object ioobj.
If target is None, use the first supported_objects from ioobj
If target is False, use the 'write' method.
If target is the Block or Segment class, use write_block or write_segment,
respectively.
If target is a string, use 'write_'+target.
obj is the object to write. If obj is None, an object is created
automatically for the io class.
If return_writer is True, yield the io writer function as well as the
object. yield obj, writer. Default is False.
'''
if obj is None:
supported_objects = ioobj.supported_objects
obj = generate_from_supported_objects(supported_objects)
obj_writer = create_generic_writer(ioobj, target=target)
obj_writer(obj)
if return_writer:
return obj, obj_writer
return obj
|