1.1 --- a/moinformat/utils/directory.py Sat Aug 04 16:57:49 2018 +0200
1.2 +++ b/moinformat/utils/directory.py Sun Aug 05 18:07:02 2018 +0200
1.3 @@ -19,8 +19,9 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from glob import glob
1.8 +from os import makedirs, walk
1.9 from os.path import abspath, commonprefix, exists, join
1.10 +import fnmatch
1.11
1.12 # Get the directory with trailing path separator when assessing path prefixes
1.13 # in order to prevent sibling directory confusion.
1.14 @@ -81,6 +82,19 @@
1.15 else:
1.16 raise ValueError, filename
1.17
1.18 + def makedirs(self, filename):
1.19 +
1.20 + """
1.21 + Ensure that a directory having the given 'filename' exists by creating
1.22 + it and any directories needed for it to be created. This filename is
1.23 + relative to the directory.
1.24 + """
1.25 +
1.26 + pathname = self.get_filename(filename)
1.27 +
1.28 + if not exists(pathname):
1.29 + makedirs(pathname)
1.30 +
1.31 def select_files(self, pattern):
1.32
1.33 """
1.34 @@ -88,15 +102,34 @@
1.35 'pattern'. These filenames are relative to the directory.
1.36 """
1.37
1.38 - full_pattern = self.get_filename(pattern)
1.39 + selected = []
1.40 +
1.41 + # Obtain pathnames, directory names and filenames within the directory.
1.42 +
1.43 + for dirpath, dirnames, filenames in walk(self.filename):
1.44 + for filename in filenames:
1.45
1.46 - filenames = []
1.47 + # Qualify filenames with the directory path.
1.48 +
1.49 + pathname = join(dirpath, filename)
1.50 +
1.51 + # Obtain the local filename within the directory.
1.52 +
1.53 + local_filename = self.within(pathname)
1.54
1.55 - for filename in glob(full_pattern):
1.56 - filename = within(filename, self.filename)
1.57 - if filename:
1.58 - filenames.append(filename)
1.59 + # Match filenames supporting the pattern.
1.60 +
1.61 + if local_filename and fnmatch.fnmatch(local_filename, pattern):
1.62 + selected.append(local_filename)
1.63 +
1.64 + return selected
1.65
1.66 - return filenames
1.67 + def within(self, filename):
1.68 +
1.69 + """
1.70 + Return the given 'filename' translated to be relative to this directory.
1.71 + """
1.72 +
1.73 + return within(filename, self.filename)
1.74
1.75 # vim: tabstop=4 expandtab shiftwidth=4