def string_format(filepattern): f = [] last = 0 for match in _wildcard_regex.finditer(filepattern): f.append(filepattern[last:match.start()]) wildcard = match.group("name") f.append("{{{}}}".format(wildcard)) last = match.end() f.append(filepattern[last:]) return "".join(f)
def matches(text): return [(match.group("name"), match.group("constraint")) for match in _wildcard_regex.finditer(text)]
def matches(text): return [(match.group('name'), match.group('constraint')) for match in _wildcard_regex.finditer(text)]
def glob_wildcards(pattern, files=None): """ Glob the values of the wildcards by matching the given pattern to the filesystem. Returns a named tuple with a list of values for each wildcard. """ from snakemake.io import _wildcard_regex, namedtuple, regex import regex as re pattern = os.path.normpath(pattern) first_wildcard = re.search("{[^{]", pattern) dirname = os.path.dirname(pattern[:first_wildcard.start()] ) if first_wildcard else os.path.dirname(pattern) if not dirname: dirname = "." names = [ match.group('name') for match in _wildcard_regex.finditer(pattern) ] Wildcards = namedtuple("Wildcards", names) wildcards = Wildcards(*[list() for name in names]) pattern = regex(pattern) # work around partial matching bug in python regex module # by replacing matches for "\" with "[/\0]" (0x0 can't occur in filenames) pattern = re.sub('\\\\/', '[/\0]', pattern) cpattern = re.compile(pattern) def walker(dirname, pattern): """finds files/dirs matching `pattern` in `dirname`""" for dirpath, dirnames, filenames in os.walk(dirname): dirpath = os.path.normpath(dirpath) for f in filenames: if dirpath != ".": f = os.path.join(dirpath, f) match = pattern.match(f) if match: yield match for i in range(len(dirnames) - 1, -1, -1): d = dirnames[i] if dirpath != ".": d = os.path.join(dirpath, d) match = pattern.match(os.path.join(d, ""), partial=True) if not match: del dirnames[i] continue if match.partial: continue yield match print("searching {}".format(pattern)) if files is None: for match in walker(dirname, cpattern): for name, value in match.groupdict().items(): getattr(wildcards, name).append(value) else: for f in files: match = re.match(cpattern, os.normpath(f)) if match: for name, value in match.groupdict().items(): getattr(wildcards, name).append(value) print("searching {}: done".format(pattern)) return wildcards