|
9 | 9 | import fnmatch |
10 | 10 | import zipfile as zf |
11 | 11 | from os import path |
| 12 | +from pathlib import Path |
12 | 13 |
|
13 | | -from ..compat import _pattern_type, get_filedialog, path_types |
| 14 | +from ..compat import _pattern_type, get_filedialog |
14 | 15 | from ..core.data import Data |
15 | 16 | from ..formats.utils.zip import test_is_zip |
16 | 17 | from .core import BaseFolder |
17 | 18 | from .mixins import DiskBasedFolderMixin |
18 | 19 | from .utils import pathjoin |
19 | 20 |
|
20 | 21 |
|
| 22 | +def _prune_list(p, files): |
| 23 | + """Prune the files list of entries that match pattern p.""" |
| 24 | + if isinstance(p, str): |
| 25 | + for f in list(fnmatch.filter(files, p)): |
| 26 | + del files[files.index(f)] |
| 27 | + if isinstance(p, _pattern_type): |
| 28 | + matched = [] |
| 29 | + # For reg expts we iterate over all files, but we can't delete matched |
| 30 | + # files as we go as we're iterating over them - so we store the |
| 31 | + # indices and delete them later. |
| 32 | + for f in files: |
| 33 | + if p.search(f): |
| 34 | + matched.append(files.index(f)) |
| 35 | + matched.sort(reverse=True) |
| 36 | + for i in matched: # reverse sort the matching indices to safely delete |
| 37 | + del files[i] |
| 38 | + |
| 39 | + |
| 40 | +def _build_list(fldr, p, files): |
| 41 | + """Add matching files from the list to folder if matching pattern p.""" |
| 42 | + if isinstance(p, str): |
| 43 | + for f in fnmatch.filter(files, p): |
| 44 | + del files[files.index(f)] |
| 45 | + f.replace(path.sep, "/") |
| 46 | + fldr.append(f) |
| 47 | + elif isinstance(p, _pattern_type): |
| 48 | + matched = [] |
| 49 | + # For reg expts we iterate over all files, but we can't delete matched |
| 50 | + # files as we go as we're iterating over them - so we store the |
| 51 | + # indices and delete them later. |
| 52 | + for ix, f in enumerate(files): |
| 53 | + if p.search(f): |
| 54 | + f.replace(path.sep, "/") |
| 55 | + fldr.append(f) |
| 56 | + else: |
| 57 | + matched.append(ix) |
| 58 | + for i in reversed(matched): # reverse sort the matching indices to safely delete |
| 59 | + del files[i] |
| 60 | + |
| 61 | + |
21 | 62 | class ZipFolder(DiskBasedFolderMixin, BaseFolder): |
22 | 63 | """A sub class of DataFile that sores itself in a zip file. |
23 | 64 |
|
@@ -95,72 +136,39 @@ def getlist(self, recursive=None, directory=None, flatten=None, **_): |
95 | 136 | if flatten is None: |
96 | 137 | flatten = self.flat |
97 | 138 |
|
98 | | - if self.File is None and directory is None: |
99 | | - self.File = zf.ZipFile(self._zip_file_dialog(), "r") |
100 | | - close_me = True |
101 | | - elif isinstance(directory, zf.ZipFile): |
102 | | - if directory.fp: |
| 139 | + match directory: |
| 140 | + case None if self.File is None: |
| 141 | + self.File = zf.ZipFile(self._zip_file_dialog(), "r") # pylint: disable=R1732 |
| 142 | + close_me = True |
| 143 | + case zf.ZipFile() if directory.fp: |
103 | 144 | self.File = directory |
104 | 145 | close_me = False |
105 | | - else: |
106 | | - self.File = zf.ZipFile(directory, "r") |
| 146 | + case zf.ZipFile(): |
| 147 | + self.File = zf.ZipFile(directory, "r") # pylint: disable=R1732 |
107 | 148 | close_me = True |
108 | | - elif isinstance(directory, path_types) and path.isdir(directory): # Fall back to DataFolder |
109 | | - return super().getlist(recursive=recursive, directory=directory, flatten=flatten) |
110 | | - elif isinstance(directory, path_types) and zf.is_zipfile(directory): |
111 | | - self.File = zf.ZipFile(directory, "r") |
112 | | - close_me = True |
113 | | - elif isinstance(self.File, zf.ZipFile): |
114 | | - if self.File.fp: |
| 149 | + case str() | Path() if path.isdir(directory): |
| 150 | + return super().getlist(recursive=recursive, directory=directory, flatten=flatten) |
| 151 | + case str() | Path() if zf.is_zipfile(directory): |
| 152 | + self.File = zf.ZipFile(directory, "r") # pylint: disable=R1732 |
| 153 | + close_me = True |
| 154 | + case _ if isinstance(self.File, zf.ZipFile) and self.File.fp: |
115 | 155 | close_me = False |
116 | | - else: |
| 156 | + case _ if isinstance(self.File, zf.ZipFile): |
117 | 157 | self.File = zf.ZipFile(self.File.filename, "r") # pylint: disable=R1732 |
118 | 158 | close_me = True |
119 | | - else: |
120 | | - raise IOError(f"{directory} does not appear to be zip file!") |
| 159 | + case _: |
| 160 | + raise IOError(f"{directory} does not appear to be zip file!") |
121 | 161 | # At this point directory contains an open h5py.File object, or possibly a group |
122 | 162 | self.path = self.File.filename |
123 | 163 | files = [x.filename for x in self.File.filelist] |
124 | 164 | for p in self.exclude: # Remove excluded files |
125 | | - if isinstance(p, str): |
126 | | - for f in list(fnmatch.filter(files, p)): |
127 | | - del files[files.index(f)] |
128 | | - if isinstance(p, _pattern_type): |
129 | | - matched = [] |
130 | | - # For reg expts we iterate over all files, but we can't delete matched |
131 | | - # files as we go as we're iterating over them - so we store the |
132 | | - # indices and delete them later. |
133 | | - for f in files: |
134 | | - if p.search(f): |
135 | | - matched.append(files.index(f)) |
136 | | - matched.sort(reverse=True) |
137 | | - for i in matched: # reverse sort the matching indices to safely delete |
138 | | - del files[i] |
139 | | - |
| 165 | + _prune_list(p, files) |
140 | 166 | for p in self.pattern: # pattern is a list of strings and regeps |
141 | | - if isinstance(p, str): |
142 | | - for f in fnmatch.filter(files, p): |
143 | | - del files[files.index(f)] |
144 | | - f.replace(path.sep, "/") |
145 | | - self.append(f) |
146 | | - elif isinstance(p, _pattern_type): |
147 | | - matched = [] |
148 | | - # For reg expts we iterate over all files, but we can't delete matched |
149 | | - # files as we go as we're iterating over them - so we store the |
150 | | - # indices and delete them later. |
151 | | - for ix, f in enumerate(files): |
152 | | - if p.search(f): |
153 | | - f.replace(path.sep, "/") |
154 | | - self.append(f) |
155 | | - else: |
156 | | - matched.append(ix) |
157 | | - for i in reversed(matched): # reverse sort the matching indices to safely delete |
158 | | - del files[i] |
159 | | - |
| 167 | + _build_list(self, p, files) |
160 | 168 | self._zip_contents = files |
161 | 169 |
|
162 | 170 | if flatten is None or not flatten: |
163 | | - self.unflatten() |
| 171 | + self.unflatten() # pylint: disable=no-member |
164 | 172 | if close_me: |
165 | 173 | self.File.close() |
166 | 174 | return self |
@@ -260,7 +268,7 @@ def save(self, root=None): |
260 | 268 | self.File.close() |
261 | 269 | mode = "a" if path.exists(root) else "w" |
262 | 270 | with zf.ZipFile(root, mode) as self.File: |
263 | | - tmp = self.walk_groups(self._saver) |
| 271 | + tmp = self.walk_groups(self._saver) # pylint: disable=no-member |
264 | 272 | return tmp |
265 | 273 |
|
266 | 274 | def _saver(self, f, trail): |
|
0 commit comments