1 #!/usr/bin/env python 2 3 """ 4 Directory context functionality. 5 6 Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from glob import glob 23 from os.path import abspath, commonprefix, exists, join 24 25 # Get the directory with trailing path separator when assessing path prefixes 26 # in order to prevent sibling directory confusion. 27 28 def inside(filename, dirname): 29 30 "Return whether 'filename' is inside 'dirname'." 31 32 dirname = join(dirname, "") 33 return commonprefix((filename, dirname)) == dirname 34 35 def within(filename, dirname): 36 37 "Return the part of 'filename' found within 'dirname'." 38 39 dirname = join(dirname, "") 40 prefix = commonprefix((filename, dirname)) 41 42 if prefix == dirname: 43 return filename[len(prefix):] 44 else: 45 return None 46 47 48 49 class Directory: 50 51 "A directory abstraction." 52 53 def __init__(self, filename): 54 55 "Initialise the abstraction with the given 'filename'." 56 57 self.filename = abspath(filename) 58 59 def exists(self, filename): 60 61 """ 62 Return whether 'filename' exists within the directory. This filename 63 is relative to the directory. 64 """ 65 66 return exists(self.get_filename(filename)) 67 68 def get_filename(self, filename): 69 70 """ 71 Return the full path of a file with the given 'filename' found within 72 the directory. The full path is an absolute path. 73 """ 74 75 # Get the absolute path for the combination of directory and filename. 76 77 pathname = abspath(join(self.filename, filename)) 78 79 if inside(pathname, self.filename): 80 return pathname 81 else: 82 raise ValueError, filename 83 84 def select_files(self, pattern): 85 86 """ 87 Return a list of filenames found within the directory matching 88 'pattern'. These filenames are relative to the directory. 89 """ 90 91 full_pattern = self.get_filename(pattern) 92 93 filenames = [] 94 95 for filename in glob(full_pattern): 96 filename = within(filename, self.filename) 97 if filename: 98 filenames.append(filename) 99 100 return filenames 101 102 # vim: tabstop=4 expandtab shiftwidth=4