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
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))
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
def test_no_delimiter(): pr = PatternReplacer("replaceme", '-/') assert pr.get_n_delimiters() == 0 assert pr.get_n_tokens() == 1, pr.get_n_tokens() assert pr.replace(['onetoken']) == 'onetoken'
def test_other_delimiter(): pr = PatternReplacer(r'abd#k\hj#$', '#$') assert pr.get_n_delimiters() == 2 assert pr.replace(['first', 'second']) == r'first#second#$'
def test_replace(): tokens = ['first', 'second', 'third', 'fourth', 'fifth'] pr = PatternReplacer(samplepattern_no, '-/') assert pr.replace(tokens) == 'first-second-third-/fourth/-fifth', pr.replace(tokens) pr = PatternReplacer(samplepattern_begin, '-/') assert pr.replace(tokens) == '/-first-second-third-/fourth/-fifth', pr.replace(tokens) pr = PatternReplacer(samplepattern_end, '-/') assert pr.replace(tokens) == '/-first-second-third-/fourth/-fifth/', pr.replace(tokens) pr = PatternReplacer(samplepattern_both, '-/') assert pr.replace(tokens[0:4]) == '///first-second-/third/-fourth/', pr.replace(tokens[0:4])
def test_number_of_tokens(): pats = [(samplepattern_no, 5), (samplepattern_begin, 5), (samplepattern_end, 5), (samplepattern_both, 4)] for pat, expect in pats: pr = PatternReplacer(pat, '-/') assert pr.get_n_tokens() == expect, pat
def test_finds_proper_number_of_delimiters(): pr = PatternReplacer(samplepattern_no, '-/') assert pr.get_n_delimiters() == 4 pr = PatternReplacer(samplepattern_both, '-/') assert pr.get_n_delimiters() == 5