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