def test_verify(): pr = PatternReplacer(samplepattern_no, '-/') assert pr.verify('i-want-to-/a/-g') is True assert pr.verify('i-wan-tto-/a/-g') is True assert pr.verify('i-wan-tto-/ag/-') is False assert pr.verify('-iwan-tto-/a/-g') is False assert pr.verify('i-wan-t-to-/a-g') is False assert pr.verify('/i-want-to-/ag') is False assert pr.verify('/i-wantto-/ag/') is False
class SubsetImageModel: def __init__(self, paths, split_tokens="-", suffix=""): self._paths = paths self._split_token = split_tokens self._suffix = suffix self._view = None self._active_subset = None self._glob_pattern = None self.filenames = None # get subdirs of imagedir subdirs = self._paths # use first path as pattern print("Creating pattern") self.pr = PatternReplacer(subdirs[0], split_tokens) self._n_subsets = self.pr.get_n_tokens() # all subdirs should have the same number of tokens print("Verifying filenames") assert all(self.pr.verify(s) for s in subdirs), "All paths must have the same number of tokens" # add asterisks self.subsets = [set("*") for i in range(self._n_subsets)] for subdir in subdirs: tokens = re.split("|".join(re.escape(t) for t in split_tokens), subdir) tokens = [t for t in tokens if t] # remove empty ones assert len(tokens) == self._n_subsets for i, t in enumerate(tokens): self.subsets[i].add(t) self.default_subsets = ["*"] * self._n_subsets # Mapping from changeable token index to index in complete range self._perm = [] for i, subset in enumerate(self.subsets): if len(subset) != 2: # * and one token self._perm.append(i) else: # fix tokens which are unique. No need to glob them assert "*" in subset self.default_subsets[i] = (subset - set("*")).pop() self.set_active_subset(["*"] * len(self._perm)) def _changeable_to_total(self, changeable): # fill total pattern with changeable using perm pass def __total_to_changeable(self, total): # use only the ones mentioned in perm pass def get_subsets(self): # return only changeable subsets return [sorted(self.subsets[i]) for i in self._perm] def get_filenames(self): return self.filenames def set_active_subset(self, active_subset): self._active_subset = active_subset self._update_filenames() def _update_filenames(self): # glob everything tokens = list(self.default_subsets) # but fill the given values for i, token in enumerate(self._active_subset): tokens[self._perm[i]] = token self._glob_pattern = self.pr.replace(tokens) self.filenames = sorted(fnmatch.filter(self._paths, self._glob_pattern)) self.filenames = [fn + self._suffix for fn in self.filenames] def get_active_subset(self): return self._active_subset def get_glob_pattern(self): return self._glob_pattern