-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdsiterator.py
More file actions
executable file
·340 lines (252 loc) · 11.5 KB
/
pdsiterator.py
File metadata and controls
executable file
·340 lines (252 loc) · 11.5 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
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
################################################################################
# pdsiterator.py: PdsIterator class iterates across related files, jumping
# into adjacent directories and parallel volumes when required.
################################################################################
import os
import fnmatch
import pdsfile
import pdslogger
# Useful filters
def dirs_only(parent_pdsfile, basename):
if parent_pdsfile is None: return True
child_pdsfile = parent_pdsfile.child(basename)
return child_pdsfile.isdir
def same_in_next_anchor(parent_pdsfile, basename):
parts = pdsfile.PdsFile.from_logical_path(self.current_logical_path).split()
return basename.endswith(parts[1] + parts[2])
################################################################################
# Cache
################################################################################
DIRECTORY_CACHE = {}
################################################################################
# PdsDirIterator
################################################################################
class PdsDirIterator(object):
def __init__(self, pdsf, sign=1, logger=None):
global DIRECTORY_CACHE
if pdsf is None:
self.neighbors = []
self.neighbor_index = 0
self.current_logical_path = None
self.sign = 1
fnmatch_patterns = pdsf.NEIGHBORS.first(pdsf.logical_path)
if isinstance(fnmatch_patterns, str):
fnmatch_patterns = (fnmatch_patterns,)
if fnmatch_patterns:
if fnmatch_patterns in DIRECTORY_CACHE:
logical_paths = DIRECTORY_CACHE[fnmatch_patterns]
else:
paths = []
for fnmatch_pattern in fnmatch_patterns:
abspaths = pdsfile.PdsFile.glob_glob(pdsf.root_ +
fnmatch_pattern)
abspaths = [pdsfile.repair_case(p) for p in abspaths]
abspaths = [a for a in abspaths if os.path.isdir(a)]
paths += pdsfile.PdsFile.logicals_for_abspaths(abspaths)
# Remove duplicates
paths = list(set(paths))
# Remove blanks (although they shouldn't be there)
if '' in paths:
paths.remove('')
# Sort based on the rules
logical_paths = pdsfile.PdsFile.sort_logical_paths(paths)
DIRECTORY_CACHE[fnmatch_patterns] = logical_paths
else:
logical_paths = [pdsf.logical_path]
# Case insensitive search
logical_paths_lc = [p.lower() for p in logical_paths]
this_path_lc = pdsf.logical_path.lower()
try:
self.neighbor_index = logical_paths_lc.index(this_path_lc)
except ValueError:
logical_paths.append(pdsf.logical_path)
logical_paths = pdsfile.PdsFile.sort_logical_paths(logical_paths)
self.neighbor_index = logical_paths.index(pdsf.logical_path)
self.sign = -1 if sign < 0 else +1
self.neighbors = logical_paths
self.neighbors_lc = [p.lower() for p in logical_paths]
self.current_logical_path = pdsf.logical_path
self.logger = logger
def copy(self, sign=None):
"""Return a clone of this iterator, possibly reversed."""
if sign is None:
sign1 = self.sign
else:
sign1 = -1 if sign < 0 else +1
pdsf = pdsfile.PdsFile.from_logical_path(self.current_logical_path)
this = PdsDirIterator(pdsf, sign=sign1, logger=self.logger)
return this
############################################################################
# Iterator
############################################################################
def __iter__(self):
return self
def next(self):
return self.__next__()
def __next__(self):
"""Iterator returns (logical_path, display path, level)"""
prev_logical_path = self.current_logical_path
# Try to return the next neighbor
self.neighbor_index += self.sign
if (self.neighbor_index < 0 or
self.neighbor_index >= len(self.neighbors)):
raise StopIteration
# If we get this far, figure out what to return
# Find the common parts of the previous logical path and this one
neighbor = self.neighbors[self.neighbor_index]
prev_parts = prev_logical_path.split('/')
new_parts = neighbor.split('/')
for k in range(len(prev_parts)):
if prev_parts[k] != new_parts[k]:
break
# The display path is the string starting where prev and this differ
new_parts = new_parts[k:]
display_path = '/'.join(new_parts)
# Level is 0 if prev and this are the same up to the basename; otherwise
# level is 1.
if len(new_parts) == 1:
level = 0
else:
level = 1
return (neighbor, display_path, level)
################################################################################
# PdsFileIterator
################################################################################
class PdsFileIterator(object):
def __init__(self, pdsf, sign=1, pattern=None, exclude=None, filter=None,
logger=None):
self.parent = pdsf.parent()
self.dir_iterator = PdsDirIterator(self.parent, sign, logger=logger)
self.pattern = pattern
self.exclude = exclude
self.filter = filter
self.sign = self.dir_iterator.sign
self.current_logical_path = pdsf.logical_path
# the pattern applies to the basenam
basenames = self.parent.sort_basenames(self.parent.childnames)
basenames = self._filter_names(basenames)
basenames_lc = [n.lower() for n in basenames]
# If this object is missing, insert it into the list of siblings
if pdsf.basename.lower() not in basenames_lc:
basenames.append(pdsf.basename)
basenames = self.parent.sort_basenames(basenames)
self.sibnames = self.parent.logicals_for_basenames(basenames)
self.sibnames_lc = [n.lower() for n in self.sibnames]
self.logger = logger
# Case-insensitive search
logical_path_lc = pdsf.logical_path.lower()
self.sibling_index = self.sibnames_lc.index(logical_path_lc)
def copy(self, sign=None):
"""Return a clone of this iterator."""
if sign is None:
sign1 = self.sign
else:
sign1 = -1 if sign < 0 else +1
pdsf = pdsfile.PdsFile.from_logical_path(self.current_logical_path)
this = PdsFileIterator(pdsf, sign=sign1,
pattern=self.pattern, exclude=self.exclude,
filter=self.filter, logger=self.logger)
return this
def _filter_names(self, basenames):
if self.pattern:
basenames = [s for s in basenames
if fnmatch.fnmatch(s, self.pattern)]
if self.exclude:
basenames = [s for s in basenames
if not fnmatch.fnmatch(s, self.exclude)]
if self.filter:
basenames = [s for s in basenames if self.filter(self.parent, s)]
return basenames
############################################################################
# Iterator
############################################################################
def __iter__(self):
return self
def next(self):
return self.__next__()
def __next__(self):
"""Iterator returns (logical_path, display path, level of jump)
Level of jump is 0 for a sibling, 1 for a cousin.
Display path is the part of the path that has changed.
At level 0, it is basename;
At level 1, it is parent directory/basename;
"""
# Try to return the next sibling
try:
self.sibling_index += self.sign
if self.sibling_index < 0:
raise IndexError()
sibname = self.sibnames[self.sibling_index]
# Jump to adjacent directory if necessary
except IndexError:
return self.next_cousin()
# Otherwise return the next sibling
else:
self.current_logical_path = sibname
return (sibname, os.path.basename(sibname), 0)
def next_cousin(self):
"""Move the iteration into the adjacent parent directory."""
prev_logical_path = self.current_logical_path
# Go to the next parent
(parent_logical_path, parent_display_path, _) = self.dir_iterator.next()
self.parent = pdsfile.PdsFile.from_logical_path(parent_logical_path)
# Load the next set of siblings
basenames = self.parent.sort_basenames(self.parent.childnames)
basenames = self._filter_names(basenames)
self.sibnames = self.parent.logicals_for_basenames(basenames)
# Return the first new sibling
if self.sign > 0:
self.sibling_index = -1
else:
self.sibling_index = len(self.sibnames)
(logical_path, basename, _) = self.next()
return (logical_path, parent_display_path + '/' + basename, 1)
################################################################################
# PdsRowIterator
################################################################################
class PdsRowIterator(object):
def __init__(self, pdsf, sign=1, logger=None):
self.parent_pdsf = pdsf.parent()
self.parent_logical_path_ = self.parent_pdsf.logical_path + '/'
self.sign = sign
basenames = self.parent_pdsf.childnames
basenames_lc = self.parent_pdsf.childnames_lc
# If this object is missing, insert it into the list of siblings
this_basename_lc = pdsf.basename.lower()
if this_basename_lc not in basenames_lc:
basenames.append(pdsf.basename)
basenames = self.parent_pdsf.sort_basenames(basenames)
basenames_lc = [n.lower() for n in basenames]
self.sibnames = basenames
self.sibnames_lc = basenames_lc
self.sibling_index = basenames_lc.index(this_basename_lc)
self.logger = logger
def copy(self, sign=None):
"""Return a clone of this iterator."""
if sign is None:
sign1 = self.sign
else:
sign1 = -1 if sign < 0 else +1
childname = self.sibnames[self.sibling_index]
return PdsRowIterator(self.parent_pdsf.child(childname), sign=sign1,
logger=self.logger)
############################################################################
# Iterator
############################################################################
def __iter__(self):
return self
def next(self):
return self.__next__()
def __next__(self):
"""Iterator returns (logical_path, display path, level of jump)
Level of jump is 0 for a sibling, 1 for a cousin.
Display path is the part of the path that has changed.
At level 0, it is basename;
At level 1, it is parent directory/basename;
"""
self.sibling_index += self.sign
if self.sibling_index < 0 or self.sibling_index >= len(self.sibnames):
raise StopIteration
sibname = self.sibnames[self.sibling_index]
return (self.parent_logical_path_ + sibname, sibname, 0)
################################################################################