# HG changeset patch # User Paul Boddie # Date 1533485222 -7200 # Node ID df6611ecc1a5ebbcf1c9e5f4284cf03ffc714428 # Parent bc150f6c2567f7c7be4f789b93a3b76eb0c311dd Changed file selection to match entire names within the directory. Added makedirs and within methods. diff -r bc150f6c2567 -r df6611ecc1a5 moinformat/utils/directory.py --- a/moinformat/utils/directory.py Sat Aug 04 16:57:49 2018 +0200 +++ b/moinformat/utils/directory.py Sun Aug 05 18:07:02 2018 +0200 @@ -19,8 +19,9 @@ this program. If not, see . """ -from glob import glob +from os import makedirs, walk from os.path import abspath, commonprefix, exists, join +import fnmatch # Get the directory with trailing path separator when assessing path prefixes # in order to prevent sibling directory confusion. @@ -81,6 +82,19 @@ else: raise ValueError, filename + def makedirs(self, filename): + + """ + Ensure that a directory having the given 'filename' exists by creating + it and any directories needed for it to be created. This filename is + relative to the directory. + """ + + pathname = self.get_filename(filename) + + if not exists(pathname): + makedirs(pathname) + def select_files(self, pattern): """ @@ -88,15 +102,34 @@ 'pattern'. These filenames are relative to the directory. """ - full_pattern = self.get_filename(pattern) + selected = [] + + # Obtain pathnames, directory names and filenames within the directory. + + for dirpath, dirnames, filenames in walk(self.filename): + for filename in filenames: - filenames = [] + # Qualify filenames with the directory path. + + pathname = join(dirpath, filename) + + # Obtain the local filename within the directory. + + local_filename = self.within(pathname) - for filename in glob(full_pattern): - filename = within(filename, self.filename) - if filename: - filenames.append(filename) + # Match filenames supporting the pattern. + + if local_filename and fnmatch.fnmatch(local_filename, pattern): + selected.append(local_filename) + + return selected - return filenames + def within(self, filename): + + """ + Return the given 'filename' translated to be relative to this directory. + """ + + return within(filename, self.filename) # vim: tabstop=4 expandtab shiftwidth=4