BUG: Use manager to set title
1 |
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
|
|
2 |
# vi: set ft=python sts=4 ts=4 sw=4 et:
|
|
3 |
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
|
4 |
#
|
|
5 |
# See COPYING file distributed along with the NiBabel package for the
|
|
6 |
# copyright and license terms.
|
|
7 |
#
|
|
8 |
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
|
9 | 33 |
""" Fileholder class """
|
10 |
|
|
11 | 33 |
from copy import copy |
12 |
|
|
13 | 33 |
from .openers import ImageOpener |
14 |
|
|
15 |
|
|
16 | 33 |
class FileHolderError(Exception): |
17 | 33 |
pass
|
18 |
|
|
19 |
|
|
20 | 33 |
class FileHolder(object): |
21 |
""" class to contain filename, fileobj and file position
|
|
22 |
"""
|
|
23 |
|
|
24 | 33 |
def __init__(self, |
25 |
filename=None, |
|
26 |
fileobj=None, |
|
27 |
pos=0): |
|
28 |
""" Initialize FileHolder instance
|
|
29 |
|
|
30 |
Parameters
|
|
31 |
----------
|
|
32 |
filename : str, optional
|
|
33 |
filename. Default is None
|
|
34 |
fileobj : file-like object, optional
|
|
35 |
Should implement at least 'seek' (for the purposes for this
|
|
36 |
class). Default is None
|
|
37 |
pos : int, optional
|
|
38 |
position in filename or fileobject at which to start reading
|
|
39 |
or writing data; defaults to 0
|
|
40 |
"""
|
|
41 | 33 |
self.filename = filename |
42 | 33 |
self.fileobj = fileobj |
43 | 33 |
self.pos = pos |
44 |
|
|
45 | 33 |
def get_prepare_fileobj(self, *args, **kwargs): |
46 |
""" Return fileobj if present, or return fileobj from filename
|
|
47 |
|
|
48 |
Set position to that given in self.pos
|
|
49 |
|
|
50 |
Parameters
|
|
51 |
----------
|
|
52 |
*args : tuple
|
|
53 |
positional arguments to file open. Ignored if there is a
|
|
54 |
defined ``self.fileobj``. These might include the mode, such
|
|
55 |
as 'rb'
|
|
56 |
**kwargs : dict
|
|
57 |
named arguments to file open. Ignored if there is a
|
|
58 |
defined ``self.fileobj``
|
|
59 |
|
|
60 |
Returns
|
|
61 |
-------
|
|
62 |
fileobj : file-like object
|
|
63 |
object has position set (via ``fileobj.seek()``) to
|
|
64 |
``self.pos``
|
|
65 |
"""
|
|
66 |
if self.fileobj is not None: |
|
67 | 33 |
obj = ImageOpener(self.fileobj) # for context manager |
68 | 33 |
obj.seek(self.pos) |
69 |
elif self.filename is not None: |
|
70 | 33 |
obj = ImageOpener(self.filename, *args, **kwargs) |
71 |
if self.pos != 0: |
|
72 |
obj.seek(self.pos) |
|
73 |
else: |
|
74 | 33 |
raise FileHolderError('No filename or fileobj present') |
75 | 33 |
return obj |
76 |
|
|
77 | 33 |
def same_file_as(self, other): |
78 |
""" Test if `self` refers to same files / fileobj as `other`
|
|
79 |
|
|
80 |
Parameters
|
|
81 |
----------
|
|
82 |
other : object
|
|
83 |
object with `filename` and `fileobj` attributes
|
|
84 |
|
|
85 |
Returns
|
|
86 |
-------
|
|
87 |
tf : bool
|
|
88 |
True if `other` has the same filename (or both have None) and the
|
|
89 |
same fileobj (or both have None
|
|
90 |
"""
|
|
91 | 33 |
return ((self.filename == other.filename) and |
92 |
(self.fileobj == other.fileobj)) |
|
93 |
|
|
94 | 33 |
@property
|
95 | 5 |
def file_like(self): |
96 |
""" Return ``self.fileobj`` if not None, otherwise ``self.filename``
|
|
97 |
"""
|
|
98 | 33 |
return self.fileobj if self.fileobj is not None else self.filename |
99 |
|
|
100 |
|
|
101 | 33 |
def copy_file_map(file_map): |
102 |
r""" Copy mapping of fileholders given by `file_map` |
|
103 |
|
|
104 |
Parameters
|
|
105 |
----------
|
|
106 |
file_map : mapping
|
|
107 |
mapping of ``FileHolder`` instances
|
|
108 |
|
|
109 |
Returns
|
|
110 |
-------
|
|
111 |
fm_copy : dict
|
|
112 |
Copy of `file_map`, using shallow copy of ``FileHolder``\s
|
|
113 |
|
|
114 |
"""
|
|
115 | 33 |
fm_copy = {} |
116 |
for key, fh in file_map.items(): |
|
117 | 33 |
fm_copy[key] = copy(fh) |
118 | 33 |
return fm_copy |
Read our documentation on viewing source code .